Updated doc/NEWS file
[midnight-commander.git] / lib / vfs / interface.c
blob914fc4e94f60f977c796da89b96af069f273eb2b
1 /*
2 Virtual File System: interface functions
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /**
27 * \file
28 * \brief Source: Virtual File System: path handlers
29 * \author Slava Zanko
30 * \date 2011
34 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h> /* For atol() */
38 #include <stdarg.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <signal.h>
43 #include <ctype.h> /* is_digit() */
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <dirent.h>
48 #include <pwd.h>
49 #include <grp.h>
51 #include "lib/global.h"
53 #include "lib/widget.h" /* message() */
54 #include "lib/strutil.h" /* str_crt_conv_from() */
55 #include "lib/util.h"
57 #include "vfs.h"
58 #include "utilvfs.h"
59 #include "path.h"
60 #include "gc.h"
61 #include "xdirentry.h"
63 extern GString *vfs_str_buffer;
64 extern struct vfs_class *current_vfs;
66 /*** global variables ****************************************************************************/
68 struct dirent *mc_readdir_result = NULL;
70 /*** file scope macro definitions ****************************************************************/
72 /*** file scope type declarations ****************************************************************/
74 /*** file scope variables ************************************************************************/
76 /*** file scope functions ************************************************************************/
77 /* --------------------------------------------------------------------------------------------- */
79 static vfs_path_t *
80 mc_def_getlocalcopy (const vfs_path_t * filename_vpath)
82 vfs_path_t *tmp_vpath = NULL;
83 int fdin = -1, fdout = -1;
84 ssize_t i;
85 char buffer[BUF_1K * 8];
86 struct stat mystat;
88 fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR);
89 if (fdin == -1)
90 goto fail;
92 fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath));
93 if (fdout == -1)
94 goto fail;
96 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
98 if (write (fdout, buffer, i) != i)
99 goto fail;
101 if (i == -1)
102 goto fail;
103 i = mc_close (fdin);
104 fdin = -1;
105 if (i == -1)
106 goto fail;
108 i = close (fdout);
109 fdout = -1;
110 if (i == -1)
111 goto fail;
113 if (mc_stat (filename_vpath, &mystat) != -1)
114 mc_chmod (tmp_vpath, mystat.st_mode);
116 return tmp_vpath;
118 fail:
119 vfs_path_free (tmp_vpath);
120 if (fdout != -1)
121 close (fdout);
122 if (fdin != -1)
123 mc_close (fdin);
124 return NULL;
127 /* --------------------------------------------------------------------------------------------- */
129 static int
130 mc_def_ungetlocalcopy (const vfs_path_t * filename_vpath,
131 const vfs_path_t * local_vpath, gboolean has_changed)
133 int fdin = -1, fdout = -1;
134 const char *local;
136 local = vfs_path_get_last_path_str (local_vpath);
138 if (has_changed)
140 char buffer[BUF_1K * 8];
141 ssize_t i;
143 if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL)
144 goto failed;
146 fdin = open (local, O_RDONLY);
147 if (fdin == -1)
148 goto failed;
149 fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC);
150 if (fdout == -1)
151 goto failed;
152 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
153 if (mc_write (fdout, buffer, (size_t) i) != i)
154 goto failed;
155 if (i == -1)
156 goto failed;
158 if (close (fdin) == -1)
160 fdin = -1;
161 goto failed;
163 fdin = -1;
164 if (mc_close (fdout) == -1)
166 fdout = -1;
167 goto failed;
170 unlink (local);
171 return 0;
173 failed:
174 message (D_ERROR, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath));
175 if (fdout != -1)
176 mc_close (fdout);
177 if (fdin != -1)
178 close (fdin);
179 unlink (local);
180 return -1;
183 /* --------------------------------------------------------------------------------------------- */
184 /*** public functions ****************************************************************************/
185 /* --------------------------------------------------------------------------------------------- */
188 mc_open (const vfs_path_t * vpath, int flags, ...)
190 int mode = 0, result = -1;
191 const vfs_path_element_t *path_element;
193 if (vpath == NULL)
194 return -1;
196 /* Get the mode flag */
197 if (flags & O_CREAT)
199 va_list ap;
200 va_start (ap, flags);
201 mode = va_arg (ap, int);
202 va_end (ap);
205 path_element = vfs_path_get_by_index (vpath, -1);
206 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
208 void *info;
209 /* open must be supported */
210 info = path_element->class->open (vpath, flags, mode);
211 if (info == NULL)
212 errno = vfs_ferrno (path_element->class);
213 else
214 result = vfs_new_handle (path_element->class, info);
216 else
217 errno = -EOPNOTSUPP;
219 return result;
222 /* --------------------------------------------------------------------------------------------- */
224 /* *INDENT-OFF* */
226 #define MC_NAMEOP(name, inarg, callarg) \
227 int mc_##name inarg \
229 int result; \
230 const vfs_path_element_t *path_element; \
232 if (vpath == NULL) \
233 return -1; \
235 path_element = vfs_path_get_by_index (vpath, -1); \
236 if (!vfs_path_element_valid (path_element)) \
238 return -1; \
241 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
242 if (result == -1) \
243 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
244 return result; \
247 MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
248 MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
249 MC_NAMEOP (utime, (const vfs_path_t *vpath, struct utimbuf * times), (vpath, times))
250 MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
251 MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
252 MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
253 MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
254 MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
256 /* *INDENT-ON* */
258 /* --------------------------------------------------------------------------------------------- */
261 mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
263 int result = -1;
265 if (vpath1 == NULL)
266 return -1;
268 if (vpath1 != NULL)
270 const vfs_path_element_t *path_element;
272 path_element = vfs_path_get_by_index (vpath2, -1);
273 if (vfs_path_element_valid (path_element))
275 result =
276 path_element->class->symlink != NULL ?
277 path_element->class->symlink (vpath1, vpath2) : -1;
279 if (result == -1)
280 errno =
281 path_element->class->symlink != NULL ?
282 vfs_ferrno (path_element->class) : E_NOTSUPP;
285 return result;
288 /* --------------------------------------------------------------------------------------------- */
290 /* *INDENT-OFF* */
292 #define MC_HANDLEOP(name, inarg, callarg) \
293 ssize_t mc_##name inarg \
295 struct vfs_class *vfs; \
296 int result; \
297 if (handle == -1) \
298 return -1; \
299 vfs = vfs_class_find_by_handle (handle); \
300 if (vfs == NULL) \
301 return -1; \
302 result = vfs->name != NULL ? vfs->name callarg : -1; \
303 if (result == -1) \
304 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
305 return result; \
308 MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_class_data_find_by_handle (handle), buffer, count))
309 MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data_find_by_handle (handle), buf, nbyte))
311 /* --------------------------------------------------------------------------------------------- */
313 #define MC_RENAMEOP(name) \
314 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
316 int result; \
317 const vfs_path_element_t *path_element1; \
318 const vfs_path_element_t *path_element2; \
320 if (vpath1 == NULL || vpath2 == NULL) \
321 return -1; \
323 path_element1 = vfs_path_get_by_index (vpath1, (-1)); \
324 path_element2 = vfs_path_get_by_index (vpath2, (-1)); \
326 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
327 path_element1->class != path_element2->class) \
329 errno = EXDEV; \
330 return -1; \
333 result = path_element1->class->name != NULL \
334 ? path_element1->class->name (vpath1, vpath2) \
335 : -1; \
336 if (result == -1) \
337 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
338 return result; \
341 MC_RENAMEOP (link)
342 MC_RENAMEOP (rename)
344 /* *INDENT-ON* */
346 /* --------------------------------------------------------------------------------------------- */
349 mc_ctl (int handle, int ctlop, void *arg)
351 struct vfs_class *vfs = vfs_class_find_by_handle (handle);
353 if (vfs == NULL)
354 return 0;
356 return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
359 /* --------------------------------------------------------------------------------------------- */
362 mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
364 int result = -1;
365 const vfs_path_element_t *path_element;
367 if (vpath == NULL)
368 vfs_die ("You don't want to pass NULL to mc_setctl.");
370 path_element = vfs_path_get_by_index (vpath, -1);
371 if (vfs_path_element_valid (path_element))
372 result =
373 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
374 ctlop, arg) : 0;
376 return result;
379 /* --------------------------------------------------------------------------------------------- */
382 mc_close (int handle)
384 struct vfs_class *vfs;
385 int result;
387 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
388 return -1;
390 vfs = vfs_class_find_by_handle (handle);
391 if (vfs == NULL)
392 return -1;
394 if (handle < 3)
395 return close (handle);
397 if (!vfs->close)
398 vfs_die ("VFS must support close.\n");
399 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
400 vfs_free_handle (handle);
401 if (result == -1)
402 errno = vfs_ferrno (vfs);
404 return result;
407 /* --------------------------------------------------------------------------------------------- */
409 DIR *
410 mc_opendir (const vfs_path_t * vpath)
412 int handle, *handlep;
413 void *info;
414 vfs_path_element_t *path_element;
416 if (vpath == NULL)
417 return NULL;
419 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
421 if (!vfs_path_element_valid (path_element))
423 errno = E_NOTSUPP;
424 return NULL;
427 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
429 if (info == NULL)
431 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
432 return NULL;
435 path_element->dir.info = info;
437 #ifdef HAVE_CHARSET
438 path_element->dir.converter = (path_element->encoding != NULL) ?
439 str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
440 if (path_element->dir.converter == INVALID_CONV)
441 path_element->dir.converter = str_cnv_from_term;
442 #endif
444 handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
446 handlep = g_new (int, 1);
447 *handlep = handle;
448 return (DIR *) handlep;
451 /* --------------------------------------------------------------------------------------------- */
453 struct dirent *
454 mc_readdir (DIR * dirp)
456 int handle;
457 struct vfs_class *vfs;
458 struct dirent *entry = NULL;
459 vfs_path_element_t *vfs_path_element;
460 #ifdef HAVE_CHARSET
461 estr_t state;
462 #endif
464 if (!mc_readdir_result)
466 /* We can't just allocate struct dirent as (see man dirent.h)
467 * struct dirent has VERY nonnaive semantics of allocating
468 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
469 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
470 * heap corrupter. So, allocate longliving dirent with at least
471 * (MAXNAMLEN + 1) for d_name in it.
472 * Strictly saying resulting dirent is unusable as we don't adjust internal
473 * structures, holding dirent size. But we don't use it in libc infrastructure.
474 * TODO: to make simpler homemade dirent-alike structure.
476 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
479 if (!dirp)
481 errno = EFAULT;
482 return NULL;
484 handle = *(int *) dirp;
486 vfs = vfs_class_find_by_handle (handle);
487 if (vfs == NULL)
488 return NULL;
490 vfs_path_element = vfs_class_data_find_by_handle (handle);
491 if (vfs->readdir)
493 entry = (*vfs->readdir) (vfs_path_element->dir.info);
494 if (entry == NULL)
495 return NULL;
497 g_string_set_size (vfs_str_buffer, 0);
498 #ifdef HAVE_CHARSET
499 state =
500 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
501 #else
502 g_string_assign (vfs_str_buffer, entry->d_name);
503 #endif
504 mc_readdir_result->d_ino = entry->d_ino;
505 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
507 if (entry == NULL)
508 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
509 return (entry != NULL) ? mc_readdir_result : NULL;
512 /* --------------------------------------------------------------------------------------------- */
515 mc_closedir (DIR * dirp)
517 int handle = *(int *) dirp;
518 struct vfs_class *vfs;
519 int result = -1;
521 vfs = vfs_class_find_by_handle (handle);
522 if (vfs != NULL)
524 vfs_path_element_t *vfs_path_element;
525 vfs_path_element = vfs_class_data_find_by_handle (handle);
527 #ifdef HAVE_CHARSET
528 if (vfs_path_element->dir.converter != str_cnv_from_term)
530 str_close_conv (vfs_path_element->dir.converter);
531 vfs_path_element->dir.converter = INVALID_CONV;
533 #endif
535 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
536 vfs_free_handle (handle);
537 vfs_path_element_free (vfs_path_element);
539 g_free (dirp);
540 return result;
543 /* --------------------------------------------------------------------------------------------- */
546 mc_stat (const vfs_path_t * vpath, struct stat *buf)
548 int result = -1;
549 const vfs_path_element_t *path_element;
551 if (vpath == NULL)
552 return -1;
554 path_element = vfs_path_get_by_index (vpath, -1);
556 if (vfs_path_element_valid (path_element))
558 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
559 if (result == -1)
560 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
563 return result;
566 /* --------------------------------------------------------------------------------------------- */
569 mc_lstat (const vfs_path_t * vpath, struct stat *buf)
571 int result = -1;
572 const vfs_path_element_t *path_element;
574 if (vpath == NULL)
575 return -1;
577 path_element = vfs_path_get_by_index (vpath, -1);
579 if (vfs_path_element_valid (path_element))
581 result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
582 if (result == -1)
583 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
586 return result;
589 /* --------------------------------------------------------------------------------------------- */
592 mc_fstat (int handle, struct stat *buf)
594 struct vfs_class *vfs;
595 int result;
597 if (handle == -1)
598 return -1;
600 vfs = vfs_class_find_by_handle (handle);
601 if (vfs == NULL)
602 return -1;
604 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
605 if (result == -1)
606 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
607 return result;
610 /* --------------------------------------------------------------------------------------------- */
612 vfs_path_t *
613 mc_getlocalcopy (const vfs_path_t * pathname_vpath)
615 vfs_path_t *result = NULL;
616 const vfs_path_element_t *path_element;
618 if (pathname_vpath == NULL)
619 return NULL;
621 path_element = vfs_path_get_by_index (pathname_vpath, -1);
623 if (vfs_path_element_valid (path_element))
625 result = path_element->class->getlocalcopy != NULL ?
626 path_element->class->getlocalcopy (pathname_vpath) :
627 mc_def_getlocalcopy (pathname_vpath);
628 if (result == NULL)
629 errno = vfs_ferrno (path_element->class);
631 return result;
634 /* --------------------------------------------------------------------------------------------- */
637 mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
638 gboolean has_changed)
640 int return_value = -1;
641 const vfs_path_element_t *path_element;
643 if (pathname_vpath == NULL)
644 return -1;
646 path_element = vfs_path_get_by_index (pathname_vpath, -1);
648 if (vfs_path_element_valid (path_element))
649 return_value = path_element->class->ungetlocalcopy != NULL ?
650 path_element->class->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
651 mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
653 return return_value;
656 /* --------------------------------------------------------------------------------------------- */
658 * VFS chdir.
660 * @param vpath VFS-path
662 * @return 0 on success, -1 on failure.
666 mc_chdir (const vfs_path_t * vpath)
668 struct vfs_class *old_vfs;
669 vfsid old_vfsid;
670 int result;
671 const vfs_path_element_t *path_element;
672 vfs_path_t *cd_vpath;
674 if (vpath == NULL)
675 return -1;
677 if (vpath->relative)
678 cd_vpath = vfs_path_to_absolute (vpath);
679 else
680 cd_vpath = vfs_path_clone (vpath);
682 path_element = vfs_path_get_by_index (cd_vpath, -1);
684 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
686 goto error_end;
689 result = (*path_element->class->chdir) (cd_vpath);
691 if (result == -1)
693 errno = vfs_ferrno (path_element->class);
694 goto error_end;
697 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
698 old_vfs = current_vfs;
700 /* Actually change directory */
701 vfs_set_raw_current_dir (cd_vpath);
703 current_vfs = path_element->class;
705 /* This function uses the new current_dir implicitly */
706 vfs_stamp_create (old_vfs, old_vfsid);
708 /* Sometimes we assume no trailing slash on cwd */
709 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
710 if (vfs_path_element_valid (path_element))
712 if (*path_element->path != '\0')
714 char *p;
716 p = strchr (path_element->path, 0) - 1;
717 if (*p == PATH_SEP && p > path_element->path)
718 *p = '\0';
721 #ifdef ENABLE_VFS_NET
723 struct vfs_s_super *super;
725 super = vfs_get_super_by_vpath (vpath);
726 if (super != NULL && super->path_element != NULL)
728 g_free (super->path_element->path);
729 super->path_element->path = g_strdup (path_element->path);
732 #endif /* ENABLE_VFS_NET */
735 return 0;
737 error_end:
738 vfs_path_free (cd_vpath);
739 return -1;
742 /* --------------------------------------------------------------------------------------------- */
744 off_t
745 mc_lseek (int fd, off_t offset, int whence)
747 struct vfs_class *vfs;
748 off_t result;
750 if (fd == -1)
751 return -1;
753 vfs = vfs_class_find_by_handle (fd);
754 if (vfs == NULL)
755 return -1;
757 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
758 if (result == -1)
759 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
760 return result;
763 /* --------------------------------------------------------------------------------------------- */
764 /* Following code heavily borrows from libiberty, mkstemps.c */
766 * Arguments:
767 * pname (output) - pointer to the name of the temp file (needs g_free).
768 * NULL if the function fails.
769 * prefix - part of the filename before the random part.
770 * Prepend $TMPDIR or /tmp if there are no path separators.
771 * suffix - if not NULL, part of the filename after the random part.
773 * Result:
774 * handle of the open file or -1 if couldn't open any.
778 mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
780 char *p1, *p2;
781 int fd;
783 if (strchr (prefix, PATH_SEP) != NULL)
784 p1 = g_strdup (prefix);
785 else
787 /* Add prefix first to find the position of XXXXXX */
788 p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
791 p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
792 g_free (p1);
794 fd = g_mkstemp (p2);
795 if (fd >= 0)
796 *pname_vpath = vfs_path_from_str (p2);
797 else
799 *pname_vpath = NULL;
800 fd = -1;
803 g_free (p2);
805 return fd;
808 /* --------------------------------------------------------------------------------------------- */
810 * Return the directory where mc should keep its temporary files.
811 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
812 * When called the first time, the directory is created if needed.
813 * The first call should be done early, since we are using fprintf()
814 * and not message() to report possible problems.
817 const char *
818 mc_tmpdir (void)
820 static char buffer[64];
821 static const char *tmpdir = NULL;
822 const char *sys_tmp;
823 struct passwd *pwd;
824 struct stat st;
825 const char *error = NULL;
827 /* Check if already correctly initialized */
828 if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
829 st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
830 return tmpdir;
832 sys_tmp = getenv ("TMPDIR");
833 if (!sys_tmp || sys_tmp[0] != '/')
835 sys_tmp = TMPDIR_DEFAULT;
838 pwd = getpwuid (getuid ());
840 if (pwd)
841 g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
842 else
843 g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
845 canonicalize_pathname (buffer);
847 if (lstat (buffer, &st) == 0)
849 /* Sanity check for existing directory */
850 if (!S_ISDIR (st.st_mode))
851 error = _("%s is not a directory\n");
852 else if (st.st_uid != getuid ())
853 error = _("Directory %s is not owned by you\n");
854 else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
855 error = _("Cannot set correct permissions for directory %s\n");
857 else
859 /* Need to create directory */
860 if (mkdir (buffer, S_IRWXU) != 0)
862 fprintf (stderr,
863 _("Cannot create temporary directory %s: %s\n"),
864 buffer, unix_error_string (errno));
865 error = "";
869 if (error != NULL)
871 int test_fd;
872 char *fallback_prefix;
873 gboolean fallback_ok = FALSE;
874 vfs_path_t *test_vpath;
876 if (*error)
877 fprintf (stderr, error, buffer);
879 /* Test if sys_tmp is suitable for temporary files */
880 fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
881 test_fd = mc_mkstemps (&test_vpath, fallback_prefix, NULL);
882 g_free (fallback_prefix);
883 if (test_fd != -1)
885 char *test_fn;
887 test_fn = vfs_path_to_str (test_vpath);
888 close (test_fd);
889 test_fd = open (test_fn, O_RDONLY);
890 g_free (test_fn);
891 if (test_fd != -1)
893 close (test_fd);
894 unlink (test_fn);
895 fallback_ok = TRUE;
899 if (fallback_ok)
901 fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
902 g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
903 error = NULL;
905 else
907 fprintf (stderr, _("Temporary files will not be created\n"));
908 g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
911 vfs_path_free (test_vpath);
912 fprintf (stderr, "%s\n", _("Press any key to continue..."));
913 getc (stdin);
916 tmpdir = buffer;
918 if (!error)
919 g_setenv ("MC_TMPDIR", tmpdir, TRUE);
921 return tmpdir;
924 /* --------------------------------------------------------------------------------------------- */