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
189 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
190 if (ret == INVALID_SET_FILE_POINTER)
192 SetErrnoFromWinError (GetLastError ());
193 return GNUNET_SYSERR;
197 static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET,
198 [GNUNET_DISK_SEEK_CUR] = SEEK_CUR,[GNUNET_DISK_SEEK_END] = SEEK_END
201 return lseek (h->fd, offset, t[whence]);
207 * Get the size of the file (or directory) of the given file (in
210 * @param filename name of the file or directory
211 * @param size set to the size of the file (or,
212 * in the case of directories, the sum
213 * of all sizes of files in the directory)
214 * @param includeSymLinks should symbolic links be
216 * @return GNUNET_SYSERR on error, GNUNET_OK on success
219 GNUNET_DISK_file_size (const char *filename,
220 uint64_t * size, int includeSymLinks)
222 struct GetFileSizeData gfsd;
225 GNUNET_assert (size != NULL);
227 gfsd.include_sym_links = includeSymLinks;
228 ret = getSizeRec (&gfsd, filename);
235 * Obtain some unique identifiers for the given file
236 * that can be used to identify it in the local system.
237 * This function is used between GNUnet processes to
238 * quickly check if two files with the same absolute path
239 * are actually identical. The two processes represent
240 * the same peer but may communicate over the network
241 * (and the file may be on an NFS volume). This function
242 * may not be supported on all operating systems.
244 * @param filename name of the file
245 * @param dev set to the device ID
246 * @param ino set to the inode ID
247 * @return GNUNET_OK on success
250 GNUNET_DISK_file_get_identifiers (const char *filename,
251 uint32_t * dev, uint64_t * ino)
257 if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf)))
259 *dev = (uint32_t) fbuf.f_fsid;
260 *ino = (uint64_t) sbuf.st_ino;
264 return GNUNET_SYSERR;
269 * Create an (empty) temporary file on disk. If the given name is not
270 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
271 * 6 random characters will be appended to the name to create a unique
274 * @param t component to use for the name;
275 * does NOT contain "XXXXXX" or "/tmp/".
276 * @return NULL on error, otherwise name of fresh
277 * file on disk in directory for temporary files
280 GNUNET_DISK_mktemp (const char *t)
287 if ( (t[0] != '/') &&
290 tmpdir = getenv ("TMPDIR");
291 tmpdir = tmpdir ? tmpdir : "/tmp";
292 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
296 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
299 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
300 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
313 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
318 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
324 * Get the number of blocks that are left on the partition that
325 * contains the given file (for normal users).
327 * @param part a file on the partition to check
328 * @return -1 on errors, otherwise the number of free blocks
331 GNUNET_DISK_get_blocks_available (const char *part)
336 if (0 != statvfs (part, &buf))
338 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
348 path = GNUNET_STRINGS_filename_expand (part);
351 memcpy (szDrive, path, 3);
354 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
357 _("`%s' failed for drive `%s': %u\n"),
358 "GetDiskFreeSpace", szDrive, GetLastError ());
365 if (0 != statfs (part, &s))
367 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
376 * Test if "fil" is a directory.
377 * Will not print an error message if the directory
378 * does not exist. Will log errors if GNUNET_SYSERR is
379 * returned (i.e., a file exists with the same name).
381 * @param fil filename to test
382 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
386 GNUNET_DISK_directory_test (const char *fil)
388 struct stat filestat;
391 ret = STAT (fil, &filestat);
396 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
397 return GNUNET_SYSERR;
401 if (!S_ISDIR (filestat.st_mode))
403 if (ACCESS (fil, R_OK | X_OK) < 0)
405 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil);
406 return GNUNET_SYSERR;
412 * Check that fil corresponds to a filename
413 * (of a file that exists and that is not a directory).
415 * @param fil filename to check
416 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
417 * else (will print an error message in that case, too).
420 GNUNET_DISK_file_test (const char *fil)
422 struct stat filestat;
426 rdir = GNUNET_STRINGS_filename_expand (fil);
428 return GNUNET_SYSERR;
430 ret = STAT (rdir, &filestat);
435 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
437 return GNUNET_SYSERR;
442 if (!S_ISREG (filestat.st_mode))
447 if (ACCESS (rdir, R_OK) < 0)
449 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
451 return GNUNET_SYSERR;
459 * Implementation of "mkdir -p"
460 * @param dir the directory to create
461 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure
464 GNUNET_DISK_directory_create (const char *dir)
471 rdir = GNUNET_STRINGS_filename_expand (dir);
473 return GNUNET_SYSERR;
477 pos = 1; /* skip heading '/' */
479 /* Local or Network path? */
480 if (strncmp (rdir, "\\\\", 2) == 0)
485 if (rdir[pos] == '\\')
495 pos = 3; /* strlen("C:\\") */
500 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
503 ret = GNUNET_DISK_directory_test (rdir);
504 if (ret == GNUNET_SYSERR)
507 return GNUNET_SYSERR;
509 if (ret == GNUNET_NO)
512 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
516 if ((ret != 0) && (errno != EEXIST))
518 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir",
521 return GNUNET_SYSERR;
524 rdir[pos] = DIR_SEPARATOR;
534 * Create the directory structure for storing
537 * @param filename name of a file in the directory
538 * @returns GNUNET_OK on success,
539 * GNUNET_SYSERR on failure,
540 * GNUNET_NO if the directory
541 * exists but is not writeable for us
544 GNUNET_DISK_directory_create_for_file (const char *filename)
550 rdir = GNUNET_STRINGS_filename_expand (filename);
552 return GNUNET_SYSERR;
554 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
557 ret = GNUNET_DISK_directory_create (rdir);
558 if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
566 * Read the contents of a binary file into a buffer.
567 * @param h handle to an open file
568 * @param result the buffer to write the result to
569 * @param len the maximum number of bytes to read
570 * @return the number of bytes read on success, GNUNET_SYSERR on failure
573 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result,
579 return GNUNET_SYSERR;
585 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
587 SetErrnoFromWinError (GetLastError ());
588 return GNUNET_SYSERR;
592 return read (h->fd, result, len);
598 * Read the contents of a binary file into a buffer.
600 * @param fn file name
601 * @param result the buffer to write the result to
602 * @param len the maximum number of bytes to read
603 * @return number of bytes read, GNUNET_SYSERR on failure
606 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
608 struct GNUNET_DISK_FileHandle *fh;
611 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
612 GNUNET_DISK_PERM_NONE);
614 return GNUNET_SYSERR;
615 ret = GNUNET_DISK_file_read (fh, result, len);
616 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
623 * Write a buffer to a file.
624 * @param h handle to open file
625 * @param buffer the data to write
626 * @param n number of bytes to write
627 * @return number of bytes written on success, GNUNET_SYSERR on error
630 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
631 const void *buffer, size_t n)
636 return GNUNET_SYSERR;
642 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
644 SetErrnoFromWinError (GetLastError ());
645 return GNUNET_SYSERR;
649 return write (h->fd, buffer, n);
654 * Write a buffer to a file. If the file is longer than the
655 * number of bytes that will be written, it will be truncated.
657 * @param fn file name
658 * @param buffer the data to write
659 * @param n number of bytes to write
660 * @param mode file permissions
661 * @return number of bytes written on success, GNUNET_SYSERR on error
664 GNUNET_DISK_fn_write (const char *fn, const void *buffer,
665 size_t n, enum GNUNET_DISK_AccessPermissions mode)
667 struct GNUNET_DISK_FileHandle *fh;
670 fh = GNUNET_DISK_file_open (fn,
671 GNUNET_DISK_OPEN_WRITE
672 | GNUNET_DISK_OPEN_TRUNCATE
673 | GNUNET_DISK_OPEN_CREATE, mode);
675 return GNUNET_SYSERR;
676 ret = GNUNET_DISK_file_write (fh, buffer, n);
677 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
682 * Scan a directory for files.
684 * @param dirName the name of the directory
685 * @param callback the method to call for each file,
686 * can be NULL, in that case, we only count
687 * @param callback_cls closure for callback
688 * @return the number of files found, GNUNET_SYSERR on error or
689 * ieration aborted by callback returning GNUNET_SYSERR
692 GNUNET_DISK_directory_scan (const char *dirName,
693 GNUNET_FileNameCallback callback,
697 struct dirent *finfo;
702 unsigned int name_len;
705 GNUNET_assert (dirName != NULL);
706 dname = GNUNET_STRINGS_filename_expand (dirName);
707 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
708 dname[strlen (dname) - 1] = '\0';
709 if (0 != STAT (dname, &istat))
711 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
713 return GNUNET_SYSERR;
715 if (!S_ISDIR (istat.st_mode))
717 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
718 _("Expected `%s' to be a directory!\n"), dirName);
720 return GNUNET_SYSERR;
723 dinfo = OPENDIR (dname);
724 if ((errno == EACCES) || (dinfo == NULL))
726 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
730 return GNUNET_SYSERR;
733 n_size = strlen (dname) + name_len + 2;
734 name = GNUNET_malloc (n_size);
735 while ((finfo = readdir (dinfo)) != NULL)
737 if ((0 == strcmp (finfo->d_name, ".")) ||
738 (0 == strcmp (finfo->d_name, "..")))
740 if (callback != NULL)
742 if (name_len < strlen (finfo->d_name))
745 name_len = strlen (finfo->d_name);
746 n_size = strlen (dname) + name_len + 2;
747 name = GNUNET_malloc (n_size);
749 /* dname can end in "/" only if dname == "/";
750 if dname does not end in "/", we need to add
751 a "/" (otherwise, we must not!) */
752 GNUNET_snprintf (name,
756 (strcmp (dname, DIR_SEPARATOR_STR) ==
757 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
758 if (GNUNET_OK != callback (callback_cls, name))
763 return GNUNET_SYSERR;
776 * Opaque handle used for iterating over a directory.
778 struct GNUNET_DISK_DirectoryIterator
783 struct GNUNET_SCHEDULER_Handle *sched;
786 * Function to call on directory entries.
788 GNUNET_DISK_DirectoryIteratorCallback callback;
791 * Closure for callback.
796 * Reference to directory.
806 * Next filename to process.
813 enum GNUNET_SCHEDULER_Priority priority;
819 * Task used by the directory iterator.
822 directory_iterator_task (void *cls,
823 const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct GNUNET_DISK_DirectoryIterator *iter = cls;
828 name = iter->next_name;
829 GNUNET_assert (name != NULL);
830 iter->next_name = NULL;
831 iter->callback (iter->callback_cls, iter, name, iter->dirname);
837 * This function must be called during the DiskIteratorCallback
838 * (exactly once) to schedule the task to process the next
839 * filename in the directory (if there is one).
841 * @param iter opaque handle for the iterator
842 * @param can set to GNUNET_YES to terminate the iteration early
843 * @return GNUNET_YES if iteration will continue,
844 * GNUNET_NO if this was the last entry (and iteration is complete),
845 * GNUNET_SYSERR if abort was YES
848 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
851 struct dirent *finfo;
853 GNUNET_assert (iter->next_name == NULL);
854 if (can == GNUNET_YES)
856 closedir (iter->directory);
857 GNUNET_free (iter->dirname);
859 return GNUNET_SYSERR;
861 while (NULL != (finfo = readdir (iter->directory)))
863 if ((0 == strcmp (finfo->d_name, ".")) ||
864 (0 == strcmp (finfo->d_name, "..")))
866 GNUNET_asprintf (&iter->next_name,
868 iter->dirname, DIR_SEPARATOR_STR, finfo->d_name);
873 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
876 GNUNET_SCHEDULER_add_with_priority (iter->sched,
878 &directory_iterator_task, iter);
884 * Scan a directory for files using the scheduler to run a task for
885 * each entry. The name of the directory must be expanded first (!).
886 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
887 * may provide a simpler API.
889 * @param sched scheduler to use
890 * @param prio priority to use
891 * @param dirName the name of the directory
892 * @param callback the method to call for each file
893 * @param callback_cls closure for callback
896 GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
897 enum GNUNET_SCHEDULER_Priority prio,
899 GNUNET_DISK_DirectoryIteratorCallback
900 callback, void *callback_cls)
902 struct GNUNET_DISK_DirectoryIterator *di;
904 di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
906 di->callback = callback;
907 di->callback_cls = callback_cls;
908 di->directory = OPENDIR (dirName);
909 if (di->directory == NULL)
912 callback (callback_cls, NULL, NULL, NULL);
915 di->dirname = GNUNET_strdup (dirName);
917 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
922 * Function that removes the given directory by calling
923 * "GNUNET_DISK_directory_remove".
925 * @param unused not used
926 * @param fn directory to remove
930 remove_helper (void *unused, const char *fn)
932 (void) GNUNET_DISK_directory_remove (fn);
938 * Remove all files in a directory (rm -rf). Call with
942 * @param fileName the file to remove
943 * @return GNUNET_OK on success, GNUNET_SYSERR on error
946 GNUNET_DISK_directory_remove (const char *fileName)
950 if (0 != LSTAT (fileName, &istat))
951 return GNUNET_NO; /* file may not exist... */
952 if (UNLINK (fileName) == 0)
954 if ((errno != EISDIR) &&
955 /* EISDIR is not sufficient in all cases, e.g.
956 sticky /tmp directory may result in EPERM on BSD.
957 So we also explicitly check "isDirectory" */
958 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
960 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
961 return GNUNET_SYSERR;
964 GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL))
965 return GNUNET_SYSERR;
966 if (0 != RMDIR (fileName))
968 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
969 return GNUNET_SYSERR;
978 * @param src file to copy
979 * @param dst destination file name
980 * @return GNUNET_OK on success, GNUNET_SYSERR on error
983 GNUNET_DISK_file_copy (const char *src, const char *dst)
989 struct GNUNET_DISK_FileHandle *in;
990 struct GNUNET_DISK_FileHandle *out;
992 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
993 return GNUNET_SYSERR;
995 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
996 GNUNET_DISK_PERM_NONE);
998 return GNUNET_SYSERR;
999 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
1000 | GNUNET_DISK_OPEN_CREATE |
1001 GNUNET_DISK_OPEN_FAILIFEXISTS,
1002 GNUNET_DISK_PERM_USER_READ |
1003 GNUNET_DISK_PERM_USER_WRITE |
1004 GNUNET_DISK_PERM_GROUP_READ |
1005 GNUNET_DISK_PERM_GROUP_WRITE);
1008 GNUNET_DISK_file_close (in);
1009 return GNUNET_SYSERR;
1011 buf = GNUNET_malloc (COPY_BLK_SIZE);
1014 len = COPY_BLK_SIZE;
1015 if (len > size - pos)
1017 if (len != GNUNET_DISK_file_read (in, buf, len))
1019 if (len != GNUNET_DISK_file_write (out, buf, len))
1024 GNUNET_DISK_file_close (in);
1025 GNUNET_DISK_file_close (out);
1029 GNUNET_DISK_file_close (in);
1030 GNUNET_DISK_file_close (out);
1031 return GNUNET_SYSERR;
1036 * @brief Removes special characters as ':' from a filename.
1037 * @param fn the filename to canonicalize
1040 GNUNET_DISK_filename_canonicalize (char *fn)
1050 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
1051 c == '"' || c == '<' || c == '>' || c == '|')
1063 * @brief Change owner of a file
1065 * @param filename name of file to change the owner of
1066 * @param user name of the new owner
1067 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1070 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1075 pws = getpwnam (user);
1078 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1079 _("Cannot obtain information about user `%s': %s\n"),
1080 user, STRERROR (errno));
1081 return GNUNET_SYSERR;
1083 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1084 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1091 * Lock a part of a file
1092 * @param fh file handle
1093 * @param lockStart absolute position from where to lock
1094 * @param lockEnd absolute position until where to lock
1095 * @param excl GNUNET_YES for an exclusive lock
1096 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1099 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
1100 off_t lockEnd, int excl)
1105 return GNUNET_SYSERR;
1111 memset (&fl, 0, sizeof (struct flock));
1112 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1113 fl.l_whence = SEEK_SET;
1114 fl.l_start = lockStart;
1117 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1121 memset (&o, 0, sizeof (OVERLAPPED));
1122 o.Offset = lockStart;
1124 if (!LockFileEx (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0)
1125 | LOCKFILE_FAIL_IMMEDIATELY, 0, lockEnd - lockStart, 0,
1128 SetErrnoFromWinError (GetLastError ());
1129 return GNUNET_SYSERR;
1138 * Unlock a part of a file
1139 * @param fh file handle
1140 * @param unlockStart absolute position from where to unlock
1141 * @param unlockEnd absolute position until where to unlock
1142 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1145 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1151 return GNUNET_SYSERR;
1157 memset (&fl, 0, sizeof (struct flock));
1158 fl.l_type = F_UNLCK;
1159 fl.l_whence = SEEK_SET;
1160 fl.l_start = unlockStart;
1161 fl.l_len = unlockEnd;
1163 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1167 memset (&o, 0, sizeof (OVERLAPPED));
1168 o.Offset = unlockStart;
1170 if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o))
1172 SetErrnoFromWinError (GetLastError ());
1173 return GNUNET_SYSERR;
1182 * Open a file. Note that the access permissions will only be
1183 * used if a new file is created and if the underlying operating
1184 * system supports the given permissions.
1186 * @param fn file name to be opened
1187 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1188 * @param perm permissions for the newly created file, use
1189 * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this
1190 * call (because of flags)
1191 * @return IO handle on success, NULL on error
1193 struct GNUNET_DISK_FileHandle *
1194 GNUNET_DISK_file_open (const char *fn,
1195 enum GNUNET_DISK_OpenFlags flags,
1196 enum GNUNET_DISK_AccessPermissions perm)
1199 struct GNUNET_DISK_FileHandle *ret;
1210 expfn = GNUNET_STRINGS_filename_expand (fn);
1215 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1216 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1217 else if (flags & GNUNET_DISK_OPEN_READ)
1219 else if (flags & GNUNET_DISK_OPEN_WRITE)
1224 GNUNET_free (expfn);
1227 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1228 oflags |= (O_CREAT | O_EXCL);
1229 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1231 if (flags & GNUNET_DISK_OPEN_APPEND)
1233 if (flags & GNUNET_DISK_OPEN_CREATE)
1235 (void) GNUNET_DISK_directory_create_for_file (expfn);
1237 if (perm & GNUNET_DISK_PERM_USER_READ)
1239 if (perm & GNUNET_DISK_PERM_USER_WRITE)
1241 if (perm & GNUNET_DISK_PERM_USER_EXEC)
1243 if (perm & GNUNET_DISK_PERM_GROUP_READ)
1245 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
1247 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
1249 if (perm & GNUNET_DISK_PERM_OTHER_READ)
1251 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
1253 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
1257 fd = open (expfn, oflags | O_LARGEFILE, mode);
1260 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1261 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1263 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1264 GNUNET_free (expfn);
1271 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1272 access = FILE_READ_DATA | FILE_WRITE_DATA;
1273 else if (flags & GNUNET_DISK_OPEN_READ)
1274 access = FILE_READ_DATA;
1275 else if (flags & GNUNET_DISK_OPEN_WRITE)
1276 access = FILE_WRITE_DATA;
1278 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1282 else if (flags & GNUNET_DISK_OPEN_CREATE)
1284 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1285 disp = CREATE_ALWAYS;
1289 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1291 disp = TRUNCATE_EXISTING;
1295 disp = OPEN_EXISTING;
1298 /* TODO: access priviledges? */
1299 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1300 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL,
1302 if (h == INVALID_HANDLE_VALUE)
1304 SetErrnoFromWinError (GetLastError ());
1305 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1306 GNUNET_free (expfn);
1310 if (flags & GNUNET_DISK_OPEN_APPEND)
1311 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1313 SetErrnoFromWinError (GetLastError ());
1314 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer",
1317 GNUNET_free (expfn);
1322 ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
1328 GNUNET_free (expfn);
1334 * Close an open file
1335 * @param h file handle
1336 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1339 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1344 return GNUNET_SYSERR;
1348 if (!CloseHandle (h->h))
1350 SetErrnoFromWinError (GetLastError ());
1351 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1353 return GNUNET_SYSERR;
1356 if (close (h->fd) != 0)
1358 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1360 return GNUNET_SYSERR;
1369 * Construct full path to a file inside of the private
1370 * directory used by GNUnet. Also creates the corresponding
1371 * directory. If the resulting name is supposed to be
1372 * a directory, end the last argument in '/' (or pass
1373 * DIR_SEPARATOR_STR as the last argument before NULL).
1375 * @param cfg configuration to use (determines HOME)
1376 * @param serviceName name of the service
1377 * @param ... is NULL-terminated list of
1378 * path components to append to the
1379 * private directory name.
1380 * @return the constructed filename
1383 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1384 const char *serviceName, ...)
1390 unsigned int needed;
1393 GNUNET_CONFIGURATION_get_value_filename (cfg,
1394 serviceName, "HOME", &pfx))
1398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1399 _("No `%s' specified for service `%s' in configuration.\n"),
1400 "HOME", serviceName);
1403 needed = strlen (pfx) + 2;
1404 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1406 va_start (ap, serviceName);
1409 c = va_arg (ap, const char *);
1412 needed += strlen (c);
1413 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1417 ret = GNUNET_malloc (needed);
1420 va_start (ap, serviceName);
1423 c = va_arg (ap, const char *);
1426 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1427 strcat (ret, DIR_SEPARATOR_STR);
1431 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1432 GNUNET_DISK_directory_create_for_file (ret);
1434 GNUNET_DISK_directory_create (ret);
1440 * Handle for a memory-mapping operation.
1442 struct GNUNET_DISK_MapHandle
1446 * Underlying OS handle.
1451 * Address where the map is in memory.
1456 * Number of bytes mapped.
1464 #define MAP_FAILED ((void *) -1)
1468 * Map a file into memory
1470 * @param h open file handle
1471 * @param m handle to the new mapping
1472 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
1473 * @param len size of the mapping
1474 * @return pointer to the mapped memory region, NULL on failure
1477 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1478 struct GNUNET_DISK_MapHandle **m,
1479 enum GNUNET_DISK_MapType access, size_t len)
1488 DWORD mapAccess, protect;
1491 if ((access & GNUNET_DISK_MAP_TYPE_READ) &&
1492 (access & GNUNET_DISK_MAP_TYPE_WRITE))
1494 protect = PAGE_READWRITE;
1495 mapAccess = FILE_MAP_ALL_ACCESS;
1497 else if (access & GNUNET_DISK_MAP_TYPE_READ)
1499 protect = PAGE_READONLY;
1500 mapAccess = FILE_MAP_READ;
1502 else if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1504 protect = PAGE_READWRITE;
1505 mapAccess = FILE_MAP_WRITE;
1513 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1514 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1515 if ((*m)->h == INVALID_HANDLE_VALUE)
1517 SetErrnoFromWinError (GetLastError ());
1522 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1525 SetErrnoFromWinError (GetLastError ());
1526 CloseHandle ((*m)->h);
1536 if (access & GNUNET_DISK_MAP_TYPE_READ)
1538 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1540 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1541 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1542 GNUNET_assert (NULL != (*m)->addr);
1543 if (MAP_FAILED == (*m)->addr)
1557 * @param h mapping handle
1558 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1561 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1567 return GNUNET_SYSERR;
1571 ret = UnmapViewOfFile (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1572 if (ret != GNUNET_OK)
1573 SetErrnoFromWinError (GetLastError ());
1574 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1576 ret = GNUNET_SYSERR;
1577 SetErrnoFromWinError (GetLastError ());
1580 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1588 * Write file changes to disk
1589 * @param h handle to an open file
1590 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1593 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1598 return GNUNET_SYSERR;
1604 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1605 if (ret != GNUNET_OK)
1606 SetErrnoFromWinError (GetLastError ());
1608 #elif defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN)
1609 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1611 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1616 * Creates an interprocess channel
1618 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
1619 * @return handle to the new pipe, NULL on error
1621 struct GNUNET_DISK_PipeHandle *
1622 GNUNET_DISK_pipe (int blocking)
1624 struct GNUNET_DISK_PipeHandle *p;
1625 struct GNUNET_DISK_FileHandle *fds;
1628 GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) +
1629 2 * sizeof (struct GNUNET_DISK_FileHandle));
1630 fds = (struct GNUNET_DISK_FileHandle *) &p[1];
1647 p->fd[0]->fd = fd[0];
1648 p->fd[1]->fd = fd[1];
1651 flags = fcntl (fd[0], F_GETFL);
1652 flags |= O_NONBLOCK;
1653 ret = fcntl (fd[0], F_SETFL, flags);
1656 flags = fcntl (fd[1], F_GETFL);
1657 flags |= O_NONBLOCK;
1658 ret = fcntl (fd[1], F_SETFL, flags);
1663 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fcntl");
1664 GNUNET_break (0 == close (p->fd[0]->fd));
1665 GNUNET_break (0 == close (p->fd[1]->fd));
1674 ret = CreatePipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0);
1678 SetErrnoFromWinError (GetLastError ());
1686 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
1687 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
1688 /* this always fails on Windows 95, so we don't care about error handling */
1696 * Closes an interprocess channel
1698 * @param p pipe to close
1699 * @param end which end of the pipe to close
1700 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1703 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
1704 enum GNUNET_DISK_PipeEnd end)
1706 int ret = GNUNET_OK;
1710 if (end == GNUNET_DISK_PIPE_END_READ)
1712 if (!CloseHandle (p->fd[0]->h))
1714 SetErrnoFromWinError (GetLastError ());
1715 ret = GNUNET_SYSERR;
1717 p->fd[0]->h = INVALID_HANDLE_VALUE;
1719 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1721 if (!CloseHandle (p->fd[1]->h))
1723 SetErrnoFromWinError (GetLastError ());
1724 ret = GNUNET_SYSERR;
1726 p->fd[1]->h = INVALID_HANDLE_VALUE;
1731 if (end == GNUNET_DISK_PIPE_END_READ)
1733 if (0 != close (p->fd[0]->fd))
1735 ret = GNUNET_SYSERR;
1740 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1742 if (0 != close (p->fd[1]->fd))
1744 ret = GNUNET_SYSERR;
1755 * Closes an interprocess channel
1757 * @param p pipe to close
1758 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1761 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1763 int ret = GNUNET_OK;
1767 if (!CloseHandle (p->fd[0]->h))
1769 SetErrnoFromWinError (GetLastError ());
1770 ret = GNUNET_SYSERR;
1772 if (!CloseHandle (p->fd[1]->h))
1774 SetErrnoFromWinError (GetLastError ());
1775 ret = GNUNET_SYSERR;
1780 if (p->fd[0]->fd != -1)
1782 if (0 != close (p->fd[0]->fd))
1784 ret = GNUNET_SYSERR;
1789 if (p->fd[1]->fd != -1)
1791 if (0 != close (p->fd[1]->fd))
1793 ret = GNUNET_SYSERR;
1805 * Get the handle to a particular pipe end
1808 * @param n end to access
1809 * @return handle for the respective end
1811 const struct GNUNET_DISK_FileHandle *
1812 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
1813 enum GNUNET_DISK_PipeEnd n)
1817 case GNUNET_DISK_PIPE_END_READ:
1818 case GNUNET_DISK_PIPE_END_WRITE:
1828 * Retrieve OS file handle
1830 * @param fh GNUnet file descriptor
1831 * @param dst destination buffer
1832 * @param dst_len length of dst
1833 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1836 GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
1837 void *dst, size_t dst_len)
1840 if (dst_len < sizeof (HANDLE))
1841 return GNUNET_SYSERR;
1842 *((HANDLE *) dst) = fh->h;
1844 if (dst_len < sizeof (int))
1845 return GNUNET_SYSERR;
1846 *((int *) dst) = fh->fd;