2 This file is part of GNUnet.
3 Copyright (C) 2001--2013, 2016, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief disk IO convenience methods
23 * @author Christian Grothoff
28 #include "gnunet_strings_lib.h"
29 #include "gnunet_disk_lib.h"
31 #define LOG(kind, ...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__)
33 #define LOG_STRERROR(kind, syscall) \
34 GNUNET_log_from_strerror (kind, "util-disk", syscall)
36 #define LOG_STRERROR_FILE(kind, syscall, filename) \
37 GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)
40 * Block size for IO for copying files.
42 #define COPY_BLK_SIZE 65536
44 #include <sys/types.h>
49 #include <sys/param.h>
52 #include <sys/mount.h>
54 #if HAVE_SYS_STATVFS_H
55 #include <sys/statvfs.h>
59 #define _IFMT 0170000 /* type of file */
60 #define _IFLNK 0120000 /* symbolic link */
61 #define S_ISLNK(m) (((m) & _IFMT) == _IFLNK)
66 * Handle used to manage a pipe.
68 struct GNUNET_DISK_PipeHandle
71 * File descriptors for the pipe.
72 * One or both of them could be NULL.
74 struct GNUNET_DISK_FileHandle *fd[2];
79 * Closure for the recursion to determine the file size
82 struct GetFileSizeData
85 * Set to the total file size.
90 * GNUNET_YES if symbolic links should be included.
92 int include_sym_links;
95 * GNUNET_YES if mode is file-only (return total == -1 for directories).
102 * Translate GNUnet-internal permission bitmap to UNIX file
103 * access permission bitmap.
105 * @param perm file permissions, GNUnet style
106 * @return file permissions, UNIX style
109 translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
114 if (perm & GNUNET_DISK_PERM_USER_READ)
116 if (perm & GNUNET_DISK_PERM_USER_WRITE)
118 if (perm & GNUNET_DISK_PERM_USER_EXEC)
120 if (perm & GNUNET_DISK_PERM_GROUP_READ)
122 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
124 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
126 if (perm & GNUNET_DISK_PERM_OTHER_READ)
128 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
130 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
138 * Iterate over all files in the given directory and
139 * accumulate their size.
141 * @param cls closure of type `struct GetFileSizeData`
142 * @param fn current filename we are looking at
143 * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK
146 getSizeRec (void *cls, const char *fn)
148 struct GetFileSizeData *gfsd = cls;
150 #if defined(HAVE_STAT64) && \
151 ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
154 if (0 != stat64 (fn, &buf))
156 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat64", fn);
157 return GNUNET_SYSERR;
162 if (0 != stat (fn, &buf))
164 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn);
165 return GNUNET_SYSERR;
168 if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
171 return GNUNET_SYSERR;
173 if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
174 gfsd->total += buf.st_size;
175 if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) &&
176 ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
178 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
179 return GNUNET_SYSERR;
186 * Checks whether a handle is invalid
188 * @param h handle to check
189 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
192 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
194 return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
199 * Get the size of an open file.
201 * @param fh open file handle
202 * @param size where to write size of the file
203 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
206 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size)
210 if (0 != fstat (fh->fd, &sbuf))
211 return GNUNET_SYSERR;
212 *size = sbuf.st_size;
218 * Move the read/write pointer in a file
220 * @param h handle of an open file
221 * @param offset position to move to
222 * @param whence specification to which position the offset parameter relates to
223 * @return the new position on success, #GNUNET_SYSERR otherwise
226 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
228 enum GNUNET_DISK_Seek whence)
233 return GNUNET_SYSERR;
236 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
238 return lseek (h->fd, offset, t[whence]);
243 * Get the size of the file (or directory) of the given file (in
246 * @param filename name of the file or directory
247 * @param size set to the size of the file (or,
248 * in the case of directories, the sum
249 * of all sizes of files in the directory)
250 * @param include_symbolic_links should symbolic links be
252 * @param single_file_mode #GNUNET_YES to only get size of one file
253 * and return #GNUNET_SYSERR for directories.
254 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
257 GNUNET_DISK_file_size (const char *filename,
259 int include_symbolic_links,
260 int single_file_mode)
262 struct GetFileSizeData gfsd;
265 GNUNET_assert (size != NULL);
267 gfsd.include_sym_links = include_symbolic_links;
268 gfsd.single_file_mode = single_file_mode;
269 ret = getSizeRec (&gfsd, filename);
276 * Obtain some unique identifiers for the given file
277 * that can be used to identify it in the local system.
278 * This function is used between GNUnet processes to
279 * quickly check if two files with the same absolute path
280 * are actually identical. The two processes represent
281 * the same peer but may communicate over the network
282 * (and the file may be on an NFS volume). This function
283 * may not be supported on all operating systems.
285 * @param filename name of the file
286 * @param dev set to the device ID
287 * @param ino set to the inode ID
288 * @return #GNUNET_OK on success
291 GNUNET_DISK_file_get_identifiers (const char *filename,
299 if (0 != stat (filename, &sbuf))
301 return GNUNET_SYSERR;
303 *ino = (uint64_t) sbuf.st_ino;
312 if (0 != statvfs (filename, &fbuf))
314 return GNUNET_SYSERR;
316 *dev = (uint64_t) fbuf.f_fsid;
322 if (0 != statfs (filename, &fbuf))
324 return GNUNET_SYSERR;
327 ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
337 * Create the name for a temporary file or directory from a template.
339 * @param t template (without XXXXX or "/tmp/")
340 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error
343 mktemp_name (const char *t)
349 if ((t[0] != '/') && (t[0] != '\\'))
351 /* FIXME: This uses system codepage on W32, not UTF-8 */
352 tmpdir = getenv ("TMPDIR");
354 tmpdir = getenv ("TMP");
356 tmpdir = getenv ("TEMP");
359 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
363 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
371 * Update POSIX permissions mask of a file on disk. If both argumets
372 * are #GNUNET_NO, the file is made world-read-write-executable (777).
374 * @param fn name of the file to update
375 * @param require_uid_match #GNUNET_YES means 700
376 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
379 GNUNET_DISK_fix_permissions (const char *fn,
380 int require_uid_match,
381 int require_gid_match)
385 if (GNUNET_YES == require_uid_match)
386 mode = S_IRUSR | S_IWUSR | S_IXUSR;
387 else if (GNUNET_YES == require_gid_match)
388 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
390 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
392 if (0 != chmod (fn, mode))
393 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn);
398 * Create an (empty) temporary directory on disk. If the given name is not
399 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
400 * 6 random characters will be appended to the name to create a unique
403 * @param t component to use for the name;
404 * does NOT contain "XXXXXX" or "/tmp/".
405 * @return NULL on error, otherwise name of fresh
406 * file on disk in directory for temporary files
409 GNUNET_DISK_mkdtemp (const char *t)
414 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
415 fn = mktemp_name (t);
416 if (fn != mkdtemp (fn))
418 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn);
429 * Move a file out of the way (create a backup) by
430 * renaming it to "orig.NUM~" where NUM is the smallest
431 * number that is not used yet.
433 * @param fil name of the file to back up
436 GNUNET_DISK_file_backup (const char *fil)
442 slen = strlen (fil) + 20;
443 target = GNUNET_malloc (slen);
447 GNUNET_snprintf (target, slen, "%s.%u~", fil, num++);
449 while (0 == access (target, F_OK));
450 if (0 != rename (fil, target))
451 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "rename", fil);
452 GNUNET_free (target);
457 * Create an (empty) temporary file on disk. If the given name is not
458 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
459 * 6 random characters will be appended to the name to create a unique
462 * @param t component to use for the name;
463 * does NOT contain "XXXXXX" or "/tmp/".
464 * @return NULL on error, otherwise name of fresh
465 * file on disk in directory for temporary files
468 GNUNET_DISK_mktemp (const char *t)
474 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
475 fn = mktemp_name (t);
476 if (-1 == (fd = mkstemp (fn)))
478 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
485 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
491 * Test if @a fil is a directory and listable. Optionally, also check if the
492 * directory is readable. Will not print an error message if the directory does
493 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
494 * with the same name).
496 * @param fil filename to test
497 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
498 * #GNUNET_NO to disable this check
499 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
500 * does not exist or stat'ed
503 GNUNET_DISK_directory_test (const char *fil, int is_readable)
505 struct stat filestat;
508 ret = stat (fil, &filestat);
512 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
513 return GNUNET_SYSERR;
515 if (! S_ISDIR (filestat.st_mode))
517 LOG (GNUNET_ERROR_TYPE_INFO,
518 "A file already exits with the same name %s\n",
522 if (GNUNET_YES == is_readable)
523 ret = access (fil, R_OK | X_OK);
525 ret = access (fil, X_OK);
528 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
536 * Check that fil corresponds to a filename
537 * (of a file that exists and that is not a directory).
539 * @param fil filename to check
540 * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something
541 * else (will print an error message in that case, too).
544 GNUNET_DISK_file_test (const char *fil)
546 struct stat filestat;
550 rdir = GNUNET_STRINGS_filename_expand (fil);
552 return GNUNET_SYSERR;
554 ret = stat (rdir, &filestat);
559 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
561 return GNUNET_SYSERR;
566 if (! S_ISREG (filestat.st_mode))
571 if (access (rdir, F_OK) < 0)
573 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
575 return GNUNET_SYSERR;
583 * Implementation of "mkdir -p"
585 * @param dir the directory to create
586 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
589 GNUNET_DISK_directory_create (const char *dir)
597 rdir = GNUNET_STRINGS_filename_expand (dir);
601 return GNUNET_SYSERR;
606 pos = 1; /* skip heading '/' */
608 /* Check which low level directories already exist */
610 rdir[len] = DIR_SEPARATOR;
613 if (DIR_SEPARATOR == rdir[pos2])
616 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
617 if (GNUNET_NO == ret)
619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
620 "Creating directory `%s' failed",
623 return GNUNET_SYSERR;
625 rdir[pos2] = DIR_SEPARATOR;
626 if (GNUNET_YES == ret)
637 /* Start creating directories */
640 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
643 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
644 if (GNUNET_NO == ret)
646 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
647 "Creating directory `%s' failed",
650 return GNUNET_SYSERR;
652 if (GNUNET_SYSERR == ret)
655 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
656 | S_IXOTH); /* 755 */
658 if ((ret != 0) && (errno != EEXIST))
660 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
662 return GNUNET_SYSERR;
665 rdir[pos] = DIR_SEPARATOR;
675 * Create the directory structure for storing a file.
677 * @param filename name of a file in the directory
678 * @returns #GNUNET_OK on success,
679 * #GNUNET_SYSERR on failure,
680 * #GNUNET_NO if the directory
681 * exists but is not writeable for us
684 GNUNET_DISK_directory_create_for_file (const char *filename)
691 rdir = GNUNET_STRINGS_filename_expand (filename);
695 return GNUNET_SYSERR;
697 if (0 == access (rdir, W_OK))
704 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
707 /* The empty path is invalid and in this case refers to / */
711 rdir = GNUNET_strdup ("/");
713 ret = GNUNET_DISK_directory_create (rdir);
714 if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK)))
724 * Read the contents of a binary file into a buffer.
726 * @param h handle to an open file
727 * @param result the buffer to write the result to
728 * @param len the maximum number of bytes to read
729 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
732 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
739 return GNUNET_SYSERR;
742 return read (h->fd, result, len);
747 * Read the contents of a binary file into a buffer.
748 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
749 * when no data can be read).
751 * @param h handle to an open file
752 * @param result the buffer to write the result to
753 * @param len the maximum number of bytes to read
754 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
757 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
764 return GNUNET_SYSERR;
770 /* set to non-blocking, read, then set back */
771 flags = fcntl (h->fd, F_GETFL);
772 if (0 == (flags & O_NONBLOCK))
773 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
774 ret = read (h->fd, result, len);
775 if (0 == (flags & O_NONBLOCK))
778 (void) fcntl (h->fd, F_SETFL, flags);
786 * Read the contents of a binary file into a buffer.
788 * @param fn file name
789 * @param result the buffer to write the result to
790 * @param len the maximum number of bytes to read
791 * @return number of bytes read, #GNUNET_SYSERR on failure
794 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
796 struct GNUNET_DISK_FileHandle *fh;
800 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
802 return GNUNET_SYSERR;
803 ret = GNUNET_DISK_file_read (fh, result, len);
805 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
812 * Write a buffer to a file.
814 * @param h handle to open file
815 * @param buffer the data to write
816 * @param n number of bytes to write
817 * @return number of bytes written on success, #GNUNET_SYSERR on error
820 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
827 return GNUNET_SYSERR;
831 return write (h->fd, buffer, n);
836 * Write a buffer to a file, blocking, if necessary.
838 * @param h handle to open file
839 * @param buffer the data to write
840 * @param n number of bytes to write
841 * @return number of bytes written on success, #GNUNET_SYSERR on error
844 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
851 return GNUNET_SYSERR;
858 /* set to blocking, write, then set back */
859 flags = fcntl (h->fd, F_GETFL);
860 if (0 != (flags & O_NONBLOCK))
861 (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
862 ret = write (h->fd, buffer, n);
863 if (0 == (flags & O_NONBLOCK))
864 (void) fcntl (h->fd, F_SETFL, flags);
870 * Write a buffer to a file. If the file is longer than the
871 * number of bytes that will be written, it will be truncated.
873 * @param fn file name
874 * @param buffer the data to write
875 * @param n number of bytes to write
876 * @param mode file permissions
877 * @return number of bytes written on success, #GNUNET_SYSERR on error
880 GNUNET_DISK_fn_write (const char *fn,
883 enum GNUNET_DISK_AccessPermissions mode)
885 struct GNUNET_DISK_FileHandle *fh;
889 GNUNET_DISK_file_open (fn,
890 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
891 | GNUNET_DISK_OPEN_CREATE,
894 return GNUNET_SYSERR;
895 ret = GNUNET_DISK_file_write (fh, buffer, n);
896 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
902 * Scan a directory for files.
904 * @param dir_name the name of the directory
905 * @param callback the method to call for each file,
906 * can be NULL, in that case, we only count
907 * @param callback_cls closure for @a callback
908 * @return the number of files found, #GNUNET_SYSERR on error or
909 * ieration aborted by callback returning #GNUNET_SYSERR
912 GNUNET_DISK_directory_scan (const char *dir_name,
913 GNUNET_FileNameCallback callback,
917 struct dirent *finfo;
923 unsigned int name_len;
926 GNUNET_assert (NULL != dir_name);
927 dname = GNUNET_STRINGS_filename_expand (dir_name);
929 return GNUNET_SYSERR;
930 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
931 dname[strlen (dname) - 1] = '\0';
932 if (0 != stat (dname, &istat))
934 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
936 return GNUNET_SYSERR;
938 if (! S_ISDIR (istat.st_mode))
940 LOG (GNUNET_ERROR_TYPE_WARNING,
941 _ ("Expected `%s' to be a directory!\n"),
944 return GNUNET_SYSERR;
947 dinfo = opendir (dname);
948 if ((EACCES == errno) || (NULL == dinfo))
950 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
954 return GNUNET_SYSERR;
957 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
958 name = GNUNET_malloc (n_size);
959 while (NULL != (finfo = readdir (dinfo)))
961 if ((0 == strcmp (finfo->d_name, ".")) ||
962 (0 == strcmp (finfo->d_name, "..")))
964 if (NULL != callback)
966 if (name_len < strlen (finfo->d_name))
969 name_len = strlen (finfo->d_name);
970 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
971 name = GNUNET_malloc (n_size);
973 /* dname can end in "/" only if dname == "/";
974 * if dname does not end in "/", we need to add
975 * a "/" (otherwise, we must not!) */
976 GNUNET_snprintf (name,
980 (0 == strcmp (dname, DIR_SEPARATOR_STR))
984 ret = callback (callback_cls, name);
985 if (GNUNET_OK != ret)
990 if (GNUNET_NO == ret)
992 return GNUNET_SYSERR;
1005 * Function that removes the given directory by calling
1006 * #GNUNET_DISK_directory_remove().
1008 * @param unused not used
1009 * @param fn directory to remove
1010 * @return #GNUNET_OK
1013 remove_helper (void *unused, const char *fn)
1016 (void) GNUNET_DISK_directory_remove (fn);
1022 * Remove all files in a directory (rm -r). Call with
1025 * @param filename the file to remove
1026 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1029 GNUNET_DISK_directory_remove (const char *filename)
1033 if (NULL == filename)
1036 return GNUNET_SYSERR;
1038 if (0 != lstat (filename, &istat))
1039 return GNUNET_NO; /* file may not exist... */
1040 (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR);
1041 if (0 == unlink (filename))
1043 if ((errno != EISDIR) &&
1044 /* EISDIR is not sufficient in all cases, e.g.
1045 * sticky /tmp directory may result in EPERM on BSD.
1046 * So we also explicitly check "isDirectory" */
1047 (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES)))
1049 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1050 return GNUNET_SYSERR;
1052 if (GNUNET_SYSERR ==
1053 GNUNET_DISK_directory_scan (filename, &remove_helper, NULL))
1054 return GNUNET_SYSERR;
1055 if (0 != rmdir (filename))
1057 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1058 return GNUNET_SYSERR;
1067 * @param src file to copy
1068 * @param dst destination file name
1069 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1072 GNUNET_DISK_file_copy (const char *src, const char *dst)
1079 struct GNUNET_DISK_FileHandle *in;
1080 struct GNUNET_DISK_FileHandle *out;
1082 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1084 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", src);
1085 return GNUNET_SYSERR;
1089 GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
1092 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", src);
1093 return GNUNET_SYSERR;
1096 GNUNET_DISK_file_open (dst,
1097 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
1098 | GNUNET_DISK_OPEN_FAILIFEXISTS,
1099 GNUNET_DISK_PERM_USER_READ
1100 | GNUNET_DISK_PERM_USER_WRITE
1101 | GNUNET_DISK_PERM_GROUP_READ
1102 | GNUNET_DISK_PERM_GROUP_WRITE);
1105 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", dst);
1106 GNUNET_DISK_file_close (in);
1107 return GNUNET_SYSERR;
1109 buf = GNUNET_malloc (COPY_BLK_SIZE);
1112 len = COPY_BLK_SIZE;
1113 if (len > size - pos)
1115 sret = GNUNET_DISK_file_read (in, buf, len);
1116 if ((sret < 0) || (len != (size_t) sret))
1118 sret = GNUNET_DISK_file_write (out, buf, len);
1119 if ((sret < 0) || (len != (size_t) sret))
1124 GNUNET_DISK_file_close (in);
1125 GNUNET_DISK_file_close (out);
1129 GNUNET_DISK_file_close (in);
1130 GNUNET_DISK_file_close (out);
1131 return GNUNET_SYSERR;
1136 * @brief Removes special characters as ':' from a filename.
1137 * @param fn the filename to canonicalize
1140 GNUNET_DISK_filename_canonicalize (char *fn)
1145 for (idx = fn; *idx; idx++)
1149 if ((c == '/') || (c == '\\') || (c == ':') || (c == '*') || (c == '?') ||
1153 (c == '<') || (c == '>') || (c == '|') )
1162 * @brief Change owner of a file
1164 * @param filename name of file to change the owner of
1165 * @param user name of the new owner
1166 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1169 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1173 pws = getpwnam (user);
1176 LOG (GNUNET_ERROR_TYPE_ERROR,
1177 _ ("Cannot obtain information about user `%s': %s\n"),
1180 return GNUNET_SYSERR;
1182 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1184 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1185 return GNUNET_SYSERR;
1192 * Lock a part of a file
1194 * @param fh file handle
1195 * @param lock_start absolute position from where to lock
1196 * @param lock_end absolute position until where to lock
1197 * @param excl #GNUNET_YES for an exclusive lock
1198 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1201 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
1209 return GNUNET_SYSERR;
1214 memset (&fl, 0, sizeof(struct flock));
1215 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1216 fl.l_whence = SEEK_SET;
1217 fl.l_start = lock_start;
1218 fl.l_len = lock_end;
1220 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1225 * Unlock a part of a file
1227 * @param fh file handle
1228 * @param unlock_start absolute position from where to unlock
1229 * @param unlock_end absolute position until where to unlock
1230 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1233 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
1240 return GNUNET_SYSERR;
1245 memset (&fl, 0, sizeof(struct flock));
1246 fl.l_type = F_UNLCK;
1247 fl.l_whence = SEEK_SET;
1248 fl.l_start = unlock_start;
1249 fl.l_len = unlock_end;
1251 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1256 * Open a file. Note that the access permissions will only be
1257 * used if a new file is created and if the underlying operating
1258 * system supports the given permissions.
1260 * @param fn file name to be opened
1261 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1262 * @param perm permissions for the newly created file, use
1263 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
1264 * call (because of flags)
1265 * @return IO handle on success, NULL on error
1267 struct GNUNET_DISK_FileHandle *
1268 GNUNET_DISK_file_open (const char *fn,
1269 enum GNUNET_DISK_OpenFlags flags,
1270 enum GNUNET_DISK_AccessPermissions perm)
1273 struct GNUNET_DISK_FileHandle *ret;
1279 expfn = GNUNET_STRINGS_filename_expand (fn);
1284 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1285 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1286 else if (flags & GNUNET_DISK_OPEN_READ)
1288 else if (flags & GNUNET_DISK_OPEN_WRITE)
1293 GNUNET_free (expfn);
1296 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1297 oflags |= (O_CREAT | O_EXCL);
1298 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1300 if (flags & GNUNET_DISK_OPEN_APPEND)
1302 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
1304 if (flags & GNUNET_DISK_OPEN_CREATE)
1306 (void) GNUNET_DISK_directory_create_for_file (expfn);
1308 mode = translate_unix_perms (perm);
1321 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1322 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1324 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1325 GNUNET_free (expfn);
1329 ret = GNUNET_new (struct GNUNET_DISK_FileHandle);
1333 GNUNET_free (expfn);
1339 * Close an open file.
1341 * @param h file handle
1342 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1345 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1352 return GNUNET_SYSERR;
1357 if (close (h->fd) != 0)
1359 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1360 ret = GNUNET_SYSERR;
1369 * Get a handle from a native integer FD.
1371 * @param fno native integer file descriptor
1372 * @return file handle corresponding to the descriptor, NULL on error
1374 struct GNUNET_DISK_FileHandle *
1375 GNUNET_DISK_get_handle_from_int_fd (int fno)
1377 struct GNUNET_DISK_FileHandle *fh;
1379 if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1380 return NULL; /* invalid FD */
1382 fh = GNUNET_new (struct GNUNET_DISK_FileHandle);
1391 * Get a handle from a native streaming FD.
1393 * @param fd native streaming file descriptor
1394 * @return file handle corresponding to the descriptor
1396 struct GNUNET_DISK_FileHandle *
1397 GNUNET_DISK_get_handle_from_native (FILE *fd)
1405 return GNUNET_DISK_get_handle_from_int_fd (fno);
1410 * Handle for a memory-mapping operation.
1412 struct GNUNET_DISK_MapHandle
1415 * Address where the map is in memory.
1420 * Number of bytes mapped.
1427 #define MAP_FAILED ((void *) -1)
1431 * Map a file into memory
1433 * @param h open file handle
1434 * @param m handle to the new mapping
1435 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
1436 * @param len size of the mapping
1437 * @return pointer to the mapped memory region, NULL on failure
1440 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1441 struct GNUNET_DISK_MapHandle **m,
1442 enum GNUNET_DISK_MapType access,
1454 if (access & GNUNET_DISK_MAP_TYPE_READ)
1456 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1458 *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
1459 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1460 GNUNET_assert (NULL != (*m)->addr);
1461 if (MAP_FAILED == (*m)->addr)
1473 * @param h mapping handle
1474 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1477 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1484 return GNUNET_SYSERR;
1487 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1495 * Write file changes to disk
1496 * @param h handle to an open file
1497 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1500 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1505 return GNUNET_SYSERR;
1508 #if ! defined(__linux__) || ! defined(GNU)
1509 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1511 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1517 * Creates an interprocess channel
1519 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
1520 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
1521 * @param inherit_read inherit the parent processes stdin (only for windows)
1522 * @param inherit_write inherit the parent processes stdout (only for windows)
1523 * @return handle to the new pipe, NULL on error
1525 struct GNUNET_DISK_PipeHandle *
1526 GNUNET_DISK_pipe (int blocking_read,
1535 (void) inherit_read;
1536 (void) inherit_write;
1541 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1545 return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd);
1550 * Creates a pipe object from a couple of file descriptors.
1551 * Useful for wrapping existing pipe FDs.
1553 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
1554 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
1555 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
1557 * @return handle to the new pipe, NULL on error
1559 struct GNUNET_DISK_PipeHandle *
1560 GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
1562 struct GNUNET_DISK_PipeHandle *p;
1564 p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
1568 int eno = 0; /* make gcc happy */
1573 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1574 p->fd[0]->fd = fd[0];
1575 if (! blocking_read)
1577 flags = fcntl (fd[0], F_GETFL);
1578 flags |= O_NONBLOCK;
1579 if (0 > fcntl (fd[0], F_SETFL, flags))
1585 flags = fcntl (fd[0], F_GETFD);
1586 flags |= FD_CLOEXEC;
1587 if (0 > fcntl (fd[0], F_SETFD, flags))
1596 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1597 p->fd[1]->fd = fd[1];
1598 if (! blocking_write)
1600 flags = fcntl (fd[1], F_GETFL);
1601 flags |= O_NONBLOCK;
1602 if (0 > fcntl (fd[1], F_SETFL, flags))
1608 flags = fcntl (fd[1], F_GETFD);
1609 flags |= FD_CLOEXEC;
1610 if (0 > fcntl (fd[1], F_SETFD, flags))
1619 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
1620 if (p->fd[0]->fd >= 0)
1621 GNUNET_break (0 == close (p->fd[0]->fd));
1622 if (p->fd[1]->fd >= 0)
1623 GNUNET_break (0 == close (p->fd[1]->fd));
1624 GNUNET_free_non_null (p->fd[0]);
1625 GNUNET_free_non_null (p->fd[1]);
1636 * Closes an interprocess channel
1638 * @param p pipe to close
1639 * @param end which end of the pipe to close
1640 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1643 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
1644 enum GNUNET_DISK_PipeEnd end)
1646 int ret = GNUNET_OK;
1648 if (end == GNUNET_DISK_PIPE_END_READ)
1652 ret = GNUNET_DISK_file_close (p->fd[0]);
1656 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1660 ret = GNUNET_DISK_file_close (p->fd[1]);
1670 * Detaches one of the ends from the pipe.
1671 * Detached end is a fully-functional FileHandle, it will
1672 * not be affected by anything you do with the pipe afterwards.
1673 * Each end of a pipe can only be detched from it once (i.e.
1674 * it is not duplicated).
1676 * @param p pipe to detach an end from
1677 * @param end which end of the pipe to detach
1678 * @return Detached end on success, NULL on failure
1679 * (or if that end is not present or is closed).
1681 struct GNUNET_DISK_FileHandle *
1682 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
1683 enum GNUNET_DISK_PipeEnd end)
1685 struct GNUNET_DISK_FileHandle *ret = NULL;
1687 if (end == GNUNET_DISK_PIPE_END_READ)
1695 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1709 * Closes an interprocess channel
1711 * @param p pipe to close
1712 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1715 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1717 int ret = GNUNET_OK;
1720 int write_end_close;
1721 int read_end_close_errno;
1722 int write_end_close_errno;
1724 read_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_READ);
1725 read_end_close_errno = errno;
1726 write_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_WRITE);
1727 write_end_close_errno = errno;
1730 if (GNUNET_OK != read_end_close)
1732 errno = read_end_close_errno;
1733 ret = read_end_close;
1735 else if (GNUNET_OK != write_end_close)
1737 errno = write_end_close_errno;
1738 ret = write_end_close;
1746 * Get the handle to a particular pipe end
1749 * @param n end to access
1750 * @return handle for the respective end
1752 const struct GNUNET_DISK_FileHandle *
1753 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
1754 enum GNUNET_DISK_PipeEnd n)
1758 case GNUNET_DISK_PIPE_END_READ:
1759 case GNUNET_DISK_PIPE_END_WRITE:
1770 * Retrieve OS file handle
1772 * @param fh GNUnet file descriptor
1773 * @param dst destination buffer
1774 * @param dst_len length of dst
1775 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1778 GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
1783 return GNUNET_SYSERR;
1785 if (dst_len < sizeof(int))
1786 return GNUNET_SYSERR;
1787 *((int *) dst) = fh->fd;
1794 * Helper function for #GNUNET_DISK_purge_cfg_dir.
1796 * @param cls a `const char *` with the option to purge
1797 * @param cfg our configuration
1798 * @return #GNUNET_OK on success
1801 purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1803 const char *option = cls;
1807 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
1809 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option);
1812 if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname))
1814 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "remove", tmpname);
1815 GNUNET_free (tmpname);
1818 GNUNET_free (tmpname);
1824 * Remove the directory given under @a option in
1825 * section [PATHS] in configuration under @a cfg_filename
1827 * @param cfg_filename configuration file to parse
1828 * @param option option with the dir name to purge
1831 GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option)
1833 GNUNET_break (GNUNET_OK ==
1834 GNUNET_CONFIGURATION_parse_and_run (cfg_filename,