Ticket #2779: Active VFS directories list contain incorrect current path
[midnight-commander.git] / lib / vfs / interface.c
blob02b6628b34d62c2324c47447946a9f1dec7aee09
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;
107 i = close (fdout);
108 fdout = -1;
109 if (i == -1)
111 fdout = -1;
112 goto fail;
115 if (mc_stat (filename_vpath, &mystat) != -1)
116 mc_chmod (tmp_vpath, mystat.st_mode);
118 return tmp_vpath;
120 fail:
121 vfs_path_free (tmp_vpath);
122 if (fdout != -1)
123 close (fdout);
124 if (fdin != -1)
125 mc_close (fdin);
126 return NULL;
129 /* --------------------------------------------------------------------------------------------- */
131 static int
132 mc_def_ungetlocalcopy (const vfs_path_t * filename_vpath,
133 const vfs_path_t * local_vpath, gboolean has_changed)
135 int fdin = -1, fdout = -1;
136 const char *local;
138 local = vfs_path_get_last_path_str (local_vpath);
140 if (has_changed)
142 char buffer[BUF_1K * 8];
143 ssize_t i;
145 if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL)
146 goto failed;
148 fdin = open (local, O_RDONLY);
149 if (fdin == -1)
150 goto failed;
151 fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC);
152 if (fdout == -1)
153 goto failed;
154 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
155 if (mc_write (fdout, buffer, (size_t) i) != i)
156 goto failed;
157 if (i == -1)
158 goto failed;
160 if (close (fdin) == -1)
162 fdin = -1;
163 goto failed;
165 fdin = -1;
166 if (mc_close (fdout) == -1)
168 fdout = -1;
169 goto failed;
172 unlink (local);
173 return 0;
175 failed:
176 message (D_ERROR, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath));
177 if (fdout != -1)
178 mc_close (fdout);
179 if (fdin != -1)
180 close (fdin);
181 unlink (local);
182 return -1;
185 /* --------------------------------------------------------------------------------------------- */
186 /*** public functions ****************************************************************************/
187 /* --------------------------------------------------------------------------------------------- */
190 mc_open (const vfs_path_t * vpath, int flags, ...)
192 int mode = 0, result = -1;
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, int);
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 path_element->dir.converter = (path_element->encoding != NULL) ?
440 str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
441 if (path_element->dir.converter == INVALID_CONV)
442 path_element->dir.converter = str_cnv_from_term;
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 estr_t state;
462 if (!mc_readdir_result)
464 /* We can't just allocate struct dirent as (see man dirent.h)
465 * struct dirent has VERY nonnaive semantics of allocating
466 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
467 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
468 * heap corrupter. So, allocate longliving dirent with at least
469 * (MAXNAMLEN + 1) for d_name in it.
470 * Strictly saying resulting dirent is unusable as we don't adjust internal
471 * structures, holding dirent size. But we don't use it in libc infrastructure.
472 * TODO: to make simpler homemade dirent-alike structure.
474 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
477 if (!dirp)
479 errno = EFAULT;
480 return NULL;
482 handle = *(int *) dirp;
484 vfs = vfs_class_find_by_handle (handle);
485 if (vfs == NULL)
486 return NULL;
488 vfs_path_element = vfs_class_data_find_by_handle (handle);
489 if (vfs->readdir)
491 entry = (*vfs->readdir) (vfs_path_element->dir.info);
492 if (entry == NULL)
493 return NULL;
494 g_string_set_size (vfs_str_buffer, 0);
495 state =
496 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
497 mc_readdir_result->d_ino = entry->d_ino;
498 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
500 if (entry == NULL)
501 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
502 return (entry != NULL) ? mc_readdir_result : NULL;
505 /* --------------------------------------------------------------------------------------------- */
508 mc_closedir (DIR * dirp)
510 int handle = *(int *) dirp;
511 struct vfs_class *vfs;
512 int result = -1;
514 vfs = vfs_class_find_by_handle (handle);
515 if (vfs != NULL)
517 vfs_path_element_t *vfs_path_element;
518 vfs_path_element = vfs_class_data_find_by_handle (handle);
519 if (vfs_path_element->dir.converter != str_cnv_from_term)
521 str_close_conv (vfs_path_element->dir.converter);
522 vfs_path_element->dir.converter = INVALID_CONV;
525 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
526 vfs_free_handle (handle);
527 vfs_path_element_free (vfs_path_element);
529 g_free (dirp);
530 return result;
533 /* --------------------------------------------------------------------------------------------- */
536 mc_stat (const vfs_path_t * vpath, struct stat *buf)
538 int result = -1;
539 const vfs_path_element_t *path_element;
541 if (vpath == NULL)
542 return -1;
544 path_element = vfs_path_get_by_index (vpath, -1);
546 if (vfs_path_element_valid (path_element))
548 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
549 if (result == -1)
550 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
553 return result;
556 /* --------------------------------------------------------------------------------------------- */
559 mc_lstat (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->lstat ? (*path_element->class->lstat) (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_fstat (int handle, struct stat *buf)
584 struct vfs_class *vfs;
585 int result;
587 if (handle == -1)
588 return -1;
590 vfs = vfs_class_find_by_handle (handle);
591 if (vfs == NULL)
592 return -1;
594 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
595 if (result == -1)
596 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
597 return result;
600 /* --------------------------------------------------------------------------------------------- */
602 vfs_path_t *
603 mc_getlocalcopy (const vfs_path_t * pathname_vpath)
605 vfs_path_t *result = NULL;
606 const vfs_path_element_t *path_element;
608 if (pathname_vpath == NULL)
609 return NULL;
611 path_element = vfs_path_get_by_index (pathname_vpath, -1);
613 if (vfs_path_element_valid (path_element))
615 result = path_element->class->getlocalcopy != NULL ?
616 path_element->class->getlocalcopy (pathname_vpath) :
617 mc_def_getlocalcopy (pathname_vpath);
618 if (result == NULL)
619 errno = vfs_ferrno (path_element->class);
621 return result;
624 /* --------------------------------------------------------------------------------------------- */
627 mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
628 gboolean has_changed)
630 int return_value = -1;
631 const vfs_path_element_t *path_element;
633 if (pathname_vpath == NULL)
634 return -1;
636 path_element = vfs_path_get_by_index (pathname_vpath, -1);
638 if (vfs_path_element_valid (path_element))
639 return_value = path_element->class->ungetlocalcopy != NULL ?
640 path_element->class->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
641 mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
643 return return_value;
646 /* --------------------------------------------------------------------------------------------- */
648 * VFS chdir.
650 * @param vpath VFS-path
652 * @return 0 on success, -1 on failure.
656 mc_chdir (const vfs_path_t * vpath)
658 struct vfs_class *old_vfs;
659 vfsid old_vfsid;
660 int result;
661 const vfs_path_element_t *path_element;
662 vfs_path_t *cd_vpath;
664 if (vpath == NULL)
665 return -1;
667 if (vpath->relative)
668 cd_vpath = vfs_path_to_absolute (vpath);
669 else
670 cd_vpath = vfs_path_clone (vpath);
672 path_element = vfs_path_get_by_index (cd_vpath, -1);
674 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
676 goto error_end;
679 result = (*path_element->class->chdir) (cd_vpath);
681 if (result == -1)
683 errno = vfs_ferrno (path_element->class);
684 goto error_end;
687 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
688 old_vfs = current_vfs;
690 /* Actually change directory */
691 vfs_set_raw_current_dir (cd_vpath);
693 current_vfs = path_element->class;
695 /* This function uses the new current_dir implicitly */
696 vfs_stamp_create (old_vfs, old_vfsid);
698 /* Sometimes we assume no trailing slash on cwd */
699 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
700 if (vfs_path_element_valid (path_element))
702 if (*path_element->path != '\0')
704 char *p;
706 p = strchr (path_element->path, 0) - 1;
707 if (p != NULL && *p == PATH_SEP && p != path_element->path)
708 *p = '\0';
710 #ifdef ENABLE_VFS_NET
712 struct vfs_s_subclass *subclass;
714 subclass = (struct vfs_s_subclass *) path_element->class->data;
715 if (subclass != NULL)
717 struct vfs_s_super *super = NULL;
719 (void) vfs_s_get_path (vpath, &super, 0);
720 if (super != NULL && super->path_element != NULL)
722 g_free (super->path_element->path);
723 super->path_element->path = g_strdup (path_element->path);
727 #endif /* ENABLE_VFS_NET */
730 return 0;
732 error_end:
733 vfs_path_free (cd_vpath);
734 return -1;
737 /* --------------------------------------------------------------------------------------------- */
739 off_t
740 mc_lseek (int fd, off_t offset, int whence)
742 struct vfs_class *vfs;
743 off_t result;
745 if (fd == -1)
746 return -1;
748 vfs = vfs_class_find_by_handle (fd);
749 if (vfs == NULL)
750 return -1;
752 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
753 if (result == -1)
754 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
755 return result;
758 /* --------------------------------------------------------------------------------------------- */
759 /* Following code heavily borrows from libiberty, mkstemps.c */
761 * Arguments:
762 * pname (output) - pointer to the name of the temp file (needs g_free).
763 * NULL if the function fails.
764 * prefix - part of the filename before the random part.
765 * Prepend $TMPDIR or /tmp if there are no path separators.
766 * suffix - if not NULL, part of the filename after the random part.
768 * Result:
769 * handle of the open file or -1 if couldn't open any.
773 mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
775 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
776 static unsigned long value;
777 struct timeval tv;
778 char *tmpbase;
779 char *tmpname;
780 char *XXXXXX;
781 char *ret_path;
782 int count;
784 if (strchr (prefix, PATH_SEP) == NULL)
786 /* Add prefix first to find the position of XXXXXX */
787 tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL);
789 else
791 tmpbase = g_strdup (prefix);
794 tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL);
795 ret_path = tmpname;
796 XXXXXX = &tmpname[strlen (tmpbase)];
797 g_free (tmpbase);
799 /* Get some more or less random data. */
800 gettimeofday (&tv, NULL);
801 value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
803 for (count = 0; count < TMP_MAX; ++count)
805 unsigned long v = value;
806 int fd;
808 /* Fill in the random bits. */
809 XXXXXX[0] = letters[v % 62];
810 v /= 62;
811 XXXXXX[1] = letters[v % 62];
812 v /= 62;
813 XXXXXX[2] = letters[v % 62];
814 v /= 62;
815 XXXXXX[3] = letters[v % 62];
816 v /= 62;
817 XXXXXX[4] = letters[v % 62];
818 v /= 62;
819 XXXXXX[5] = letters[v % 62];
821 fd = open (tmpname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
822 if (fd >= 0)
824 /* Successfully created. */
825 *pname_vpath = vfs_path_from_str (ret_path);
826 g_free (ret_path);
827 return fd;
830 /* This is a random value. It is only necessary that the next
831 TMP_MAX values generated by adding 7777 to VALUE are different
832 with (module 2^32). */
833 value += 7777;
836 /* Unsuccessful. Free the filename. */
837 g_free (ret_path);
838 *pname_vpath = NULL;
840 return -1;
843 /* --------------------------------------------------------------------------------------------- */
845 * Return the directory where mc should keep its temporary files.
846 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
847 * When called the first time, the directory is created if needed.
848 * The first call should be done early, since we are using fprintf()
849 * and not message() to report possible problems.
852 const char *
853 mc_tmpdir (void)
855 static char buffer[64];
856 static const char *tmpdir = NULL;
857 const char *sys_tmp;
858 struct passwd *pwd;
859 struct stat st;
860 const char *error = NULL;
862 /* Check if already correctly initialized */
863 if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
864 st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
865 return tmpdir;
867 sys_tmp = getenv ("TMPDIR");
868 if (!sys_tmp || sys_tmp[0] != '/')
870 sys_tmp = TMPDIR_DEFAULT;
873 pwd = getpwuid (getuid ());
875 if (pwd)
876 g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
877 else
878 g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
880 canonicalize_pathname (buffer);
882 if (lstat (buffer, &st) == 0)
884 /* Sanity check for existing directory */
885 if (!S_ISDIR (st.st_mode))
886 error = _("%s is not a directory\n");
887 else if (st.st_uid != getuid ())
888 error = _("Directory %s is not owned by you\n");
889 else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
890 error = _("Cannot set correct permissions for directory %s\n");
892 else
894 /* Need to create directory */
895 if (mkdir (buffer, S_IRWXU) != 0)
897 fprintf (stderr,
898 _("Cannot create temporary directory %s: %s\n"),
899 buffer, unix_error_string (errno));
900 error = "";
904 if (error != NULL)
906 int test_fd;
907 char *fallback_prefix;
908 gboolean fallback_ok = FALSE;
909 vfs_path_t *test_vpath;
911 if (*error)
912 fprintf (stderr, error, buffer);
914 /* Test if sys_tmp is suitable for temporary files */
915 fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
916 test_fd = mc_mkstemps (&test_vpath, fallback_prefix, NULL);
917 g_free (fallback_prefix);
918 if (test_fd != -1)
920 char *test_fn;
922 test_fn = vfs_path_to_str (test_vpath);
923 close (test_fd);
924 test_fd = open (test_fn, O_RDONLY);
925 g_free (test_fn);
926 if (test_fd != -1)
928 close (test_fd);
929 unlink (test_fn);
930 fallback_ok = TRUE;
934 if (fallback_ok)
936 fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
937 g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
938 error = NULL;
940 else
942 fprintf (stderr, _("Temporary files will not be created\n"));
943 g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
946 vfs_path_free (test_vpath);
947 fprintf (stderr, "%s\n", _("Press any key to continue..."));
948 getc (stdin);
951 tmpdir = buffer;
953 if (!error)
954 g_setenv ("MC_TMPDIR", tmpdir, TRUE);
956 return tmpdir;
959 /* --------------------------------------------------------------------------------------------- */