Changed interface of functions mc_getlocalcopy() and mc_ungetlocalcopy()
[midnight-commander.git] / lib / vfs / interface.c
blob7944e6e051408768a8aafc5ef8e861a3904f991a
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"
62 extern GString *vfs_str_buffer;
63 extern struct vfs_class *current_vfs;
65 /*** global variables ****************************************************************************/
67 struct dirent *mc_readdir_result = NULL;
69 /*** file scope macro definitions ****************************************************************/
71 /*** file scope type declarations ****************************************************************/
73 /*** file scope variables ************************************************************************/
75 /*** file scope functions ************************************************************************/
76 /* --------------------------------------------------------------------------------------------- */
78 static vfs_path_t *
79 mc_def_getlocalcopy (const vfs_path_t * filename_vpath)
81 vfs_path_t *tmp_vpath = NULL;
82 int fdin = -1, fdout = -1;
83 ssize_t i;
84 char buffer[BUF_1K * 8];
85 struct stat mystat;
87 fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR);
88 if (fdin == -1)
89 goto fail;
91 fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath));
92 if (fdout == -1)
93 goto fail;
95 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
97 if (write (fdout, buffer, i) != i)
98 goto fail;
100 if (i == -1)
101 goto fail;
102 i = mc_close (fdin);
103 fdin = -1;
104 if (i == -1)
105 goto fail;
106 i = close (fdout);
107 fdout = -1;
108 if (i == -1)
110 fdout = -1;
111 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 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 mode = 0, result = -1;
192 vfs_path_element_t *path_element;
194 if (vpath == NULL)
195 return -1;
197 /* Get the mode flag */
198 if (flags & O_CREAT)
200 va_list ap;
201 va_start (ap, flags);
202 mode = va_arg (ap, int);
203 va_end (ap);
206 path_element = vfs_path_get_by_index (vpath, -1);
207 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
209 void *info;
210 /* open must be supported */
211 info = path_element->class->open (vpath, flags, mode);
212 if (info == NULL)
213 errno = vfs_ferrno (path_element->class);
214 else
215 result = vfs_new_handle (path_element->class, info);
217 else
218 errno = -EOPNOTSUPP;
220 return result;
223 /* --------------------------------------------------------------------------------------------- */
225 /* *INDENT-OFF* */
227 #define MC_NAMEOP(name, inarg, callarg) \
228 int mc_##name inarg \
230 int result; \
231 vfs_path_element_t *path_element; \
233 if (vpath == NULL) \
234 return -1; \
236 path_element = vfs_path_get_by_index (vpath, -1); \
237 if (!vfs_path_element_valid (path_element)) \
239 return -1; \
242 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
243 if (result == -1) \
244 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
245 return result; \
248 MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
249 MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
250 MC_NAMEOP (utime, (const vfs_path_t *vpath, struct utimbuf * times), (vpath, times))
251 MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
252 MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
253 MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
254 MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
255 MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
257 /* *INDENT-ON* */
259 /* --------------------------------------------------------------------------------------------- */
262 mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
264 int result = -1;
266 if (vpath1 == NULL)
267 return -1;
269 if (vpath1 != NULL)
271 vfs_path_element_t *path_element;
273 path_element = vfs_path_get_by_index (vpath2, -1);
274 if (vfs_path_element_valid (path_element))
276 result =
277 path_element->class->symlink != NULL ?
278 path_element->class->symlink (vpath1, vpath2) : -1;
280 if (result == -1)
281 errno =
282 path_element->class->symlink != NULL ?
283 vfs_ferrno (path_element->class) : E_NOTSUPP;
286 return result;
289 /* --------------------------------------------------------------------------------------------- */
291 /* *INDENT-OFF* */
293 #define MC_HANDLEOP(name, inarg, callarg) \
294 ssize_t mc_##name inarg \
296 struct vfs_class *vfs; \
297 int result; \
298 if (handle == -1) \
299 return -1; \
300 vfs = vfs_class_find_by_handle (handle); \
301 if (vfs == NULL) \
302 return -1; \
303 result = vfs->name != NULL ? vfs->name callarg : -1; \
304 if (result == -1) \
305 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
306 return result; \
309 MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_class_data_find_by_handle (handle), buffer, count))
310 MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data_find_by_handle (handle), buf, nbyte))
312 /* --------------------------------------------------------------------------------------------- */
314 #define MC_RENAMEOP(name) \
315 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
317 int result; \
318 vfs_path_element_t *path_element1, *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;
366 vfs_path_element_t *path_element;
368 if (vpath == NULL)
369 vfs_die ("You don't want to pass NULL to mc_setctl.");
371 path_element = vfs_path_get_by_index (vpath, -1);
372 if (vfs_path_element_valid (path_element))
373 result =
374 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
375 ctlop, arg) : 0;
377 return result;
380 /* --------------------------------------------------------------------------------------------- */
383 mc_close (int handle)
385 struct vfs_class *vfs;
386 int result;
388 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
389 return -1;
391 vfs = vfs_class_find_by_handle (handle);
392 if (vfs == NULL)
393 return -1;
395 if (handle < 3)
396 return close (handle);
398 if (!vfs->close)
399 vfs_die ("VFS must support close.\n");
400 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
401 vfs_free_handle (handle);
402 if (result == -1)
403 errno = vfs_ferrno (vfs);
405 return result;
408 /* --------------------------------------------------------------------------------------------- */
410 DIR *
411 mc_opendir (const vfs_path_t * vpath)
413 int handle, *handlep;
414 void *info;
415 vfs_path_element_t *path_element;
417 if (vpath == NULL)
418 return NULL;
420 path_element = vfs_path_get_by_index (vpath, -1);
422 if (!vfs_path_element_valid (path_element))
424 errno = E_NOTSUPP;
425 return NULL;
428 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
430 if (info == NULL)
432 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
433 return NULL;
436 path_element->dir.info = info;
438 path_element->dir.converter =
439 (path_element->encoding !=
440 NULL) ? 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 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 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 * Return current directory. If it's local, reread the current directory
603 * from the OS. Put directory to the provided buffer.
606 char *
607 mc_get_current_wd (char *buffer, size_t size)
609 char *cwd = _vfs_get_cwd ();
611 g_strlcpy (buffer, cwd, size);
612 g_free (cwd);
614 return buffer;
617 /* --------------------------------------------------------------------------------------------- */
619 vfs_path_t *
620 mc_getlocalcopy (const vfs_path_t * pathname_vpath)
622 vfs_path_t *result = NULL;
623 vfs_path_element_t *path_element;
625 if (pathname_vpath == NULL)
626 return NULL;
628 path_element = vfs_path_get_by_index (pathname_vpath, -1);
630 if (vfs_path_element_valid (path_element))
632 result = path_element->class->getlocalcopy != NULL ?
633 path_element->class-> getlocalcopy (pathname_vpath) :
634 mc_def_getlocalcopy (pathname_vpath);
635 if (result == NULL)
636 errno = vfs_ferrno (path_element->class);
638 return result;
641 /* --------------------------------------------------------------------------------------------- */
644 mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
645 gboolean has_changed)
647 int return_value = -1;
648 vfs_path_element_t *path_element;
650 if (pathname_vpath == NULL)
651 return -1;
653 path_element = vfs_path_get_by_index (pathname_vpath, -1);
655 if (vfs_path_element_valid (path_element))
656 return_value = path_element->class->ungetlocalcopy != NULL ?
657 path_element->class->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
658 mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
660 return return_value;
663 /* --------------------------------------------------------------------------------------------- */
665 * VFS chdir.
667 * @param vpath VFS-path
669 * @return 0 on success, -1 on failure.
673 mc_chdir (const vfs_path_t * vpath)
675 struct vfs_class *old_vfs;
676 vfsid old_vfsid;
677 int result;
678 vfs_path_element_t *path_element;
680 if (vpath == NULL)
681 return -1;
683 path_element = vfs_path_get_by_index (vpath, -1);
685 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
687 return -1;
690 result = (*path_element->class->chdir) (vpath);
692 if (result == -1)
694 errno = vfs_ferrno (path_element->class);
695 return -1;
698 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
699 old_vfs = current_vfs;
701 /* Actually change directory */
702 vfs_set_raw_current_dir (vfs_path_clone (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) && (*path_element->path != '\0'))
712 char *p;
713 p = strchr (path_element->path, 0) - 1;
714 if (*p == PATH_SEP && p > path_element->path)
715 *p = 0;
717 return 0;
720 /* --------------------------------------------------------------------------------------------- */
722 off_t
723 mc_lseek (int fd, off_t offset, int whence)
725 struct vfs_class *vfs;
726 off_t result;
728 if (fd == -1)
729 return -1;
731 vfs = vfs_class_find_by_handle (fd);
732 if (vfs == NULL)
733 return -1;
735 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
736 if (result == -1)
737 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
738 return result;
741 /* --------------------------------------------------------------------------------------------- */
742 /* Following code heavily borrows from libiberty, mkstemps.c */
744 * Arguments:
745 * pname (output) - pointer to the name of the temp file (needs g_free).
746 * NULL if the function fails.
747 * prefix - part of the filename before the random part.
748 * Prepend $TMPDIR or /tmp if there are no path separators.
749 * suffix - if not NULL, part of the filename after the random part.
751 * Result:
752 * handle of the open file or -1 if couldn't open any.
756 mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
758 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
759 static unsigned long value;
760 struct timeval tv;
761 char *tmpbase;
762 char *tmpname;
763 char *XXXXXX;
764 char *ret_path;
765 int count;
767 if (strchr (prefix, PATH_SEP) == NULL)
769 /* Add prefix first to find the position of XXXXXX */
770 tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL);
772 else
774 tmpbase = g_strdup (prefix);
777 tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL);
778 ret_path = tmpname;
779 XXXXXX = &tmpname[strlen (tmpbase)];
780 g_free (tmpbase);
782 /* Get some more or less random data. */
783 gettimeofday (&tv, NULL);
784 value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
786 for (count = 0; count < TMP_MAX; ++count)
788 unsigned long v = value;
789 int fd;
791 /* Fill in the random bits. */
792 XXXXXX[0] = letters[v % 62];
793 v /= 62;
794 XXXXXX[1] = letters[v % 62];
795 v /= 62;
796 XXXXXX[2] = letters[v % 62];
797 v /= 62;
798 XXXXXX[3] = letters[v % 62];
799 v /= 62;
800 XXXXXX[4] = letters[v % 62];
801 v /= 62;
802 XXXXXX[5] = letters[v % 62];
804 fd = open (tmpname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
805 if (fd >= 0)
807 /* Successfully created. */
808 *pname_vpath = vfs_path_from_str (ret_path);
809 g_free (ret_path);
810 return fd;
813 /* This is a random value. It is only necessary that the next
814 TMP_MAX values generated by adding 7777 to VALUE are different
815 with (module 2^32). */
816 value += 7777;
819 /* Unsuccessful. Free the filename. */
820 g_free (ret_path);
821 *pname_vpath = NULL;
823 return -1;
826 /* --------------------------------------------------------------------------------------------- */
828 * Return the directory where mc should keep its temporary files.
829 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
830 * When called the first time, the directory is created if needed.
831 * The first call should be done early, since we are using fprintf()
832 * and not message() to report possible problems.
835 const char *
836 mc_tmpdir (void)
838 static char buffer[64];
839 static const char *tmpdir = NULL;
840 const char *sys_tmp;
841 struct passwd *pwd;
842 struct stat st;
843 const char *error = NULL;
845 /* Check if already correctly initialized */
846 if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
847 st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
848 return tmpdir;
850 sys_tmp = getenv ("TMPDIR");
851 if (!sys_tmp || sys_tmp[0] != '/')
853 sys_tmp = TMPDIR_DEFAULT;
856 pwd = getpwuid (getuid ());
858 if (pwd)
859 g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
860 else
861 g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
863 canonicalize_pathname (buffer);
865 if (lstat (buffer, &st) == 0)
867 /* Sanity check for existing directory */
868 if (!S_ISDIR (st.st_mode))
869 error = _("%s is not a directory\n");
870 else if (st.st_uid != getuid ())
871 error = _("Directory %s is not owned by you\n");
872 else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
873 error = _("Cannot set correct permissions for directory %s\n");
875 else
877 /* Need to create directory */
878 if (mkdir (buffer, S_IRWXU) != 0)
880 fprintf (stderr,
881 _("Cannot create temporary directory %s: %s\n"),
882 buffer, unix_error_string (errno));
883 error = "";
887 if (error != NULL)
889 int test_fd;
890 char *fallback_prefix;
891 gboolean fallback_ok = FALSE;
892 vfs_path_t *test_vpath;
894 if (*error)
895 fprintf (stderr, error, buffer);
897 /* Test if sys_tmp is suitable for temporary files */
898 fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
899 test_fd = mc_mkstemps (&test_vpath, fallback_prefix, NULL);
900 g_free (fallback_prefix);
901 if (test_fd != -1)
903 char *test_fn;
905 test_fn = vfs_path_to_str (test_vpath);
906 close (test_fd);
907 test_fd = open (test_fn, O_RDONLY);
908 g_free (test_fn);
909 if (test_fd != -1)
911 close (test_fd);
912 unlink (test_fn);
913 fallback_ok = TRUE;
917 if (fallback_ok)
919 fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
920 g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
921 error = NULL;
923 else
925 fprintf (stderr, _("Temporary files will not be created\n"));
926 g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
929 vfs_path_free (test_vpath);
930 fprintf (stderr, "%s\n", _("Press any key to continue..."));
931 getc (stdin);
934 tmpdir = buffer;
936 if (!error)
937 g_setenv ("MC_TMPDIR", tmpdir, TRUE);
939 return tmpdir;
942 /* --------------------------------------------------------------------------------------------- */