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"
40 #include <sys/param.h>
41 #include <sys/mount.h>
44 #include <sys/param.h>
45 #include <sys/mount.h>
48 #include <sys/types.h>
49 #include <sys/statvfs.h>
52 #define _IFMT 0170000 /* type of file */
53 #define _IFLNK 0120000 /* symbolic link */
54 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
56 #error PORT-ME: need to port statfs (how much space is left on the drive?)
73 unsigned long long total;
74 int include_sym_links;
77 struct GNUNET_DISK_FileHandle
87 getSizeRec (void *ptr, const char *fn)
89 GetFileSizeData *gfsd = ptr;
97 if (0 != STAT64 (fn, &buf))
99 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn);
100 return GNUNET_SYSERR;
103 if (0 != STAT (fn, &buf))
105 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn);
106 return GNUNET_SYSERR;
109 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
110 gfsd->total += buf.st_size;
111 if ((S_ISDIR (buf.st_mode)) &&
112 (0 == ACCESS (fn, X_OK)) &&
113 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
115 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
116 return GNUNET_SYSERR;
122 * Checks whether a handle is invalid
123 * @param h handle to check
124 * @return GNUNET_YES if invalid, GNUNET_NO if valid
127 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
130 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
132 return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO;
138 * Move the read/write pointer in a file
139 * @param h handle of an open file
140 * @param offset position to move to
141 * @param whence specification to which position the offset parameter relates to
142 * @return the new position on success, GNUNET_SYSERR otherwise
145 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
146 enum GNUNET_DISK_Seek whence)
151 return GNUNET_SYSERR;
156 static DWORD t[] = { [GNUNET_SEEK_SET] = FILE_BEGIN,
157 [GNUNET_SEEK_CUR] = FILE_CURRENT, [GNUNET_SEEK_END] = FILE_END };
159 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
160 if (ret == INVALID_SET_FILE_POINTER)
162 SetErrnoFromWinError (GetLastError ());
163 return GNUNET_SYSERR;
167 static int t[] = { [GNUNET_SEEK_SET] = SEEK_SET,
168 [GNUNET_SEEK_CUR] = SEEK_CUR, [GNUNET_SEEK_END] = SEEK_END };
170 return lseek (h->fd, offset, t[whence]);
175 * Get the size of the file (or directory)
176 * of the given file (in bytes).
178 * @return GNUNET_SYSERR on error, GNUNET_OK on success
181 GNUNET_DISK_file_size (const char *filename,
182 unsigned long long *size, int includeSymLinks)
184 GetFileSizeData gfsd;
187 GNUNET_assert (size != NULL);
189 gfsd.include_sym_links = includeSymLinks;
190 ret = getSizeRec (&gfsd, filename);
197 * Create an (empty) temporary file on disk.
199 * @param template component to use for the name;
200 * does NOT contain "XXXXXX" or "/tmp/".
201 * @return NULL on error, otherwise name of fresh
202 * file on disk in directory for temporary files
205 GNUNET_DISK_mktemp (const char *template)
212 tmpdir = getenv ("TMPDIR");
213 tmpdir = tmpdir ? tmpdir : "/tmp";
215 GNUNET_asprintf (&tmpl,
222 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
223 plibc_conv_to_win_path (tmpl, fn);
231 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
243 * Get the number of blocks that are left on the partition that
244 * contains the given file (for normal users).
246 * @param part a file on the partition to check
247 * @return -1 on errors, otherwise the number of free blocks
250 GNUNET_DISK_get_blocks_available (const char *part)
255 if (0 != statvfs (part, &buf))
257 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
266 memcpy (szDrive, part, 3);
268 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 _("`%s' failed for drive `%s': %u\n"),
272 "GetDiskFreeSpace", szDrive, GetLastError ());
279 if (0 != statfs (part, &s))
281 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
289 * Test if fil is a directory.
291 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
295 GNUNET_DISK_directory_test (const char *fil)
297 struct stat filestat;
300 ret = STAT (fil, &filestat);
305 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
306 return GNUNET_SYSERR;
310 if (!S_ISDIR (filestat.st_mode))
312 if (ACCESS (fil, R_OK | X_OK) < 0)
314 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil);
315 return GNUNET_SYSERR;
321 * Check that fil corresponds to a filename
322 * (of a file that exists and that is not a directory).
323 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
324 * else (will print an error message in that case, too).
327 GNUNET_DISK_file_test (const char *fil)
329 struct stat filestat;
333 rdir = GNUNET_STRINGS_filename_expand (fil);
335 return GNUNET_SYSERR;
337 ret = STAT (rdir, &filestat);
342 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
344 return GNUNET_SYSERR;
349 if (!S_ISREG (filestat.st_mode))
354 if (ACCESS (rdir, R_OK) < 0)
356 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
358 return GNUNET_SYSERR;
365 * Implementation of "mkdir -p"
366 * @param dir the directory to create
367 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure
370 GNUNET_DISK_directory_create (const char *dir)
377 rdir = GNUNET_STRINGS_filename_expand (dir);
379 return GNUNET_SYSERR;
383 pos = 1; /* skip heading '/' */
385 /* Local or Network path? */
386 if (strncmp (rdir, "\\\\", 2) == 0)
391 if (rdir[pos] == '\\')
401 pos = 3; /* strlen("C:\\") */
406 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
409 ret = GNUNET_DISK_directory_test (rdir);
410 if (ret == GNUNET_SYSERR)
413 return GNUNET_SYSERR;
415 if (ret == GNUNET_NO)
418 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
422 if ((ret != 0) && (errno != EEXIST))
424 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir",
427 return GNUNET_SYSERR;
430 rdir[pos] = DIR_SEPARATOR;
440 * Create the directory structure for storing
443 * @param filename name of a file in the directory
444 * @returns GNUNET_OK on success,
445 * GNUNET_SYSERR on failure,
446 * GNUNET_NO if the directory
447 * exists but is not writeable for us
450 GNUNET_DISK_directory_create_for_file (const char *dir)
456 rdir = GNUNET_STRINGS_filename_expand (dir);
458 return GNUNET_SYSERR;
460 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
463 ret = GNUNET_DISK_directory_create (rdir);
464 if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
471 * Read the contents of a binary file into a buffer.
472 * @param h handle to an open file
473 * @param result the buffer to write the result to
474 * @param len the maximum number of bytes to read
475 * @return the number of bytes read on success, GNUNET_SYSERR on failure
478 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
484 return GNUNET_SYSERR;
490 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
492 SetErrnoFromWinError (GetLastError ());
493 return GNUNET_SYSERR;
497 return read (h->fd, result, len);
503 * Read the contents of a binary file into a buffer.
505 * @param fn file name
506 * @param result the buffer to write the result to
507 * @param len the maximum number of bytes to read
508 * @return number of bytes read, GNUNET_SYSERR on failure
511 GNUNET_DISK_fn_read (const char * const fn,
515 struct GNUNET_DISK_FileHandle *fh;
518 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
520 return GNUNET_SYSERR;
521 ret = GNUNET_DISK_file_read (fh, result, len);
522 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
529 * Write a buffer to a file.
530 * @param h handle to open file
531 * @param buffer the data to write
532 * @param n number of bytes to write
533 * @return number of bytes written on success, GNUNET_SYSERR on error
536 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buffer,
542 return GNUNET_SYSERR;
548 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
550 SetErrnoFromWinError (GetLastError ());
551 return GNUNET_SYSERR;
555 return write (h->fd, buffer, n);
560 * Write a buffer to a file. If the file is longer than the
561 * number of bytes that will be written, iit will be truncated.
563 * @param fn file name
564 * @param buffer the data to write
565 * @param n number of bytes to write
566 * @return GNUNET_OK on success, GNUNET_SYSERR on error
569 GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
572 struct GNUNET_DISK_FileHandle *fh;
575 fh = GNUNET_DISK_file_open (fn,
576 GNUNET_DISK_OPEN_WRITE
577 | GNUNET_DISK_OPEN_TRUNCATE
578 | GNUNET_DISK_OPEN_CREATE, mode);
580 return GNUNET_SYSERR;
581 ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR;
582 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
588 * Scan a directory for files. The name of the directory
589 * must be expanded first (!).
590 * @param dirName the name of the directory
591 * @param callback the method to call for each file,
592 * can be NULL, in that case, we only count
593 * @param data argument to pass to callback
594 * @return the number of files found, GNUNET_SYSERR on error or
595 * ieration aborted by callback returning GNUNET_SYSERR
598 GNUNET_DISK_directory_scan (const char *dirName,
599 GNUNET_FileNameCallback callback, void *data)
602 struct dirent *finfo;
607 unsigned int name_len;
610 GNUNET_assert (dirName != NULL);
611 dname = GNUNET_STRINGS_filename_expand (dirName);
612 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
613 dname[strlen (dname) - 1] = '\0';
614 if (0 != STAT (dname, &istat))
616 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
618 return GNUNET_SYSERR;
620 if (!S_ISDIR (istat.st_mode))
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 _("Expected `%s' to be a directory!\n"), dirName);
625 return GNUNET_SYSERR;
628 dinfo = OPENDIR (dname);
629 if ((errno == EACCES) || (dinfo == NULL))
631 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
635 return GNUNET_SYSERR;
638 n_size = strlen (dname) + name_len + 2;
639 name = GNUNET_malloc (n_size);
640 while ((finfo = readdir (dinfo)) != NULL)
642 if ((0 == strcmp (finfo->d_name, ".")) ||
643 (0 == strcmp (finfo->d_name, "..")))
645 if (callback != NULL)
647 if (name_len < strlen (finfo->d_name))
650 name_len = strlen (finfo->d_name);
651 n_size = strlen (dname) + name_len + 2;
652 name = GNUNET_malloc (n_size);
654 /* dname can end in "/" only if dname == "/";
655 if dname does not end in "/", we need to add
656 a "/" (otherwise, we must not!) */
657 GNUNET_snprintf (name,
661 (strcmp (dname, DIR_SEPARATOR_STR) ==
662 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
663 if (GNUNET_OK != callback (data, name))
668 return GNUNET_SYSERR;
681 * Opaque handle used for iterating over a directory.
683 struct GNUNET_DISK_DirectoryIterator
688 struct GNUNET_SCHEDULER_Handle *sched;
691 * Function to call on directory entries.
693 GNUNET_DISK_DirectoryIteratorCallback callback;
696 * Closure for callback.
701 * Reference to directory.
711 * Next filename to process.
718 enum GNUNET_SCHEDULER_Priority priority;
724 * Task used by the directory iterator.
727 directory_iterator_task (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc)
730 struct GNUNET_DISK_DirectoryIterator *iter = cls;
733 name = iter->next_name;
734 GNUNET_assert (name != NULL);
735 iter->next_name = NULL;
736 iter->callback (iter->callback_cls, iter, name, iter->dirname);
742 * This function must be called during the DiskIteratorCallback
743 * (exactly once) to schedule the task to process the next
744 * filename in the directory (if there is one).
746 * @param iter opaque handle for the iterator
747 * @param can set to GNUNET_YES to terminate the iteration early
748 * @return GNUNET_YES if iteration will continue,
749 * GNUNET_NO if this was the last entry (and iteration is complete),
750 * GNUNET_SYSERR if abort was YES
753 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
756 struct dirent *finfo;
758 GNUNET_assert (iter->next_name == NULL);
759 if (can == GNUNET_YES)
761 closedir (iter->directory);
762 GNUNET_free (iter->dirname);
764 return GNUNET_SYSERR;
766 while (NULL != (finfo = readdir (iter->directory)))
768 if ((0 == strcmp (finfo->d_name, ".")) ||
769 (0 == strcmp (finfo->d_name, "..")))
771 GNUNET_asprintf (&iter->next_name,
773 iter->dirname, DIR_SEPARATOR_STR, finfo->d_name);
778 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
781 GNUNET_SCHEDULER_add_after (iter->sched,
784 GNUNET_SCHEDULER_NO_TASK,
785 &directory_iterator_task, iter);
791 * Scan a directory for files using the scheduler to run a task for
792 * each entry. The name of the directory must be expanded first (!).
793 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
794 * may provide a simpler API.
796 * @param sched scheduler to use
797 * @param prio priority to use
798 * @param dirName the name of the directory
799 * @param callback the method to call for each file
800 * @param callback_cls closure for callback
803 GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
804 enum GNUNET_SCHEDULER_Priority prio,
806 GNUNET_DISK_DirectoryIteratorCallback
807 callback, void *callback_cls)
809 struct GNUNET_DISK_DirectoryIterator *di;
811 di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
813 di->callback = callback;
814 di->callback_cls = callback_cls;
815 di->directory = OPENDIR (dirName);
816 di->dirname = GNUNET_strdup (dirName);
818 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
823 remove_helper (void *unused, const char *fn)
825 GNUNET_DISK_directory_remove (fn);
830 * Remove all files in a directory (rm -rf). Call with
834 * @param fileName the file to remove
835 * @return GNUNET_OK on success, GNUNET_SYSERR on error
838 GNUNET_DISK_directory_remove (const char *fileName)
842 if (0 != LSTAT (fileName, &istat))
843 return GNUNET_NO; /* file may not exist... */
844 if (UNLINK (fileName) == 0)
846 if ((errno != EISDIR) &&
847 /* EISDIR is not sufficient in all cases, e.g.
848 sticky /tmp directory may result in EPERM on BSD.
849 So we also explicitly check "isDirectory" */
850 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
852 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
853 return GNUNET_SYSERR;
856 GNUNET_DISK_directory_scan (fileName, remove_helper, NULL))
857 return GNUNET_SYSERR;
858 if (0 != RMDIR (fileName))
860 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
861 return GNUNET_SYSERR;
866 #define COPY_BLK_SIZE 65536
870 * @return GNUNET_OK on success, GNUNET_SYSERR on error
873 GNUNET_DISK_file_copy (const char *src, const char *dst)
876 unsigned long long pos;
877 unsigned long long size;
878 unsigned long long len;
879 struct GNUNET_DISK_FileHandle *in, *out;
881 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
882 return GNUNET_SYSERR;
884 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ);
886 return GNUNET_SYSERR;
887 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
888 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS,
889 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
890 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
893 GNUNET_DISK_file_close (in);
894 return GNUNET_SYSERR;
896 buf = GNUNET_malloc (COPY_BLK_SIZE);
900 if (len > size - pos)
902 if (len != GNUNET_DISK_file_read (in, buf, len))
904 if (len != GNUNET_DISK_file_write (out, buf, len))
909 GNUNET_DISK_file_close (in);
910 GNUNET_DISK_file_close (out);
914 GNUNET_DISK_file_close (in);
915 GNUNET_DISK_file_close (out);
916 return GNUNET_SYSERR;
921 * @brief Removes special characters as ':' from a filename.
922 * @param fn the filename to canonicalize
925 GNUNET_DISK_filename_canonicalize (char *fn)
935 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
936 c == '"' || c == '<' || c == '>' || c == '|')
948 * @brief Change owner of a file
951 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
956 pws = getpwnam (user);
959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960 _("Cannot obtain information about user `%s': %s\n"),
961 user, STRERROR (errno));
962 return GNUNET_SYSERR;
964 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
965 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
972 * Lock a part of a file
973 * @param fh file handle
974 * @lockStart absolute position from where to lock
975 * @lockEnd absolute position until where to lock
976 * @return GNUNET_OK on success, GNUNET_SYSERR on error
979 GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
985 return GNUNET_SYSERR;
991 memset(&fl, 0, sizeof(struct flock));
993 fl.l_whence = SEEK_SET;
994 fl.l_start = lockStart;
997 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
999 if (!LockFile(fh->h, 0, lockStart, 0, lockEnd))
1001 SetErrnoFromWinError(GetLastError());
1002 return GNUNET_SYSERR;
1012 * @param fn file name to be opened
1013 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1014 * @param perm permissions for the newly created file
1015 * @return IO handle on success, NULL on error
1017 struct GNUNET_DISK_FileHandle *
1018 GNUNET_DISK_file_open (const char *fn, int flags, ...)
1021 struct GNUNET_DISK_FileHandle *ret;
1032 expfn = GNUNET_STRINGS_filename_expand (fn);
1037 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1038 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1039 else if (flags & GNUNET_DISK_OPEN_READ)
1041 else if (flags & GNUNET_DISK_OPEN_WRITE)
1048 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1049 oflags |= (O_CREAT & O_EXCL);
1050 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1052 if (flags & GNUNET_DISK_OPEN_APPEND)
1054 if (flags & GNUNET_DISK_OPEN_CREATE)
1061 va_start (arg, flags);
1062 perm = va_arg (arg, int);
1065 if (perm & GNUNET_DISK_PERM_USER_READ)
1067 if (perm & GNUNET_DISK_PERM_USER_WRITE)
1069 if (perm & GNUNET_DISK_PERM_USER_EXEC)
1071 if (perm & GNUNET_DISK_PERM_GROUP_READ)
1073 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
1075 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
1077 if (perm & GNUNET_DISK_PERM_OTHER_READ)
1079 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
1081 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
1085 fd = open (expfn, oflags | O_LARGEFILE, mode);
1088 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1089 GNUNET_free (expfn);
1096 if (flags & GNUNET_DISK_OPEN_READ)
1097 access = FILE_READ_DATA;
1098 if (flags & GNUNET_DISK_OPEN_WRITE)
1099 access = FILE_WRITE_DATA;
1101 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1103 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1104 disp = TRUNCATE_EXISTING;
1105 if (flags & GNUNET_DISK_OPEN_CREATE)
1106 disp |= OPEN_ALWAYS;
1108 /* TODO: access priviledges? */
1109 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1110 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1111 if (h == INVALID_HANDLE_VALUE)
1113 SetErrnoFromWinError (GetLastError ());
1114 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1115 GNUNET_free (expfn);
1119 if (flags & GNUNET_DISK_OPEN_APPEND)
1120 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1122 SetErrnoFromWinError (GetLastError ());
1123 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1125 GNUNET_free (expfn);
1130 ret = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1136 GNUNET_free (expfn);
1141 * Close an open file
1142 * @param h file handle
1143 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1146 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1151 return GNUNET_SYSERR;
1155 if (!CloseHandle (h->h))
1157 SetErrnoFromWinError (GetLastError ());
1158 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1160 return GNUNET_SYSERR;
1163 if (close (h->fd) != 0)
1165 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1167 return GNUNET_SYSERR;
1175 * Construct full path to a file inside of the private
1176 * directory used by GNUnet. Also creates the corresponding
1177 * directory. If the resulting name is supposed to be
1178 * a directory, end the last argument in '/' (or pass
1179 * DIR_SEPARATOR_STR as the last argument before NULL).
1181 * @param cfg configuration to use (determines HOME)
1182 * @param serviceName name of the service
1183 * @param varargs is NULL-terminated list of
1184 * path components to append to the
1185 * private directory name.
1186 * @return the constructed filename
1189 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1190 const char *serviceName, ...)
1196 unsigned int needed;
1199 GNUNET_CONFIGURATION_get_value_filename (cfg,
1200 serviceName, "HOME", &pfx))
1204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1205 _("No `%s' specified for service `%s' in configuration.\n"),
1206 "HOME", serviceName);
1209 needed = strlen (pfx) + 2;
1210 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1212 va_start (ap, serviceName);
1215 c = va_arg (ap, const char *);
1218 needed += strlen (c);
1219 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1223 ret = GNUNET_malloc (needed);
1226 va_start (ap, serviceName);
1229 c = va_arg (ap, const char *);
1232 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1233 strcat (ret, DIR_SEPARATOR_STR);
1237 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1238 GNUNET_DISK_directory_create_for_file (ret);
1240 GNUNET_DISK_directory_create (ret);
1244 struct GNUNET_DISK_MapHandle
1256 * Map a file into memory
1257 * @param h open file handle
1258 * @param m handle to the new mapping
1259 * @param access access specification, GNUNET_DISK_MAP_xxx
1260 * @param len size of the mapping
1261 * @return pointer to the mapped memory region, NULL on failure
1264 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m,
1265 int access, size_t len)
1274 DWORD mapAccess, protect;
1277 if (access & GNUNET_DISK_MAP_READ && access & GNUNET_DISK_MAP_WRITE)
1279 protect = PAGE_READWRITE;
1280 mapAccess = FILE_MAP_ALL_ACCESS;
1282 else if (access & GNUNET_DISK_MAP_READ)
1284 protect = PAGE_READONLY;
1285 mapAccess = FILE_MAP_READ;
1287 else if (access & GNUNET_DISK_MAP_WRITE)
1289 protect = PAGE_READWRITE;
1290 mapAccess = FILE_MAP_WRITE;
1298 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1299 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1300 if ((*m)->h == INVALID_HANDLE_VALUE)
1302 SetErrnoFromWinError (GetLastError ());
1307 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1310 SetErrnoFromWinError (GetLastError ());
1311 CloseHandle ((*m)->h);
1320 if (access & GNUNET_DISK_MAP_READ)
1322 if (access & GNUNET_DISK_MAP_WRITE)
1324 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1325 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1333 * @param h mapping handle
1334 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1337 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1343 return GNUNET_SYSERR;
1347 ret = UnmapViewOfFile (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1348 if (ret != GNUNET_OK)
1349 SetErrnoFromWinError (GetLastError ());
1350 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1352 ret = GNUNET_SYSERR;
1353 SetErrnoFromWinError (GetLastError ());
1356 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1364 * Write file changes to disk
1365 * @param h handle to an open file
1366 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1369 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1374 return GNUNET_SYSERR;
1380 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1381 if (ret != GNUNET_OK)
1382 SetErrnoFromWinError (GetLastError ());
1385 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;