chmod: some optimization.
[midnight-commander.git] / lib / vfs / interface.c
bloba3f9f1a6ae8445795996e063bf59373cdf8bcf8f
1 /*
2 Virtual File System: interface functions
4 Copyright (C) 2011-2017
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
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 <sys/stat.h>
45 #include <unistd.h>
46 #include <dirent.h>
47 #include <pwd.h>
48 #include <grp.h>
50 #include "lib/global.h"
52 #include "lib/widget.h" /* message() */
53 #include "lib/strutil.h" /* str_crt_conv_from() */
54 #include "lib/util.h"
56 #include "vfs.h"
57 #include "utilvfs.h"
58 #include "path.h"
59 #include "gc.h"
60 #include "xdirentry.h"
62 /* TODO: move it to separate private .h */
63 extern GString *vfs_str_buffer;
64 extern vfs_class *current_vfs;
65 extern struct dirent *mc_readdir_result;
67 /*** global variables ****************************************************************************/
69 struct dirent *mc_readdir_result = NULL;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /*** file scope variables ************************************************************************/
77 /*** file scope functions ************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
80 static vfs_path_t *
81 mc_def_getlocalcopy (const vfs_path_t * filename_vpath)
83 vfs_path_t *tmp_vpath = NULL;
84 int fdin, fdout = -1;
85 ssize_t i;
86 char buffer[BUF_1K * 8];
87 struct stat mystat;
89 fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR);
90 if (fdin == -1)
91 goto fail;
93 fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath));
94 if (fdout == -1)
95 goto fail;
97 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
99 if (write (fdout, buffer, i) != i)
100 goto fail;
102 if (i == -1)
103 goto fail;
104 i = mc_close (fdin);
105 fdin = -1;
106 if (i == -1)
107 goto fail;
109 i = close (fdout);
110 fdout = -1;
111 if (i == -1)
112 goto fail;
114 if (mc_stat (filename_vpath, &mystat) != -1)
115 mc_chmod (tmp_vpath, mystat.st_mode);
117 return tmp_vpath;
119 fail:
120 vfs_path_free (tmp_vpath);
121 if (fdout != -1)
122 close (fdout);
123 if (fdin != -1)
124 mc_close (fdin);
125 return NULL;
128 /* --------------------------------------------------------------------------------------------- */
130 static int
131 mc_def_ungetlocalcopy (const vfs_path_t * filename_vpath,
132 const vfs_path_t * local_vpath, gboolean has_changed)
134 int fdin = -1, fdout = -1;
135 const char *local;
137 local = vfs_path_get_last_path_str (local_vpath);
139 if (has_changed)
141 char buffer[BUF_1K * 8];
142 ssize_t i;
144 if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL)
145 goto failed;
147 fdin = open (local, O_RDONLY);
148 if (fdin == -1)
149 goto failed;
150 fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC);
151 if (fdout == -1)
152 goto failed;
153 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
154 if (mc_write (fdout, buffer, (size_t) i) != i)
155 goto failed;
156 if (i == -1)
157 goto failed;
159 if (close (fdin) == -1)
161 fdin = -1;
162 goto failed;
164 fdin = -1;
165 if (mc_close (fdout) == -1)
167 fdout = -1;
168 goto failed;
171 unlink (local);
172 return 0;
174 failed:
175 message (D_ERROR, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath));
176 if (fdout != -1)
177 mc_close (fdout);
178 if (fdin != -1)
179 close (fdin);
180 unlink (local);
181 return -1;
184 /* --------------------------------------------------------------------------------------------- */
185 /*** public functions ****************************************************************************/
186 /* --------------------------------------------------------------------------------------------- */
189 mc_open (const vfs_path_t * vpath, int flags, ...)
191 int result = -1;
192 mode_t mode = 0;
193 const vfs_path_element_t *path_element;
195 if (vpath == NULL)
196 return -1;
198 /* Get the mode flag */
199 if (flags & O_CREAT)
201 va_list ap;
202 va_start (ap, flags);
203 /* We have to use PROMOTED_MODE_T instead of mode_t. Doing 'va_arg (ap, mode_t)'
204 * fails on systems where 'mode_t' is smaller than 'int' because of C's "default
205 * argument promotions". */
206 mode = va_arg (ap, PROMOTED_MODE_T);
207 va_end (ap);
210 path_element = vfs_path_get_by_index (vpath, -1);
211 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
213 void *info;
214 /* open must be supported */
215 info = path_element->class->open (vpath, flags, mode);
216 if (info == NULL)
217 errno = vfs_ferrno (path_element->class);
218 else
219 result = vfs_new_handle (path_element->class, info);
221 else
222 errno = -EOPNOTSUPP;
224 return result;
227 /* --------------------------------------------------------------------------------------------- */
229 /* *INDENT-OFF* */
231 #define MC_NAMEOP(name, inarg, callarg) \
232 int mc_##name inarg \
234 int result; \
235 const vfs_path_element_t *path_element; \
237 if (vpath == NULL) \
238 return -1; \
240 path_element = vfs_path_get_by_index (vpath, -1); \
241 if (!vfs_path_element_valid (path_element)) \
243 return -1; \
246 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
247 if (result == -1) \
248 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
249 return result; \
252 MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
253 MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
254 MC_NAMEOP (utime, (const vfs_path_t *vpath, mc_timesbuf_t * times), (vpath, times))
255 MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
256 MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
257 MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
258 MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
259 MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
261 /* *INDENT-ON* */
263 /* --------------------------------------------------------------------------------------------- */
266 mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
268 int result = -1;
270 if (vpath1 == NULL)
271 return -1;
273 if (vpath1 != NULL)
275 const vfs_path_element_t *path_element;
277 path_element = vfs_path_get_by_index (vpath2, -1);
278 if (vfs_path_element_valid (path_element))
280 result =
281 path_element->class->symlink != NULL ?
282 path_element->class->symlink (vpath1, vpath2) : -1;
284 if (result == -1)
285 errno =
286 path_element->class->symlink != NULL ?
287 vfs_ferrno (path_element->class) : E_NOTSUPP;
290 return result;
293 /* --------------------------------------------------------------------------------------------- */
295 /* *INDENT-OFF* */
297 #define MC_HANDLEOP(name) \
298 ssize_t mc_##name (int handle, C void *buf, size_t count) \
300 struct vfs_class *vfs; \
301 void *fsinfo = NULL; \
302 int result; \
303 if (handle == -1) \
304 return -1; \
305 vfs = vfs_class_find_by_handle (handle, &fsinfo); \
306 if (vfs == NULL) \
307 return -1; \
308 result = vfs->name != NULL ? vfs->name (fsinfo, buf, count) : -1; \
309 if (result == -1) \
310 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
311 return result; \
314 #define C
315 MC_HANDLEOP (read)
316 #undef C
317 #define C const
318 MC_HANDLEOP (write)
319 #undef C
321 /* --------------------------------------------------------------------------------------------- */
323 #define MC_RENAMEOP(name) \
324 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
326 int result; \
327 const vfs_path_element_t *path_element1; \
328 const vfs_path_element_t *path_element2; \
330 if (vpath1 == NULL || vpath2 == NULL) \
331 return -1; \
333 path_element1 = vfs_path_get_by_index (vpath1, (-1)); \
334 path_element2 = vfs_path_get_by_index (vpath2, (-1)); \
336 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
337 path_element1->class != path_element2->class) \
339 errno = EXDEV; \
340 return -1; \
343 result = path_element1->class->name != NULL \
344 ? path_element1->class->name (vpath1, vpath2) \
345 : -1; \
346 if (result == -1) \
347 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
348 return result; \
351 MC_RENAMEOP (link)
352 MC_RENAMEOP (rename)
354 /* *INDENT-ON* */
356 /* --------------------------------------------------------------------------------------------- */
359 mc_ctl (int handle, int ctlop, void *arg)
361 struct vfs_class *vfs;
362 void *fsinfo = NULL;
364 vfs = vfs_class_find_by_handle (handle, &fsinfo);
366 return (vfs == NULL || vfs->ctl == NULL) ? 0 : vfs->ctl (fsinfo, ctlop, arg);
369 /* --------------------------------------------------------------------------------------------- */
372 mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
374 int result = -1;
375 const vfs_path_element_t *path_element;
377 if (vpath == NULL)
378 vfs_die ("You don't want to pass NULL to mc_setctl.");
380 path_element = vfs_path_get_by_index (vpath, -1);
381 if (vfs_path_element_valid (path_element))
382 result =
383 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
384 ctlop, arg) : 0;
386 return result;
389 /* --------------------------------------------------------------------------------------------- */
392 mc_close (int handle)
394 struct vfs_class *vfs;
395 void *fsinfo = NULL;
396 int result;
398 if (handle == -1)
399 return -1;
401 vfs = vfs_class_find_by_handle (handle, &fsinfo);
402 if (vfs == NULL || fsinfo == NULL)
403 return -1;
405 if (handle < 3)
406 return close (handle);
408 if (!vfs->close)
409 vfs_die ("VFS must support close.\n");
410 result = (*vfs->close) (fsinfo);
411 vfs_free_handle (handle);
412 if (result == -1)
413 errno = vfs_ferrno (vfs);
415 return result;
418 /* --------------------------------------------------------------------------------------------- */
420 DIR *
421 mc_opendir (const vfs_path_t * vpath)
423 int handle, *handlep;
424 void *info;
425 vfs_path_element_t *path_element;
427 if (vpath == NULL)
428 return NULL;
430 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
432 if (!vfs_path_element_valid (path_element))
434 errno = E_NOTSUPP;
435 return NULL;
438 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
440 if (info == NULL)
442 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
443 return NULL;
446 path_element->dir.info = info;
448 #ifdef HAVE_CHARSET
449 path_element->dir.converter = (path_element->encoding != NULL) ?
450 str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
451 if (path_element->dir.converter == INVALID_CONV)
452 path_element->dir.converter = str_cnv_from_term;
453 #endif
455 handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
457 handlep = g_new (int, 1);
458 *handlep = handle;
459 return (DIR *) handlep;
462 /* --------------------------------------------------------------------------------------------- */
464 struct dirent *
465 mc_readdir (DIR * dirp)
467 int handle;
468 struct vfs_class *vfs;
469 void *fsinfo = NULL;
470 struct dirent *entry = NULL;
471 vfs_path_element_t *vfs_path_element;
473 if (!mc_readdir_result)
475 /* We can't just allocate struct dirent as (see man dirent.h)
476 * struct dirent has VERY nonnaive semantics of allocating
477 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
478 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
479 * heap corrupter. So, allocate longliving dirent with at least
480 * (MAXNAMLEN + 1) for d_name in it.
481 * Strictly saying resulting dirent is unusable as we don't adjust internal
482 * structures, holding dirent size. But we don't use it in libc infrastructure.
483 * TODO: to make simpler homemade dirent-alike structure.
485 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
488 if (!dirp)
490 errno = EFAULT;
491 return NULL;
493 handle = *(int *) dirp;
495 vfs = vfs_class_find_by_handle (handle, &fsinfo);
496 if (vfs == NULL || fsinfo == NULL)
497 return NULL;
499 vfs_path_element = (vfs_path_element_t *) fsinfo;
500 if (vfs->readdir)
502 entry = (*vfs->readdir) (vfs_path_element->dir.info);
503 if (entry == NULL)
504 return NULL;
506 g_string_set_size (vfs_str_buffer, 0);
507 #ifdef HAVE_CHARSET
508 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
509 #else
510 g_string_assign (vfs_str_buffer, entry->d_name);
511 #endif
512 mc_readdir_result->d_ino = entry->d_ino;
513 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
515 if (entry == NULL)
516 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
517 return (entry != NULL) ? mc_readdir_result : NULL;
520 /* --------------------------------------------------------------------------------------------- */
523 mc_closedir (DIR * dirp)
525 int handle;
526 struct vfs_class *vfs;
527 void *fsinfo = NULL;
528 int result = -1;
530 if (dirp == NULL)
531 return result;
533 handle = *(int *) dirp;
535 vfs = vfs_class_find_by_handle (handle, &fsinfo);
536 if (vfs != NULL && fsinfo != NULL)
538 vfs_path_element_t *vfs_path_element = (vfs_path_element_t *) fsinfo;
540 #ifdef HAVE_CHARSET
541 if (vfs_path_element->dir.converter != str_cnv_from_term)
543 str_close_conv (vfs_path_element->dir.converter);
544 vfs_path_element->dir.converter = INVALID_CONV;
546 #endif
548 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
549 vfs_free_handle (handle);
550 vfs_path_element_free (vfs_path_element);
552 g_free (dirp);
553 return result;
556 /* --------------------------------------------------------------------------------------------- */
559 mc_stat (const vfs_path_t * vpath, struct stat *buf)
561 int result = -1;
562 const vfs_path_element_t *path_element;
564 if (vpath == NULL)
565 return -1;
567 path_element = vfs_path_get_by_index (vpath, -1);
569 if (vfs_path_element_valid (path_element))
571 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
572 if (result == -1)
573 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
576 return result;
579 /* --------------------------------------------------------------------------------------------- */
582 mc_lstat (const vfs_path_t * vpath, struct stat *buf)
584 int result = -1;
585 const vfs_path_element_t *path_element;
587 if (vpath == NULL)
588 return -1;
590 path_element = vfs_path_get_by_index (vpath, -1);
592 if (vfs_path_element_valid (path_element))
594 result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
595 if (result == -1)
596 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
599 return result;
602 /* --------------------------------------------------------------------------------------------- */
605 mc_fstat (int handle, struct stat *buf)
607 struct vfs_class *vfs;
608 void *fsinfo = NULL;
609 int result;
611 if (handle == -1)
612 return -1;
614 vfs = vfs_class_find_by_handle (handle, &fsinfo);
615 if (vfs == NULL)
616 return -1;
618 result = vfs->fstat ? (*vfs->fstat) (fsinfo, buf) : -1;
619 if (result == -1)
620 errno = vfs->fstat ? vfs_ferrno (vfs) : E_NOTSUPP;
621 return result;
624 /* --------------------------------------------------------------------------------------------- */
626 vfs_path_t *
627 mc_getlocalcopy (const vfs_path_t * pathname_vpath)
629 vfs_path_t *result = NULL;
630 const vfs_path_element_t *path_element;
632 if (pathname_vpath == NULL)
633 return NULL;
635 path_element = vfs_path_get_by_index (pathname_vpath, -1);
637 if (vfs_path_element_valid (path_element))
639 result = path_element->class->getlocalcopy != NULL ?
640 path_element->class->getlocalcopy (pathname_vpath) :
641 mc_def_getlocalcopy (pathname_vpath);
642 if (result == NULL)
643 errno = vfs_ferrno (path_element->class);
645 return result;
648 /* --------------------------------------------------------------------------------------------- */
651 mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
652 gboolean has_changed)
654 int return_value = -1;
655 const vfs_path_element_t *path_element;
657 if (pathname_vpath == NULL)
658 return -1;
660 path_element = vfs_path_get_by_index (pathname_vpath, -1);
662 if (vfs_path_element_valid (path_element))
663 return_value = path_element->class->ungetlocalcopy != NULL ?
664 path_element->class->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
665 mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
667 return return_value;
670 /* --------------------------------------------------------------------------------------------- */
672 * VFS chdir.
674 * @param vpath VFS-path
676 * @return 0 on success, -1 on failure.
680 mc_chdir (const vfs_path_t * vpath)
682 struct vfs_class *old_vfs;
683 vfsid old_vfsid;
684 int result;
685 const vfs_path_element_t *path_element;
686 vfs_path_t *cd_vpath;
688 if (vpath == NULL)
689 return -1;
691 if (vpath->relative)
692 cd_vpath = vfs_path_to_absolute (vpath);
693 else
694 cd_vpath = vfs_path_clone (vpath);
696 path_element = vfs_path_get_by_index (cd_vpath, -1);
698 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
700 goto error_end;
703 result = (*path_element->class->chdir) (cd_vpath);
705 if (result == -1)
707 errno = vfs_ferrno (path_element->class);
708 goto error_end;
711 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
712 old_vfs = current_vfs;
714 /* Actually change directory */
715 vfs_set_raw_current_dir (cd_vpath);
717 current_vfs = path_element->class;
719 /* This function uses the new current_dir implicitly */
720 vfs_stamp_create (old_vfs, old_vfsid);
722 /* Sometimes we assume no trailing slash on cwd */
723 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
724 if (vfs_path_element_valid (path_element))
726 if (*path_element->path != '\0')
728 char *p;
730 p = strchr (path_element->path, 0) - 1;
731 if (IS_PATH_SEP (*p) && p > path_element->path)
732 *p = '\0';
735 #ifdef ENABLE_VFS_NET
737 struct vfs_s_super *super;
739 super = vfs_get_super_by_vpath (vpath);
740 if (super != NULL && super->path_element != NULL)
742 g_free (super->path_element->path);
743 super->path_element->path = g_strdup (path_element->path);
746 #endif /* ENABLE_VFS_NET */
749 return 0;
751 error_end:
752 vfs_path_free (cd_vpath);
753 return -1;
756 /* --------------------------------------------------------------------------------------------- */
758 off_t
759 mc_lseek (int fd, off_t offset, int whence)
761 struct vfs_class *vfs;
762 void *fsinfo = NULL;
763 off_t result;
765 if (fd == -1)
766 return -1;
768 vfs = vfs_class_find_by_handle (fd, &fsinfo);
769 if (vfs == NULL)
770 return -1;
772 result = vfs->lseek ? (*vfs->lseek) (fsinfo, offset, whence) : -1;
773 if (result == -1)
774 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
775 return result;
778 /* --------------------------------------------------------------------------------------------- */
779 /* Following code heavily borrows from libiberty, mkstemps.c */
781 * Arguments:
782 * pname (output) - pointer to the name of the temp file (needs g_free).
783 * NULL if the function fails.
784 * prefix - part of the filename before the random part.
785 * Prepend $TMPDIR or /tmp if there are no path separators.
786 * suffix - if not NULL, part of the filename after the random part.
788 * Result:
789 * handle of the open file or -1 if couldn't open any.
793 mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
795 char *p1, *p2;
796 int fd;
798 if (strchr (prefix, PATH_SEP) != NULL)
799 p1 = g_strdup (prefix);
800 else
802 /* Add prefix first to find the position of XXXXXX */
803 p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
806 p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
807 g_free (p1);
809 fd = g_mkstemp (p2);
810 if (fd >= 0)
811 *pname_vpath = vfs_path_from_str (p2);
812 else
814 *pname_vpath = NULL;
815 fd = -1;
818 g_free (p2);
820 return fd;
823 /* --------------------------------------------------------------------------------------------- */
825 * Return the directory where mc should keep its temporary files.
826 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
827 * When called the first time, the directory is created if needed.
828 * The first call should be done early, since we are using fprintf()
829 * and not message() to report possible problems.
832 const char *
833 mc_tmpdir (void)
835 static char buffer[PATH_MAX];
836 static const char *tmpdir = NULL;
837 const char *sys_tmp;
838 struct passwd *pwd;
839 struct stat st;
840 const char *error = NULL;
842 /* Check if already correctly initialized */
843 if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
844 st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
845 return tmpdir;
847 sys_tmp = getenv ("TMPDIR");
848 if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
849 sys_tmp = TMPDIR_DEFAULT;
851 pwd = getpwuid (getuid ());
853 if (pwd)
854 g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
855 else
856 g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
858 canonicalize_pathname (buffer);
860 /* Try to create directory */
861 if (mkdir (buffer, S_IRWXU) != 0)
863 if (errno == EEXIST && lstat (buffer, &st) == 0)
865 /* Sanity check for existing directory */
866 if (!S_ISDIR (st.st_mode))
867 error = _("%s is not a directory\n");
868 else if (st.st_uid != getuid ())
869 error = _("Directory %s is not owned by you\n");
870 else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
871 error = _("Cannot set correct permissions for directory %s\n");
873 else
875 fprintf (stderr,
876 _("Cannot create temporary directory %s: %s\n"),
877 buffer, unix_error_string (errno));
878 error = "";
882 if (error != NULL)
884 int test_fd;
885 char *fallback_prefix;
886 gboolean fallback_ok = FALSE;
887 vfs_path_t *test_vpath;
889 if (*error)
890 fprintf (stderr, error, buffer);
892 /* Test if sys_tmp is suitable for temporary files */
893 fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
894 test_fd = mc_mkstemps (&test_vpath, fallback_prefix, NULL);
895 g_free (fallback_prefix);
896 if (test_fd != -1)
898 close (test_fd);
899 test_fd = open (vfs_path_as_str (test_vpath), O_RDONLY);
900 if (test_fd != -1)
902 close (test_fd);
903 unlink (vfs_path_as_str (test_vpath));
904 fallback_ok = TRUE;
908 if (fallback_ok)
910 fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
911 g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
912 error = NULL;
914 else
916 fprintf (stderr, _("Temporary files will not be created\n"));
917 g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
920 vfs_path_free (test_vpath);
921 fprintf (stderr, "%s\n", _("Press any key to continue..."));
922 getc (stdin);
925 tmpdir = buffer;
927 if (!error)
928 g_setenv ("MC_TMPDIR", tmpdir, TRUE);
930 return tmpdir;
933 /* --------------------------------------------------------------------------------------------- */