Update copyright years.
[midnight-commander.git] / lib / vfs / interface.c
blobed72aff69234dfc9a7120d93f0eea3693b48a4b8
1 /*
2 Virtual File System: interface functions
4 Copyright (C) 2011-2016
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 struct 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 mode = va_arg (ap, mode_t);
204 va_end (ap);
207 path_element = vfs_path_get_by_index (vpath, -1);
208 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
210 void *info;
211 /* open must be supported */
212 info = path_element->class->open (vpath, flags, mode);
213 if (info == NULL)
214 errno = vfs_ferrno (path_element->class);
215 else
216 result = vfs_new_handle (path_element->class, info);
218 else
219 errno = -EOPNOTSUPP;
221 return result;
224 /* --------------------------------------------------------------------------------------------- */
226 /* *INDENT-OFF* */
228 #define MC_NAMEOP(name, inarg, callarg) \
229 int mc_##name inarg \
231 int result; \
232 const vfs_path_element_t *path_element; \
234 if (vpath == NULL) \
235 return -1; \
237 path_element = vfs_path_get_by_index (vpath, -1); \
238 if (!vfs_path_element_valid (path_element)) \
240 return -1; \
243 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
244 if (result == -1) \
245 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
246 return result; \
249 MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
250 MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
251 MC_NAMEOP (utime, (const vfs_path_t *vpath, struct utimbuf * times), (vpath, times))
252 MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
253 MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
254 MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
255 MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
256 MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
258 /* *INDENT-ON* */
260 /* --------------------------------------------------------------------------------------------- */
263 mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
265 int result = -1;
267 if (vpath1 == NULL)
268 return -1;
270 if (vpath1 != NULL)
272 const vfs_path_element_t *path_element;
274 path_element = vfs_path_get_by_index (vpath2, -1);
275 if (vfs_path_element_valid (path_element))
277 result =
278 path_element->class->symlink != NULL ?
279 path_element->class->symlink (vpath1, vpath2) : -1;
281 if (result == -1)
282 errno =
283 path_element->class->symlink != NULL ?
284 vfs_ferrno (path_element->class) : E_NOTSUPP;
287 return result;
290 /* --------------------------------------------------------------------------------------------- */
292 /* *INDENT-OFF* */
294 #define MC_HANDLEOP(name, inarg, callarg) \
295 ssize_t mc_##name inarg \
297 struct vfs_class *vfs; \
298 int result; \
299 if (handle == -1) \
300 return -1; \
301 vfs = vfs_class_find_by_handle (handle); \
302 if (vfs == NULL) \
303 return -1; \
304 result = vfs->name != NULL ? vfs->name callarg : -1; \
305 if (result == -1) \
306 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
307 return result; \
310 MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_class_data_find_by_handle (handle), buffer, count))
311 MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data_find_by_handle (handle), buf, nbyte))
313 /* --------------------------------------------------------------------------------------------- */
315 #define MC_RENAMEOP(name) \
316 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
318 int result; \
319 const vfs_path_element_t *path_element1; \
320 const vfs_path_element_t *path_element2; \
322 if (vpath1 == NULL || vpath2 == NULL) \
323 return -1; \
325 path_element1 = vfs_path_get_by_index (vpath1, (-1)); \
326 path_element2 = vfs_path_get_by_index (vpath2, (-1)); \
328 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
329 path_element1->class != path_element2->class) \
331 errno = EXDEV; \
332 return -1; \
335 result = path_element1->class->name != NULL \
336 ? path_element1->class->name (vpath1, vpath2) \
337 : -1; \
338 if (result == -1) \
339 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
340 return result; \
343 MC_RENAMEOP (link)
344 MC_RENAMEOP (rename)
346 /* *INDENT-ON* */
348 /* --------------------------------------------------------------------------------------------- */
351 mc_ctl (int handle, int ctlop, void *arg)
353 struct vfs_class *vfs = vfs_class_find_by_handle (handle);
355 if (vfs == NULL)
356 return 0;
358 return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
361 /* --------------------------------------------------------------------------------------------- */
364 mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
366 int result = -1;
367 const vfs_path_element_t *path_element;
369 if (vpath == NULL)
370 vfs_die ("You don't want to pass NULL to mc_setctl.");
372 path_element = vfs_path_get_by_index (vpath, -1);
373 if (vfs_path_element_valid (path_element))
374 result =
375 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
376 ctlop, arg) : 0;
378 return result;
381 /* --------------------------------------------------------------------------------------------- */
384 mc_close (int handle)
386 struct vfs_class *vfs;
387 int result;
389 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
390 return -1;
392 vfs = vfs_class_find_by_handle (handle);
393 if (vfs == NULL)
394 return -1;
396 if (handle < 3)
397 return close (handle);
399 if (!vfs->close)
400 vfs_die ("VFS must support close.\n");
401 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
402 vfs_free_handle (handle);
403 if (result == -1)
404 errno = vfs_ferrno (vfs);
406 return result;
409 /* --------------------------------------------------------------------------------------------- */
411 DIR *
412 mc_opendir (const vfs_path_t * vpath)
414 int handle, *handlep;
415 void *info;
416 vfs_path_element_t *path_element;
418 if (vpath == NULL)
419 return NULL;
421 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
423 if (!vfs_path_element_valid (path_element))
425 errno = E_NOTSUPP;
426 return NULL;
429 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
431 if (info == NULL)
433 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
434 return NULL;
437 path_element->dir.info = info;
439 #ifdef HAVE_CHARSET
440 path_element->dir.converter = (path_element->encoding != NULL) ?
441 str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
442 if (path_element->dir.converter == INVALID_CONV)
443 path_element->dir.converter = str_cnv_from_term;
444 #endif
446 handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
448 handlep = g_new (int, 1);
449 *handlep = handle;
450 return (DIR *) handlep;
453 /* --------------------------------------------------------------------------------------------- */
455 struct dirent *
456 mc_readdir (DIR * dirp)
458 int handle;
459 struct vfs_class *vfs;
460 struct dirent *entry = NULL;
461 vfs_path_element_t *vfs_path_element;
463 if (!mc_readdir_result)
465 /* We can't just allocate struct dirent as (see man dirent.h)
466 * struct dirent has VERY nonnaive semantics of allocating
467 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
468 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
469 * heap corrupter. So, allocate longliving dirent with at least
470 * (MAXNAMLEN + 1) for d_name in it.
471 * Strictly saying resulting dirent is unusable as we don't adjust internal
472 * structures, holding dirent size. But we don't use it in libc infrastructure.
473 * TODO: to make simpler homemade dirent-alike structure.
475 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
478 if (!dirp)
480 errno = EFAULT;
481 return NULL;
483 handle = *(int *) dirp;
485 vfs = vfs_class_find_by_handle (handle);
486 if (vfs == NULL)
487 return NULL;
489 vfs_path_element = vfs_class_data_find_by_handle (handle);
490 if (vfs->readdir)
492 entry = (*vfs->readdir) (vfs_path_element->dir.info);
493 if (entry == NULL)
494 return NULL;
496 g_string_set_size (vfs_str_buffer, 0);
497 #ifdef HAVE_CHARSET
498 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
499 #else
500 g_string_assign (vfs_str_buffer, entry->d_name);
501 #endif
502 mc_readdir_result->d_ino = entry->d_ino;
503 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
505 if (entry == NULL)
506 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
507 return (entry != NULL) ? mc_readdir_result : NULL;
510 /* --------------------------------------------------------------------------------------------- */
513 mc_closedir (DIR * dirp)
515 int handle = *(int *) dirp;
516 struct vfs_class *vfs;
517 int result = -1;
519 vfs = vfs_class_find_by_handle (handle);
520 if (vfs != NULL)
522 vfs_path_element_t *vfs_path_element;
523 vfs_path_element = vfs_class_data_find_by_handle (handle);
525 #ifdef HAVE_CHARSET
526 if (vfs_path_element->dir.converter != str_cnv_from_term)
528 str_close_conv (vfs_path_element->dir.converter);
529 vfs_path_element->dir.converter = INVALID_CONV;
531 #endif
533 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
534 vfs_free_handle (handle);
535 vfs_path_element_free (vfs_path_element);
537 g_free (dirp);
538 return result;
541 /* --------------------------------------------------------------------------------------------- */
544 mc_stat (const vfs_path_t * vpath, struct stat *buf)
546 int result = -1;
547 const vfs_path_element_t *path_element;
549 if (vpath == NULL)
550 return -1;
552 path_element = vfs_path_get_by_index (vpath, -1);
554 if (vfs_path_element_valid (path_element))
556 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
557 if (result == -1)
558 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
561 return result;
564 /* --------------------------------------------------------------------------------------------- */
567 mc_lstat (const vfs_path_t * vpath, struct stat *buf)
569 int result = -1;
570 const vfs_path_element_t *path_element;
572 if (vpath == NULL)
573 return -1;
575 path_element = vfs_path_get_by_index (vpath, -1);
577 if (vfs_path_element_valid (path_element))
579 result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
580 if (result == -1)
581 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
584 return result;
587 /* --------------------------------------------------------------------------------------------- */
590 mc_fstat (int handle, struct stat *buf)
592 struct vfs_class *vfs;
593 int result;
595 if (handle == -1)
596 return -1;
598 vfs = vfs_class_find_by_handle (handle);
599 if (vfs == NULL)
600 return -1;
602 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
603 if (result == -1)
604 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
605 return result;
608 /* --------------------------------------------------------------------------------------------- */
610 vfs_path_t *
611 mc_getlocalcopy (const vfs_path_t * pathname_vpath)
613 vfs_path_t *result = NULL;
614 const vfs_path_element_t *path_element;
616 if (pathname_vpath == NULL)
617 return NULL;
619 path_element = vfs_path_get_by_index (pathname_vpath, -1);
621 if (vfs_path_element_valid (path_element))
623 result = path_element->class->getlocalcopy != NULL ?
624 path_element->class->getlocalcopy (pathname_vpath) :
625 mc_def_getlocalcopy (pathname_vpath);
626 if (result == NULL)
627 errno = vfs_ferrno (path_element->class);
629 return result;
632 /* --------------------------------------------------------------------------------------------- */
635 mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
636 gboolean has_changed)
638 int return_value = -1;
639 const vfs_path_element_t *path_element;
641 if (pathname_vpath == NULL)
642 return -1;
644 path_element = vfs_path_get_by_index (pathname_vpath, -1);
646 if (vfs_path_element_valid (path_element))
647 return_value = path_element->class->ungetlocalcopy != NULL ?
648 path_element->class->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
649 mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
651 return return_value;
654 /* --------------------------------------------------------------------------------------------- */
656 * VFS chdir.
658 * @param vpath VFS-path
660 * @return 0 on success, -1 on failure.
664 mc_chdir (const vfs_path_t * vpath)
666 struct vfs_class *old_vfs;
667 vfsid old_vfsid;
668 int result;
669 const vfs_path_element_t *path_element;
670 vfs_path_t *cd_vpath;
672 if (vpath == NULL)
673 return -1;
675 if (vpath->relative)
676 cd_vpath = vfs_path_to_absolute (vpath);
677 else
678 cd_vpath = vfs_path_clone (vpath);
680 path_element = vfs_path_get_by_index (cd_vpath, -1);
682 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
684 goto error_end;
687 result = (*path_element->class->chdir) (cd_vpath);
689 if (result == -1)
691 errno = vfs_ferrno (path_element->class);
692 goto error_end;
695 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
696 old_vfs = current_vfs;
698 /* Actually change directory */
699 vfs_set_raw_current_dir (cd_vpath);
701 current_vfs = path_element->class;
703 /* This function uses the new current_dir implicitly */
704 vfs_stamp_create (old_vfs, old_vfsid);
706 /* Sometimes we assume no trailing slash on cwd */
707 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
708 if (vfs_path_element_valid (path_element))
710 if (*path_element->path != '\0')
712 char *p;
714 p = strchr (path_element->path, 0) - 1;
715 if (IS_PATH_SEP (*p) && p > path_element->path)
716 *p = '\0';
719 #ifdef ENABLE_VFS_NET
721 struct vfs_s_super *super;
723 super = vfs_get_super_by_vpath (vpath);
724 if (super != NULL && super->path_element != NULL)
726 g_free (super->path_element->path);
727 super->path_element->path = g_strdup (path_element->path);
730 #endif /* ENABLE_VFS_NET */
733 return 0;
735 error_end:
736 vfs_path_free (cd_vpath);
737 return -1;
740 /* --------------------------------------------------------------------------------------------- */
742 off_t
743 mc_lseek (int fd, off_t offset, int whence)
745 struct vfs_class *vfs;
746 off_t result;
748 if (fd == -1)
749 return -1;
751 vfs = vfs_class_find_by_handle (fd);
752 if (vfs == NULL)
753 return -1;
755 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
756 if (result == -1)
757 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
758 return result;
761 /* --------------------------------------------------------------------------------------------- */
762 /* Following code heavily borrows from libiberty, mkstemps.c */
764 * Arguments:
765 * pname (output) - pointer to the name of the temp file (needs g_free).
766 * NULL if the function fails.
767 * prefix - part of the filename before the random part.
768 * Prepend $TMPDIR or /tmp if there are no path separators.
769 * suffix - if not NULL, part of the filename after the random part.
771 * Result:
772 * handle of the open file or -1 if couldn't open any.
776 mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
778 char *p1, *p2;
779 int fd;
781 if (strchr (prefix, PATH_SEP) != NULL)
782 p1 = g_strdup (prefix);
783 else
785 /* Add prefix first to find the position of XXXXXX */
786 p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
789 p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
790 g_free (p1);
792 fd = g_mkstemp (p2);
793 if (fd >= 0)
794 *pname_vpath = vfs_path_from_str (p2);
795 else
797 *pname_vpath = NULL;
798 fd = -1;
801 g_free (p2);
803 return fd;
806 /* --------------------------------------------------------------------------------------------- */
808 * Return the directory where mc should keep its temporary files.
809 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
810 * When called the first time, the directory is created if needed.
811 * The first call should be done early, since we are using fprintf()
812 * and not message() to report possible problems.
815 const char *
816 mc_tmpdir (void)
818 static char buffer[64];
819 static const char *tmpdir = NULL;
820 const char *sys_tmp;
821 struct passwd *pwd;
822 struct stat st;
823 const char *error = NULL;
825 /* Check if already correctly initialized */
826 if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
827 st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
828 return tmpdir;
830 sys_tmp = getenv ("TMPDIR");
831 if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
832 sys_tmp = TMPDIR_DEFAULT;
834 pwd = getpwuid (getuid ());
836 if (pwd)
837 g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
838 else
839 g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
841 canonicalize_pathname (buffer);
843 /* Try to create directory */
844 if (mkdir (buffer, S_IRWXU) != 0)
846 if (errno == EEXIST && lstat (buffer, &st) == 0)
848 /* Sanity check for existing directory */
849 if (!S_ISDIR (st.st_mode))
850 error = _("%s is not a directory\n");
851 else if (st.st_uid != getuid ())
852 error = _("Directory %s is not owned by you\n");
853 else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
854 error = _("Cannot set correct permissions for directory %s\n");
856 else
858 fprintf (stderr,
859 _("Cannot create temporary directory %s: %s\n"),
860 buffer, unix_error_string (errno));
861 error = "";
865 if (error != NULL)
867 int test_fd;
868 char *fallback_prefix;
869 gboolean fallback_ok = FALSE;
870 vfs_path_t *test_vpath;
872 if (*error)
873 fprintf (stderr, error, buffer);
875 /* Test if sys_tmp is suitable for temporary files */
876 fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
877 test_fd = mc_mkstemps (&test_vpath, fallback_prefix, NULL);
878 g_free (fallback_prefix);
879 if (test_fd != -1)
881 close (test_fd);
882 test_fd = open (vfs_path_as_str (test_vpath), O_RDONLY);
883 if (test_fd != -1)
885 close (test_fd);
886 unlink (vfs_path_as_str (test_vpath));
887 fallback_ok = TRUE;
891 if (fallback_ok)
893 fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
894 g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
895 error = NULL;
897 else
899 fprintf (stderr, _("Temporary files will not be created\n"));
900 g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
903 vfs_path_free (test_vpath);
904 fprintf (stderr, "%s\n", _("Press any key to continue..."));
905 getc (stdin);
908 tmpdir = buffer;
910 if (!error)
911 g_setenv ("MC_TMPDIR", tmpdir, TRUE);
913 return tmpdir;
916 /* --------------------------------------------------------------------------------------------- */