Moved functions mc_mkstemp() and mc_tempdir() to VFS module.
[midnight-commander.git] / lib / vfs / interface.c
blobf3e634541453012997e8c93cd2abc866a1120196
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 char *
79 mc_def_getlocalcopy (const char *filename)
81 char *tmp = NULL;
82 int fdin = -1, fdout = -1;
83 ssize_t i;
84 char buffer[BUF_1K * 8];
85 struct stat mystat;
86 vfs_path_t *vpath;
88 vpath = vfs_path_from_str (filename);
89 fdin = mc_open (vpath, O_RDONLY | O_LINEAR);
90 if (fdin == -1)
91 goto fail;
93 fdout = vfs_mkstemps (&tmp, "vfs", filename);
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;
108 if (close (fdout) == -1)
110 fdout = -1;
111 goto fail;
114 if (mc_stat (vpath, &mystat) != -1)
115 chmod (tmp, mystat.st_mode);
116 vfs_path_free (vpath);
118 return tmp;
120 fail:
121 vfs_path_free (vpath);
122 if (fdout != -1)
123 close (fdout);
124 if (fdin != -1)
125 mc_close (fdin);
126 g_free (tmp);
127 return NULL;
130 /* --------------------------------------------------------------------------------------------- */
132 static int
133 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
134 const char *local, int has_changed)
136 vfs_path_t *vpath;
137 int fdin = -1, fdout = -1;
139 vpath = vfs_path_from_str (filename);
141 if (has_changed)
143 char buffer[BUF_1K * 8];
144 ssize_t i;
146 if (!vfs->write)
147 goto failed;
149 fdin = open (local, O_RDONLY);
150 if (fdin == -1)
151 goto failed;
152 fdout = mc_open (vpath, O_WRONLY | O_TRUNC);
153 if (fdout == -1)
154 goto failed;
155 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
156 if (mc_write (fdout, buffer, (size_t) i) != i)
157 goto failed;
158 if (i == -1)
159 goto failed;
161 if (close (fdin) == -1)
163 fdin = -1;
164 goto failed;
166 fdin = -1;
167 if (mc_close (fdout) == -1)
169 fdout = -1;
170 goto failed;
173 unlink (local);
174 vfs_path_free (vpath);
175 return 0;
177 failed:
178 message (D_ERROR, _("Changes to file lost"), "%s", filename);
179 if (fdout != -1)
180 mc_close (fdout);
181 if (fdin != -1)
182 close (fdin);
183 unlink (local);
184 vfs_path_free (vpath);
185 return -1;
188 /* --------------------------------------------------------------------------------------------- */
189 /*** public functions ****************************************************************************/
190 /* --------------------------------------------------------------------------------------------- */
193 mc_open (const vfs_path_t * vpath, int flags, ...)
195 int mode = 0, result = -1;
196 vfs_path_element_t *path_element;
198 if (vpath == NULL)
199 return -1;
201 /* Get the mode flag */
202 if (flags & O_CREAT)
204 va_list ap;
205 va_start (ap, flags);
206 mode = va_arg (ap, int);
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 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, struct utimbuf * 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 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, inarg, callarg) \
298 ssize_t mc_##name inarg \
300 struct vfs_class *vfs; \
301 int result; \
302 if (handle == -1) \
303 return -1; \
304 vfs = vfs_class_find_by_handle (handle); \
305 if (vfs == NULL) \
306 return -1; \
307 result = vfs->name != NULL ? vfs->name callarg : -1; \
308 if (result == -1) \
309 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
310 return result; \
313 MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_class_data_find_by_handle (handle), buffer, count))
314 MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data_find_by_handle (handle), buf, nbyte))
316 /* --------------------------------------------------------------------------------------------- */
318 #define MC_RENAMEOP(name) \
319 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
321 int result; \
322 vfs_path_element_t *path_element1, *path_element2; \
324 if (vpath1 == NULL || vpath2 == NULL) \
325 return -1; \
327 path_element1 = vfs_path_get_by_index (vpath1, - 1); \
328 path_element2 = vfs_path_get_by_index (vpath2, - 1); \
330 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
331 path_element1->class != path_element2->class) \
333 errno = EXDEV; \
334 return -1; \
337 result = path_element1->class->name != NULL \
338 ? path_element1->class->name (vpath1, vpath2) \
339 : -1; \
340 if (result == -1) \
341 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
342 return result; \
345 MC_RENAMEOP (link)
346 MC_RENAMEOP (rename)
348 /* *INDENT-ON* */
350 /* --------------------------------------------------------------------------------------------- */
353 mc_ctl (int handle, int ctlop, void *arg)
355 struct vfs_class *vfs = vfs_class_find_by_handle (handle);
357 if (vfs == NULL)
358 return 0;
360 return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
363 /* --------------------------------------------------------------------------------------------- */
366 mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
368 int result = -1;
370 vfs_path_element_t *path_element;
372 if (vpath == NULL)
373 vfs_die ("You don't want to pass NULL to mc_setctl.");
375 path_element = vfs_path_get_by_index (vpath, -1);
376 if (vfs_path_element_valid (path_element))
377 result =
378 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
379 ctlop, arg) : 0;
381 return result;
384 /* --------------------------------------------------------------------------------------------- */
387 mc_close (int handle)
389 struct vfs_class *vfs;
390 int result;
392 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
393 return -1;
395 vfs = vfs_class_find_by_handle (handle);
396 if (vfs == NULL)
397 return -1;
399 if (handle < 3)
400 return close (handle);
402 if (!vfs->close)
403 vfs_die ("VFS must support close.\n");
404 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
405 vfs_free_handle (handle);
406 if (result == -1)
407 errno = vfs_ferrno (vfs);
409 return result;
412 /* --------------------------------------------------------------------------------------------- */
414 DIR *
415 mc_opendir (const vfs_path_t * vpath)
417 int handle, *handlep;
418 void *info;
419 vfs_path_element_t *path_element;
421 if (vpath == NULL)
422 return NULL;
424 path_element = vfs_path_get_by_index (vpath, -1);
426 if (!vfs_path_element_valid (path_element))
428 errno = E_NOTSUPP;
429 return NULL;
432 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
434 if (info == NULL)
436 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
437 return NULL;
440 path_element->dir.info = info;
442 path_element->dir.converter =
443 (path_element->encoding !=
444 NULL) ? str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
445 if (path_element->dir.converter == INVALID_CONV)
446 path_element->dir.converter = str_cnv_from_term;
448 handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
450 handlep = g_new (int, 1);
451 *handlep = handle;
452 return (DIR *) handlep;
455 /* --------------------------------------------------------------------------------------------- */
457 struct dirent *
458 mc_readdir (DIR * dirp)
460 int handle;
461 struct vfs_class *vfs;
462 struct dirent *entry = NULL;
463 vfs_path_element_t *vfs_path_element;
464 estr_t state;
466 if (!mc_readdir_result)
468 /* We can't just allocate struct dirent as (see man dirent.h)
469 * struct dirent has VERY nonnaive semantics of allocating
470 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
471 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
472 * heap corrupter. So, allocate longliving dirent with at least
473 * (MAXNAMLEN + 1) for d_name in it.
474 * Strictly saying resulting dirent is unusable as we don't adjust internal
475 * structures, holding dirent size. But we don't use it in libc infrastructure.
476 * TODO: to make simpler homemade dirent-alike structure.
478 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
481 if (!dirp)
483 errno = EFAULT;
484 return NULL;
486 handle = *(int *) dirp;
488 vfs = vfs_class_find_by_handle (handle);
489 if (vfs == NULL)
490 return NULL;
492 vfs_path_element = vfs_class_data_find_by_handle (handle);
493 if (vfs->readdir)
495 entry = (*vfs->readdir) (vfs_path_element->dir.info);
496 if (entry == NULL)
497 return NULL;
498 g_string_set_size (vfs_str_buffer, 0);
499 state =
500 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
501 mc_readdir_result->d_ino = entry->d_ino;
502 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
504 if (entry == NULL)
505 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
506 return (entry != NULL) ? mc_readdir_result : NULL;
509 /* --------------------------------------------------------------------------------------------- */
512 mc_closedir (DIR * dirp)
514 int handle = *(int *) dirp;
515 struct vfs_class *vfs;
516 int result = -1;
518 vfs = vfs_class_find_by_handle (handle);
519 if (vfs != NULL)
521 vfs_path_element_t *vfs_path_element;
522 vfs_path_element = vfs_class_data_find_by_handle (handle);
523 if (vfs_path_element->dir.converter != str_cnv_from_term)
525 str_close_conv (vfs_path_element->dir.converter);
526 vfs_path_element->dir.converter = INVALID_CONV;
529 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
530 vfs_free_handle (handle);
531 vfs_path_element_free (vfs_path_element);
533 g_free (dirp);
534 return result;
537 /* --------------------------------------------------------------------------------------------- */
540 mc_stat (const vfs_path_t * vpath, struct stat *buf)
542 int result = -1;
543 vfs_path_element_t *path_element;
545 if (vpath == NULL)
546 return -1;
548 path_element = vfs_path_get_by_index (vpath, -1);
550 if (vfs_path_element_valid (path_element))
552 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
553 if (result == -1)
554 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
557 return result;
560 /* --------------------------------------------------------------------------------------------- */
563 mc_lstat (const vfs_path_t * vpath, struct stat *buf)
565 int result = -1;
566 vfs_path_element_t *path_element;
568 if (vpath == NULL)
569 return -1;
571 path_element = vfs_path_get_by_index (vpath, -1);
573 if (vfs_path_element_valid (path_element))
575 result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
576 if (result == -1)
577 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
580 return result;
583 /* --------------------------------------------------------------------------------------------- */
586 mc_fstat (int handle, struct stat *buf)
588 struct vfs_class *vfs;
589 int result;
591 if (handle == -1)
592 return -1;
594 vfs = vfs_class_find_by_handle (handle);
595 if (vfs == NULL)
596 return -1;
598 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
599 if (result == -1)
600 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
601 return result;
604 /* --------------------------------------------------------------------------------------------- */
606 * Return current directory. If it's local, reread the current directory
607 * from the OS. Put directory to the provided buffer.
610 char *
611 mc_get_current_wd (char *buffer, size_t size)
613 char *cwd = _vfs_get_cwd ();
615 g_strlcpy (buffer, cwd, size);
616 g_free (cwd);
618 return buffer;
621 /* --------------------------------------------------------------------------------------------- */
623 char *
624 mc_getlocalcopy (const char *pathname)
626 char *result = NULL;
627 vfs_path_t *vpath;
628 vfs_path_element_t *path_element;
630 vpath = vfs_path_from_str (pathname);
631 if (vpath == NULL)
632 return NULL;
634 path_element = vfs_path_get_by_index (vpath, -1);
636 if (vfs_path_element_valid (path_element))
638 result = path_element->class->getlocalcopy != NULL ?
639 path_element->class->getlocalcopy (vpath) : mc_def_getlocalcopy (pathname);
640 if (result == NULL)
641 errno = vfs_ferrno (path_element->class);
643 vfs_path_free (vpath);
644 return result;
647 /* --------------------------------------------------------------------------------------------- */
650 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
652 int return_value = -1;
653 vfs_path_t *vpath;
654 vfs_path_element_t *path_element;
656 vpath = vfs_path_from_str (pathname);
657 if (vpath == NULL)
658 return -1;
660 path_element = vfs_path_get_by_index (vpath, -1);
662 if (vfs_path_element_valid (path_element))
664 return_value = path_element->class->ungetlocalcopy != NULL ?
665 path_element->class->ungetlocalcopy (vpath, local,
666 has_changed) :
667 mc_def_ungetlocalcopy (path_element->class, path_element->path, local, has_changed);
669 vfs_path_free (vpath);
670 return return_value;
673 /* --------------------------------------------------------------------------------------------- */
675 * VFS chdir.
677 * @param vpath VFS-path
679 * @return 0 on success, -1 on failure.
683 mc_chdir (const vfs_path_t * vpath)
685 struct vfs_class *old_vfs;
686 vfsid old_vfsid;
687 int result;
688 vfs_path_element_t *path_element;
690 if (vpath == NULL)
691 return -1;
693 path_element = vfs_path_get_by_index (vpath, -1);
695 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
697 return -1;
700 result = (*path_element->class->chdir) (vpath);
702 if (result == -1)
704 errno = vfs_ferrno (path_element->class);
705 return -1;
708 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
709 old_vfs = current_vfs;
711 /* Actually change directory */
712 vfs_set_raw_current_dir (vfs_path_clone (vpath));
713 current_vfs = path_element->class;
715 /* This function uses the new current_dir implicitly */
716 vfs_stamp_create (old_vfs, old_vfsid);
718 /* Sometimes we assume no trailing slash on cwd */
719 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
720 if (vfs_path_element_valid (path_element) && (*path_element->path != '\0'))
722 char *p;
723 p = strchr (path_element->path, 0) - 1;
724 if (*p == PATH_SEP && p > path_element->path)
725 *p = 0;
727 return 0;
730 /* --------------------------------------------------------------------------------------------- */
732 off_t
733 mc_lseek (int fd, off_t offset, int whence)
735 struct vfs_class *vfs;
736 off_t result;
738 if (fd == -1)
739 return -1;
741 vfs = vfs_class_find_by_handle (fd);
742 if (vfs == NULL)
743 return -1;
745 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
746 if (result == -1)
747 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
748 return result;
751 /* --------------------------------------------------------------------------------------------- */
752 /* Following code heavily borrows from libiberty, mkstemps.c */
754 * Arguments:
755 * pname (output) - pointer to the name of the temp file (needs g_free).
756 * NULL if the function fails.
757 * prefix - part of the filename before the random part.
758 * Prepend $TMPDIR or /tmp if there are no path separators.
759 * suffix - if not NULL, part of the filename after the random part.
761 * Result:
762 * handle of the open file or -1 if couldn't open any.
766 mc_mkstemps (char **pname, const char *prefix, const char *suffix)
768 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
769 static unsigned long value;
770 struct timeval tv;
771 char *tmpbase;
772 char *tmpname;
773 char *XXXXXX;
774 int count;
776 if (strchr (prefix, PATH_SEP) == NULL)
778 /* Add prefix first to find the position of XXXXXX */
779 tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL);
781 else
783 tmpbase = g_strdup (prefix);
786 tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL);
787 *pname = tmpname;
788 XXXXXX = &tmpname[strlen (tmpbase)];
789 g_free (tmpbase);
791 /* Get some more or less random data. */
792 gettimeofday (&tv, NULL);
793 value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
795 for (count = 0; count < TMP_MAX; ++count)
797 unsigned long v = value;
798 int fd;
800 /* Fill in the random bits. */
801 XXXXXX[0] = letters[v % 62];
802 v /= 62;
803 XXXXXX[1] = letters[v % 62];
804 v /= 62;
805 XXXXXX[2] = letters[v % 62];
806 v /= 62;
807 XXXXXX[3] = letters[v % 62];
808 v /= 62;
809 XXXXXX[4] = letters[v % 62];
810 v /= 62;
811 XXXXXX[5] = letters[v % 62];
813 fd = open (tmpname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
814 if (fd >= 0)
816 /* Successfully created. */
817 return fd;
820 /* This is a random value. It is only necessary that the next
821 TMP_MAX values generated by adding 7777 to VALUE are different
822 with (module 2^32). */
823 value += 7777;
826 /* Unsuccessful. Free the filename. */
827 g_free (tmpname);
828 *pname = NULL;
830 return -1;
833 /* --------------------------------------------------------------------------------------------- */
835 * Return the directory where mc should keep its temporary files.
836 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
837 * When called the first time, the directory is created if needed.
838 * The first call should be done early, since we are using fprintf()
839 * and not message() to report possible problems.
842 const char *
843 mc_tmpdir (void)
845 static char buffer[64];
846 static const char *tmpdir = NULL;
847 const char *sys_tmp;
848 struct passwd *pwd;
849 struct stat st;
850 const char *error = NULL;
852 /* Check if already correctly initialized */
853 if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
854 st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
855 return tmpdir;
857 sys_tmp = getenv ("TMPDIR");
858 if (!sys_tmp || sys_tmp[0] != '/')
860 sys_tmp = TMPDIR_DEFAULT;
863 pwd = getpwuid (getuid ());
865 if (pwd)
866 g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
867 else
868 g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
870 canonicalize_pathname (buffer);
872 if (lstat (buffer, &st) == 0)
874 /* Sanity check for existing directory */
875 if (!S_ISDIR (st.st_mode))
876 error = _("%s is not a directory\n");
877 else if (st.st_uid != getuid ())
878 error = _("Directory %s is not owned by you\n");
879 else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
880 error = _("Cannot set correct permissions for directory %s\n");
882 else
884 /* Need to create directory */
885 if (mkdir (buffer, S_IRWXU) != 0)
887 fprintf (stderr,
888 _("Cannot create temporary directory %s: %s\n"),
889 buffer, unix_error_string (errno));
890 error = "";
894 if (error != NULL)
896 int test_fd;
897 char *test_fn, *fallback_prefix;
898 int fallback_ok = 0;
900 if (*error)
901 fprintf (stderr, error, buffer);
903 /* Test if sys_tmp is suitable for temporary files */
904 fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
905 test_fd = mc_mkstemps (&test_fn, fallback_prefix, NULL);
906 g_free (fallback_prefix);
907 if (test_fd != -1)
909 close (test_fd);
910 test_fd = open (test_fn, O_RDONLY);
911 if (test_fd != -1)
913 close (test_fd);
914 unlink (test_fn);
915 fallback_ok = 1;
919 if (fallback_ok)
921 fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
922 g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
923 error = NULL;
925 else
927 fprintf (stderr, _("Temporary files will not be created\n"));
928 g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
931 fprintf (stderr, "%s\n", _("Press any key to continue..."));
932 getc (stdin);
935 tmpdir = buffer;
937 if (!error)
938 g_setenv ("MC_TMPDIR", tmpdir, TRUE);
940 return tmpdir;
943 /* --------------------------------------------------------------------------------------------- */