2 This file is part of GNUnet.
3 (C) 2001, 2002, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief disk IO convenience methods
24 * @author Christian Grothoff
29 #include "gnunet_common.h"
30 #include "gnunet_directories.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_strings_lib.h"
38 * Block size for IO for copying files.
40 #define COPY_BLK_SIZE 65536
44 #if defined(LINUX) || defined(CYGWIN)
47 #if defined(SOMEBSD) || defined(DARWIN)
48 #include <sys/param.h>
49 #include <sys/mount.h>
52 #include <sys/types.h>
53 #include <sys/statvfs.h>
56 #define _IFMT 0170000 /* type of file */
57 #define _IFLNK 0120000 /* symbolic link */
58 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
60 #error PORT-ME: need to port statfs (how much space is left on the drive?)
66 #if !defined(SOMEBSD) && !defined(DARWIN) && !defined(WINDOWS)
70 #include <sys/statvfs.h>
75 * Handle used to manage a pipe.
77 struct GNUNET_DISK_PipeHandle
80 * File descriptors for the pipe.
82 struct GNUNET_DISK_FileHandle * fd[2];
87 * Closure for the recursion to determine the file size
90 struct GetFileSizeData
93 * Set to the total file size.
98 * GNUNET_YES if symbolic links should be included.
100 int include_sym_links;
105 * Iterate over all files in the given directory and
106 * accumulate their size.
108 * @param cls closure of type "struct GetFileSizeData"
109 * @param fn current filename we are looking at
110 * @return GNUNET_SYSERR on serious errors, otherwise GNUNET_OK
113 getSizeRec (void *cls, const char *fn)
115 struct GetFileSizeData *gfsd = cls;
123 if (0 != STAT64 (fn, &buf))
125 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn);
126 return GNUNET_SYSERR;
129 if (0 != STAT (fn, &buf))
131 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn);
132 return GNUNET_SYSERR;
135 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
136 gfsd->total += buf.st_size;
137 if ((S_ISDIR (buf.st_mode)) &&
138 (0 == ACCESS (fn, X_OK)) &&
139 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
141 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
142 return GNUNET_SYSERR;
149 * Checks whether a handle is invalid
151 * @param h handle to check
152 * @return GNUNET_YES if invalid, GNUNET_NO if valid
155 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
158 return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO;
160 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
166 * Move the read/write pointer in a file
168 * @param h handle of an open file
169 * @param offset position to move to
170 * @param whence specification to which position the offset parameter relates to
171 * @return the new position on success, GNUNET_SYSERR otherwise
174 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
175 enum GNUNET_DISK_Seek whence)
180 return GNUNET_SYSERR;
185 static DWORD t[] = { [GNUNET_DISK_SEEK_SET] = FILE_BEGIN,
186 [GNUNET_DISK_SEEK_CUR] = FILE_CURRENT, [GNUNET_DISK_SEEK_END] = FILE_END };
188 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
189 if (ret == INVALID_SET_FILE_POINTER)
191 SetErrnoFromWinError (GetLastError ());
192 return GNUNET_SYSERR;
196 static int t[] = { [GNUNET_DISK_SEEK_SET] = SEEK_SET,
197 [GNUNET_DISK_SEEK_CUR] = SEEK_CUR, [GNUNET_DISK_SEEK_END] = SEEK_END };
199 return lseek (h->fd, offset, t[whence]);
205 * Get the size of the file (or directory) of the given file (in
208 * @param filename name of the file or directory
209 * @param size set to the size of the file (or,
210 * in the case of directories, the sum
211 * of all sizes of files in the directory)
212 * @param includeSymLinks should symbolic links be
214 * @return GNUNET_SYSERR on error, GNUNET_OK on success
217 GNUNET_DISK_file_size (const char *filename,
221 struct GetFileSizeData gfsd;
224 GNUNET_assert (size != NULL);
226 gfsd.include_sym_links = includeSymLinks;
227 ret = getSizeRec (&gfsd, filename);
234 * Obtain some unique identifiers for the given file
235 * that can be used to identify it in the local system.
236 * This function is used between GNUnet processes to
237 * quickly check if two files with the same absolute path
238 * are actually identical. The two processes represent
239 * the same peer but may communicate over the network
240 * (and the file may be on an NFS volume). This function
241 * may not be supported on all operating systems.
243 * @param filename name of the file
244 * @param dev set to the device ID
245 * @param ino set to the inode ID
246 * @return GNUNET_OK on success
248 int GNUNET_DISK_file_get_identifiers (const char *filename,
256 if ( (0 == stat(filename,
258 (0 == statvfs (filename,
261 *dev = (uint32_t) fbuf.f_fsid;
262 *ino = (uint64_t) sbuf.st_ino;
265 return GNUNET_SYSERR;
270 * Create an (empty) temporary file on disk.
272 * @param t component to use for the name;
273 * does NOT contain "XXXXXX" or "/tmp/".
274 * @return NULL on error, otherwise name of fresh
275 * file on disk in directory for temporary files
278 GNUNET_DISK_mktemp (const char *t)
285 tmpdir = getenv ("TMPDIR");
286 tmpdir = tmpdir ? tmpdir : "/tmp";
288 GNUNET_asprintf (&tmpl,
295 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
296 plibc_conv_to_win_path (tmpl, fn);
304 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
311 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
319 * Get the number of blocks that are left on the partition that
320 * contains the given file (for normal users).
322 * @param part a file on the partition to check
323 * @return -1 on errors, otherwise the number of free blocks
326 GNUNET_DISK_get_blocks_available (const char *part)
331 if (0 != statvfs (part, &buf))
333 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
343 path = GNUNET_STRINGS_filename_expand (part);
344 memcpy (szDrive, path, 3);
347 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
349 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
350 _("`%s' failed for drive `%s': %u\n"),
351 "GetDiskFreeSpace", szDrive, GetLastError ());
358 if (0 != statfs (part, &s))
360 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
369 * Test if "fil" is a directory.
370 * Will not print an error message if the directory
371 * does not exist. Will log errors if GNUNET_SYSERR is
372 * returned (i.e., a file exists with the same name).
374 * @param fil filename to test
375 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
379 GNUNET_DISK_directory_test (const char *fil)
381 struct stat filestat;
384 ret = STAT (fil, &filestat);
389 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
390 return GNUNET_SYSERR;
394 if (!S_ISDIR (filestat.st_mode))
396 if (ACCESS (fil, R_OK | X_OK) < 0)
398 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil);
399 return GNUNET_SYSERR;
405 * Check that fil corresponds to a filename
406 * (of a file that exists and that is not a directory).
408 * @param fil filename to check
409 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
410 * else (will print an error message in that case, too).
413 GNUNET_DISK_file_test (const char *fil)
415 struct stat filestat;
419 rdir = GNUNET_STRINGS_filename_expand (fil);
421 return GNUNET_SYSERR;
423 ret = STAT (rdir, &filestat);
428 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
430 return GNUNET_SYSERR;
435 if (!S_ISREG (filestat.st_mode))
440 if (ACCESS (rdir, R_OK) < 0)
442 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
444 return GNUNET_SYSERR;
452 * Implementation of "mkdir -p"
453 * @param dir the directory to create
454 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure
457 GNUNET_DISK_directory_create (const char *dir)
464 rdir = GNUNET_STRINGS_filename_expand (dir);
466 return GNUNET_SYSERR;
470 pos = 1; /* skip heading '/' */
472 /* Local or Network path? */
473 if (strncmp (rdir, "\\\\", 2) == 0)
478 if (rdir[pos] == '\\')
488 pos = 3; /* strlen("C:\\") */
493 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
496 ret = GNUNET_DISK_directory_test (rdir);
497 if (ret == GNUNET_SYSERR)
500 return GNUNET_SYSERR;
502 if (ret == GNUNET_NO)
505 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
509 if ((ret != 0) && (errno != EEXIST))
511 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir",
514 return GNUNET_SYSERR;
517 rdir[pos] = DIR_SEPARATOR;
527 * Create the directory structure for storing
530 * @param filename name of a file in the directory
531 * @returns GNUNET_OK on success,
532 * GNUNET_SYSERR on failure,
533 * GNUNET_NO if the directory
534 * exists but is not writeable for us
537 GNUNET_DISK_directory_create_for_file (const char *filename)
543 rdir = GNUNET_STRINGS_filename_expand (filename);
545 return GNUNET_SYSERR;
547 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
550 ret = GNUNET_DISK_directory_create (rdir);
551 if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
559 * Read the contents of a binary file into a buffer.
560 * @param h handle to an open file
561 * @param result the buffer to write the result to
562 * @param len the maximum number of bytes to read
563 * @return the number of bytes read on success, GNUNET_SYSERR on failure
566 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
572 return GNUNET_SYSERR;
578 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
580 SetErrnoFromWinError (GetLastError ());
581 return GNUNET_SYSERR;
585 return read (h->fd, result, len);
591 * Read the contents of a binary file into a buffer.
593 * @param fn file name
594 * @param result the buffer to write the result to
595 * @param len the maximum number of bytes to read
596 * @return number of bytes read, GNUNET_SYSERR on failure
599 GNUNET_DISK_fn_read (const char * fn,
603 struct GNUNET_DISK_FileHandle *fh;
606 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
607 GNUNET_DISK_PERM_NONE);
609 return GNUNET_SYSERR;
610 ret = GNUNET_DISK_file_read (fh, result, len);
611 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
618 * Write a buffer to a file.
619 * @param h handle to open file
620 * @param buffer the data to write
621 * @param n number of bytes to write
622 * @return number of bytes written on success, GNUNET_SYSERR on error
625 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buffer,
631 return GNUNET_SYSERR;
637 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
639 SetErrnoFromWinError (GetLastError ());
640 return GNUNET_SYSERR;
644 return write (h->fd, buffer, n);
649 * Write a buffer to a file. If the file is longer than the
650 * number of bytes that will be written, it will be truncated.
652 * @param fn file name
653 * @param buffer the data to write
654 * @param n number of bytes to write
655 * @param mode file permissions
656 * @return GNUNET_OK on success, GNUNET_SYSERR on error
659 GNUNET_DISK_fn_write (const char * fn, const void *buffer,
661 enum GNUNET_DISK_AccessPermissions mode)
663 struct GNUNET_DISK_FileHandle *fh;
666 fh = GNUNET_DISK_file_open (fn,
667 GNUNET_DISK_OPEN_WRITE
668 | GNUNET_DISK_OPEN_TRUNCATE
669 | GNUNET_DISK_OPEN_CREATE, mode);
671 return GNUNET_SYSERR;
672 ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR;
673 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
679 * Scan a directory for files.
681 * @param dirName the name of the directory
682 * @param callback the method to call for each file,
683 * can be NULL, in that case, we only count
684 * @param callback_cls closure for callback
685 * @return the number of files found, GNUNET_SYSERR on error or
686 * ieration aborted by callback returning GNUNET_SYSERR
689 GNUNET_DISK_directory_scan (const char *dirName,
690 GNUNET_FileNameCallback callback,
694 struct dirent *finfo;
699 unsigned int name_len;
702 GNUNET_assert (dirName != NULL);
703 dname = GNUNET_STRINGS_filename_expand (dirName);
704 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
705 dname[strlen (dname) - 1] = '\0';
706 if (0 != STAT (dname, &istat))
708 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
710 return GNUNET_SYSERR;
712 if (!S_ISDIR (istat.st_mode))
714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
715 _("Expected `%s' to be a directory!\n"), dirName);
717 return GNUNET_SYSERR;
720 dinfo = OPENDIR (dname);
721 if ((errno == EACCES) || (dinfo == NULL))
723 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
727 return GNUNET_SYSERR;
730 n_size = strlen (dname) + name_len + 2;
731 name = GNUNET_malloc (n_size);
732 while ((finfo = readdir (dinfo)) != NULL)
734 if ((0 == strcmp (finfo->d_name, ".")) ||
735 (0 == strcmp (finfo->d_name, "..")))
737 if (callback != NULL)
739 if (name_len < strlen (finfo->d_name))
742 name_len = strlen (finfo->d_name);
743 n_size = strlen (dname) + name_len + 2;
744 name = GNUNET_malloc (n_size);
746 /* dname can end in "/" only if dname == "/";
747 if dname does not end in "/", we need to add
748 a "/" (otherwise, we must not!) */
749 GNUNET_snprintf (name,
753 (strcmp (dname, DIR_SEPARATOR_STR) ==
754 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
755 if (GNUNET_OK != callback (callback_cls, name))
760 return GNUNET_SYSERR;
773 * Opaque handle used for iterating over a directory.
775 struct GNUNET_DISK_DirectoryIterator
780 struct GNUNET_SCHEDULER_Handle *sched;
783 * Function to call on directory entries.
785 GNUNET_DISK_DirectoryIteratorCallback callback;
788 * Closure for callback.
793 * Reference to directory.
803 * Next filename to process.
810 enum GNUNET_SCHEDULER_Priority priority;
816 * Task used by the directory iterator.
819 directory_iterator_task (void *cls,
820 const struct GNUNET_SCHEDULER_TaskContext *tc)
822 struct GNUNET_DISK_DirectoryIterator *iter = cls;
825 name = iter->next_name;
826 GNUNET_assert (name != NULL);
827 iter->next_name = NULL;
828 iter->callback (iter->callback_cls, iter, name, iter->dirname);
834 * This function must be called during the DiskIteratorCallback
835 * (exactly once) to schedule the task to process the next
836 * filename in the directory (if there is one).
838 * @param iter opaque handle for the iterator
839 * @param can set to GNUNET_YES to terminate the iteration early
840 * @return GNUNET_YES if iteration will continue,
841 * GNUNET_NO if this was the last entry (and iteration is complete),
842 * GNUNET_SYSERR if abort was YES
845 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
848 struct dirent *finfo;
850 GNUNET_assert (iter->next_name == NULL);
851 if (can == GNUNET_YES)
853 closedir (iter->directory);
854 GNUNET_free (iter->dirname);
856 return GNUNET_SYSERR;
858 while (NULL != (finfo = readdir (iter->directory)))
860 if ((0 == strcmp (finfo->d_name, ".")) ||
861 (0 == strcmp (finfo->d_name, "..")))
863 GNUNET_asprintf (&iter->next_name,
865 iter->dirname, DIR_SEPARATOR_STR, finfo->d_name);
870 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
873 GNUNET_SCHEDULER_add_after (iter->sched,
876 GNUNET_SCHEDULER_NO_TASK,
877 &directory_iterator_task, iter);
883 * Scan a directory for files using the scheduler to run a task for
884 * each entry. The name of the directory must be expanded first (!).
885 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
886 * may provide a simpler API.
888 * @param sched scheduler to use
889 * @param prio priority to use
890 * @param dirName the name of the directory
891 * @param callback the method to call for each file
892 * @param callback_cls closure for callback
895 GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
896 enum GNUNET_SCHEDULER_Priority prio,
898 GNUNET_DISK_DirectoryIteratorCallback
899 callback, void *callback_cls)
901 struct GNUNET_DISK_DirectoryIterator *di;
903 di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
905 di->callback = callback;
906 di->callback_cls = callback_cls;
907 di->directory = OPENDIR (dirName);
908 di->dirname = GNUNET_strdup (dirName);
910 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
915 * Function that removes the given directory by calling
916 * "GNUNET_DISK_directory_remove".
918 * @param unused not used
919 * @param fn directory to remove
923 remove_helper (void *unused, const char *fn)
925 GNUNET_DISK_directory_remove (fn);
931 * Remove all files in a directory (rm -rf). Call with
935 * @param fileName the file to remove
936 * @return GNUNET_OK on success, GNUNET_SYSERR on error
939 GNUNET_DISK_directory_remove (const char *fileName)
943 if (0 != LSTAT (fileName, &istat))
944 return GNUNET_NO; /* file may not exist... */
945 if (UNLINK (fileName) == 0)
947 if ((errno != EISDIR) &&
948 /* EISDIR is not sufficient in all cases, e.g.
949 sticky /tmp directory may result in EPERM on BSD.
950 So we also explicitly check "isDirectory" */
951 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
953 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
954 return GNUNET_SYSERR;
957 GNUNET_DISK_directory_scan (fileName,
960 return GNUNET_SYSERR;
961 if (0 != RMDIR (fileName))
963 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
964 return GNUNET_SYSERR;
973 * @param src file to copy
974 * @param dst destination file name
975 * @return GNUNET_OK on success, GNUNET_SYSERR on error
978 GNUNET_DISK_file_copy (const char *src, const char *dst)
984 struct GNUNET_DISK_FileHandle *in;
985 struct GNUNET_DISK_FileHandle *out;
987 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
988 return GNUNET_SYSERR;
990 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
991 GNUNET_DISK_PERM_NONE);
993 return GNUNET_SYSERR;
994 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
995 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS,
996 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
997 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
1000 GNUNET_DISK_file_close (in);
1001 return GNUNET_SYSERR;
1003 buf = GNUNET_malloc (COPY_BLK_SIZE);
1006 len = COPY_BLK_SIZE;
1007 if (len > size - pos)
1009 if (len != GNUNET_DISK_file_read (in, buf, len))
1011 if (len != GNUNET_DISK_file_write (out, buf, len))
1016 GNUNET_DISK_file_close (in);
1017 GNUNET_DISK_file_close (out);
1021 GNUNET_DISK_file_close (in);
1022 GNUNET_DISK_file_close (out);
1023 return GNUNET_SYSERR;
1028 * @brief Removes special characters as ':' from a filename.
1029 * @param fn the filename to canonicalize
1032 GNUNET_DISK_filename_canonicalize (char *fn)
1042 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
1043 c == '"' || c == '<' || c == '>' || c == '|')
1055 * @brief Change owner of a file
1057 * @param filename name of file to change the owner of
1058 * @param user name of the new owner
1059 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1062 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1067 pws = getpwnam (user);
1070 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1071 _("Cannot obtain information about user `%s': %s\n"),
1072 user, STRERROR (errno));
1073 return GNUNET_SYSERR;
1075 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1076 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1083 * Lock a part of a file
1084 * @param fh file handle
1085 * @param lockStart absolute position from where to lock
1086 * @param lockEnd absolute position until where to lock
1087 * @param excl GNUNET_YES for an exclusive lock
1088 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1091 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1092 off_t lockEnd, int excl)
1097 return GNUNET_SYSERR;
1103 memset (&fl, 0, sizeof(struct flock));
1104 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1105 fl.l_whence = SEEK_SET;
1106 fl.l_start = lockStart;
1109 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1113 memset (&o, 0, sizeof(OVERLAPPED));
1114 o.Offset = lockStart;
1116 if (!LockFileEx (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0)
1117 | LOCKFILE_FAIL_IMMEDIATELY, 0, lockEnd - lockStart, 0, &o))
1119 SetErrnoFromWinError (GetLastError ());
1120 return GNUNET_SYSERR;
1129 * Unlock a part of a file
1130 * @param fh file handle
1131 * @param unlockStart absolute position from where to unlock
1132 * @param unlockEnd absolute position until where to unlock
1133 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1136 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1142 return GNUNET_SYSERR;
1148 memset (&fl, 0, sizeof(struct flock));
1149 fl.l_type = F_UNLCK;
1150 fl.l_whence = SEEK_SET;
1151 fl.l_start = unlockStart;
1152 fl.l_len = unlockEnd;
1154 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1158 memset (&o, 0, sizeof(OVERLAPPED));
1159 o.Offset = unlockStart;
1161 if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o))
1163 SetErrnoFromWinError (GetLastError ());
1164 return GNUNET_SYSERR;
1173 * Open a file. Note that the access permissions will only be
1174 * used if a new file is created and if the underlying operating
1175 * system supports the given permissions.
1177 * @param fn file name to be opened
1178 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1179 * @param perm permissions for the newly created file, use
1180 * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this
1181 * call (because of flags)
1182 * @return IO handle on success, NULL on error
1184 struct GNUNET_DISK_FileHandle *
1185 GNUNET_DISK_file_open (const char *fn,
1186 enum GNUNET_DISK_OpenFlags flags,
1187 enum GNUNET_DISK_AccessPermissions perm)
1190 struct GNUNET_DISK_FileHandle *ret;
1201 expfn = GNUNET_STRINGS_filename_expand (fn);
1205 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1206 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1207 else if (flags & GNUNET_DISK_OPEN_READ)
1209 else if (flags & GNUNET_DISK_OPEN_WRITE)
1214 GNUNET_free (expfn);
1217 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1218 oflags |= (O_CREAT & O_EXCL);
1219 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1221 if (flags & GNUNET_DISK_OPEN_APPEND)
1223 if (flags & GNUNET_DISK_OPEN_CREATE)
1226 if (perm & GNUNET_DISK_PERM_USER_READ)
1228 if (perm & GNUNET_DISK_PERM_USER_WRITE)
1230 if (perm & GNUNET_DISK_PERM_USER_EXEC)
1232 if (perm & GNUNET_DISK_PERM_GROUP_READ)
1234 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
1236 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
1238 if (perm & GNUNET_DISK_PERM_OTHER_READ)
1240 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
1242 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
1246 fd = open (expfn, oflags | O_LARGEFILE, mode);
1249 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1250 GNUNET_free (expfn);
1257 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1258 access = FILE_READ_DATA | FILE_WRITE_DATA;
1259 else if (flags & GNUNET_DISK_OPEN_READ)
1260 access = FILE_READ_DATA;
1261 else if (flags & GNUNET_DISK_OPEN_WRITE)
1262 access = FILE_WRITE_DATA;
1264 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1268 else if (flags & GNUNET_DISK_OPEN_CREATE)
1270 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1271 disp = CREATE_ALWAYS;
1275 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1277 disp = TRUNCATE_EXISTING;
1284 /* TODO: access priviledges? */
1285 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1286 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1287 if (h == INVALID_HANDLE_VALUE)
1289 SetErrnoFromWinError (GetLastError ());
1290 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1291 GNUNET_free (expfn);
1295 if (flags & GNUNET_DISK_OPEN_APPEND)
1296 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1298 SetErrnoFromWinError (GetLastError ());
1299 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1301 GNUNET_free (expfn);
1306 ret = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1312 GNUNET_free (expfn);
1318 * Close an open file
1319 * @param h file handle
1320 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1323 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1328 return GNUNET_SYSERR;
1332 if (!CloseHandle (h->h))
1334 SetErrnoFromWinError (GetLastError ());
1335 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1337 return GNUNET_SYSERR;
1340 if (close (h->fd) != 0)
1342 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1344 return GNUNET_SYSERR;
1353 * Construct full path to a file inside of the private
1354 * directory used by GNUnet. Also creates the corresponding
1355 * directory. If the resulting name is supposed to be
1356 * a directory, end the last argument in '/' (or pass
1357 * DIR_SEPARATOR_STR as the last argument before NULL).
1359 * @param cfg configuration to use (determines HOME)
1360 * @param serviceName name of the service
1361 * @param ... is NULL-terminated list of
1362 * path components to append to the
1363 * private directory name.
1364 * @return the constructed filename
1367 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1368 const char *serviceName, ...)
1374 unsigned int needed;
1377 GNUNET_CONFIGURATION_get_value_filename (cfg,
1378 serviceName, "HOME", &pfx))
1382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1383 _("No `%s' specified for service `%s' in configuration.\n"),
1384 "HOME", serviceName);
1387 needed = strlen (pfx) + 2;
1388 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1390 va_start (ap, serviceName);
1393 c = va_arg (ap, const char *);
1396 needed += strlen (c);
1397 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1401 ret = GNUNET_malloc (needed);
1404 va_start (ap, serviceName);
1407 c = va_arg (ap, const char *);
1410 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1411 strcat (ret, DIR_SEPARATOR_STR);
1415 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1416 GNUNET_DISK_directory_create_for_file (ret);
1418 GNUNET_DISK_directory_create (ret);
1424 * Handle for a memory-mapping operation.
1426 struct GNUNET_DISK_MapHandle
1430 * Underlying OS handle.
1435 * Address where the map is in memory.
1440 * Number of bytes mapped.
1448 * Map a file into memory
1450 * @param h open file handle
1451 * @param m handle to the new mapping
1452 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
1453 * @param len size of the mapping
1454 * @return pointer to the mapped memory region, NULL on failure
1457 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m,
1458 enum GNUNET_DISK_MapType access, size_t len)
1467 DWORD mapAccess, protect;
1470 if ((access & GNUNET_DISK_MAP_TYPE_READ) &&
1471 (access & GNUNET_DISK_MAP_TYPE_WRITE))
1473 protect = PAGE_READWRITE;
1474 mapAccess = FILE_MAP_ALL_ACCESS;
1476 else if (access & GNUNET_DISK_MAP_TYPE_READ)
1478 protect = PAGE_READONLY;
1479 mapAccess = FILE_MAP_READ;
1481 else if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1483 protect = PAGE_READWRITE;
1484 mapAccess = FILE_MAP_WRITE;
1492 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1493 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1494 if ((*m)->h == INVALID_HANDLE_VALUE)
1496 SetErrnoFromWinError (GetLastError ());
1501 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1504 SetErrnoFromWinError (GetLastError ());
1505 CloseHandle ((*m)->h);
1514 if (access & GNUNET_DISK_MAP_TYPE_READ)
1516 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1518 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1519 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1527 * @param h mapping handle
1528 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1531 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1537 return GNUNET_SYSERR;
1541 ret = UnmapViewOfFile (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1542 if (ret != GNUNET_OK)
1543 SetErrnoFromWinError (GetLastError ());
1544 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1546 ret = GNUNET_SYSERR;
1547 SetErrnoFromWinError (GetLastError ());
1550 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1558 * Write file changes to disk
1559 * @param h handle to an open file
1560 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1563 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1568 return GNUNET_SYSERR;
1574 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1575 if (ret != GNUNET_OK)
1576 SetErrnoFromWinError (GetLastError ());
1578 #elif defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN)
1579 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1581 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1586 * Creates an interprocess channel
1588 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
1589 * @return handle to the new pipe, NULL on error
1591 struct GNUNET_DISK_PipeHandle *
1592 GNUNET_DISK_pipe (int blocking)
1594 struct GNUNET_DISK_PipeHandle *p;
1595 struct GNUNET_DISK_FileHandle *fds;
1597 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + 2 * sizeof (struct GNUNET_DISK_FileHandle));
1598 fds = (struct GNUNET_DISK_FileHandle *) &p[1];
1615 p->fd[0]->fd = fd[0];
1616 p->fd[1]->fd = fd[1];
1619 flags = fcntl (fd[0], F_GETFL);
1620 flags |= O_NONBLOCK;
1621 ret = fcntl (fd[0], F_SETFL, flags);
1624 flags = fcntl (fd[1], F_GETFL);
1625 flags |= O_NONBLOCK;
1626 ret = fcntl (fd[1], F_SETFL, flags);
1631 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl");
1632 GNUNET_break (0 == close (p->fd[0]->fd));
1633 GNUNET_break (0 == close (p->fd[1]->fd));
1642 ret = CreatePipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0);
1646 SetErrnoFromWinError (GetLastError ());
1654 p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1655 p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1656 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
1657 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
1658 /* this always fails on Windows 95, so we don't care about error handling */
1666 * Closes an interprocess channel
1668 * @param p pipe to close
1669 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1672 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1674 int ret = GNUNET_OK;
1678 if (!CloseHandle (p->fd[0]->h))
1680 SetErrnoFromWinError (GetLastError ());
1681 ret = GNUNET_SYSERR;
1683 if (!CloseHandle (p->fd[1]->h))
1685 SetErrnoFromWinError (GetLastError ());
1686 ret = GNUNET_SYSERR;
1691 if (0 != close (p->fd[0]->fd))
1693 ret = GNUNET_SYSERR;
1696 if (0 != close (p->fd[1]->fd))
1698 ret = GNUNET_SYSERR;
1709 * Get the handle to a particular pipe end
1711 * @param n end to access
1713 const struct GNUNET_DISK_FileHandle *
1714 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
1715 enum GNUNET_DISK_PipeEnd n)
1719 case GNUNET_DISK_PIPE_END_READ:
1720 case GNUNET_DISK_PIPE_END_WRITE:
1730 * Retrieve OS file handle
1732 * @param fh GNUnet file descriptor
1733 * @param dst destination buffer
1734 * @param dst_len length of dst
1735 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1738 GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
1743 if (dst_len < sizeof (HANDLE))
1744 return GNUNET_SYSERR;
1745 *((HANDLE *) dst) = fh->h;
1747 if (dst_len < sizeof(int))
1748 return GNUNET_SYSERR;
1749 *((int *) dst) = fh->fd;