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"
41 #include <sys/param.h>
42 #include <sys/mount.h>
45 #include <sys/param.h>
46 #include <sys/mount.h>
49 #include <sys/types.h>
50 #include <sys/statvfs.h>
53 #define _IFMT 0170000 /* type of file */
54 #define _IFLNK 0120000 /* symbolic link */
55 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
57 #error PORT-ME: need to port statfs (how much space is left on the drive?)
74 unsigned long long total;
75 int include_sym_links;
78 struct GNUNET_DISK_PipeHandle
80 struct GNUNET_DISK_FileHandle fd[2];
84 getSizeRec (void *ptr, const char *fn)
86 GetFileSizeData *gfsd = ptr;
94 if (0 != STAT64 (fn, &buf))
96 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn);
100 if (0 != STAT (fn, &buf))
102 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn);
103 return GNUNET_SYSERR;
106 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
107 gfsd->total += buf.st_size;
108 if ((S_ISDIR (buf.st_mode)) &&
109 (0 == ACCESS (fn, X_OK)) &&
110 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
112 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
113 return GNUNET_SYSERR;
119 * Checks whether a handle is invalid
120 * @param h handle to check
121 * @return GNUNET_YES if invalid, GNUNET_NO if valid
124 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
127 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
129 return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO;
135 * Move the read/write pointer in a file
136 * @param h handle of an open file
137 * @param offset position to move to
138 * @param whence specification to which position the offset parameter relates to
139 * @return the new position on success, GNUNET_SYSERR otherwise
142 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
143 enum GNUNET_DISK_Seek whence)
148 return GNUNET_SYSERR;
153 static DWORD t[] = { [GNUNET_DISK_SEEK_SET] = FILE_BEGIN,
154 [GNUNET_DISK_SEEK_CUR] = FILE_CURRENT, [GNUNET_DISK_SEEK_END] = FILE_END };
156 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
157 if (ret == INVALID_SET_FILE_POINTER)
159 SetErrnoFromWinError (GetLastError ());
160 return GNUNET_SYSERR;
164 static int t[] = { [GNUNET_DISK_SEEK_SET] = SEEK_SET,
165 [GNUNET_DISK_SEEK_CUR] = SEEK_CUR, [GNUNET_DISK_SEEK_END] = SEEK_END };
167 return lseek (h->fd, offset, t[whence]);
172 * Get the size of the file (or directory)
173 * of the given file (in bytes).
175 * @return GNUNET_SYSERR on error, GNUNET_OK on success
178 GNUNET_DISK_file_size (const char *filename,
179 unsigned long long *size, int includeSymLinks)
181 GetFileSizeData gfsd;
184 GNUNET_assert (size != NULL);
186 gfsd.include_sym_links = includeSymLinks;
187 ret = getSizeRec (&gfsd, filename);
194 * Create an (empty) temporary file on disk.
196 * @param template component to use for the name;
197 * does NOT contain "XXXXXX" or "/tmp/".
198 * @return NULL on error, otherwise name of fresh
199 * file on disk in directory for temporary files
202 GNUNET_DISK_mktemp (const char *template)
209 tmpdir = getenv ("TMPDIR");
210 tmpdir = tmpdir ? tmpdir : "/tmp";
212 GNUNET_asprintf (&tmpl,
219 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
220 plibc_conv_to_win_path (tmpl, fn);
228 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
235 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
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)
1046 GNUNET_free (expfn);
1049 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1050 oflags |= (O_CREAT & O_EXCL);
1051 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1053 if (flags & GNUNET_DISK_OPEN_APPEND)
1055 if (flags & GNUNET_DISK_OPEN_CREATE)
1062 va_start (arg, flags);
1063 perm = va_arg (arg, int);
1066 if (perm & GNUNET_DISK_PERM_USER_READ)
1068 if (perm & GNUNET_DISK_PERM_USER_WRITE)
1070 if (perm & GNUNET_DISK_PERM_USER_EXEC)
1072 if (perm & GNUNET_DISK_PERM_GROUP_READ)
1074 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
1076 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
1078 if (perm & GNUNET_DISK_PERM_OTHER_READ)
1080 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
1082 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
1086 fd = open (expfn, oflags | O_LARGEFILE, mode);
1089 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1090 GNUNET_free (expfn);
1097 if (flags & GNUNET_DISK_OPEN_READ)
1098 access = FILE_READ_DATA;
1099 if (flags & GNUNET_DISK_OPEN_WRITE)
1100 access = FILE_WRITE_DATA;
1102 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1104 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1105 disp = TRUNCATE_EXISTING;
1106 if (flags & GNUNET_DISK_OPEN_CREATE)
1107 disp |= OPEN_ALWAYS;
1109 /* TODO: access priviledges? */
1110 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1111 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1112 if (h == INVALID_HANDLE_VALUE)
1114 SetErrnoFromWinError (GetLastError ());
1115 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1116 GNUNET_free (expfn);
1120 if (flags & GNUNET_DISK_OPEN_APPEND)
1121 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1123 SetErrnoFromWinError (GetLastError ());
1124 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1126 GNUNET_free (expfn);
1131 ret = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1137 GNUNET_free (expfn);
1142 * Close an open file
1143 * @param h file handle
1144 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1147 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1152 return GNUNET_SYSERR;
1156 if (!CloseHandle (h->h))
1158 SetErrnoFromWinError (GetLastError ());
1159 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1161 return GNUNET_SYSERR;
1164 if (close (h->fd) != 0)
1166 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1168 return GNUNET_SYSERR;
1176 * Construct full path to a file inside of the private
1177 * directory used by GNUnet. Also creates the corresponding
1178 * directory. If the resulting name is supposed to be
1179 * a directory, end the last argument in '/' (or pass
1180 * DIR_SEPARATOR_STR as the last argument before NULL).
1182 * @param cfg configuration to use (determines HOME)
1183 * @param serviceName name of the service
1184 * @param varargs is NULL-terminated list of
1185 * path components to append to the
1186 * private directory name.
1187 * @return the constructed filename
1190 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1191 const char *serviceName, ...)
1197 unsigned int needed;
1200 GNUNET_CONFIGURATION_get_value_filename (cfg,
1201 serviceName, "HOME", &pfx))
1205 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1206 _("No `%s' specified for service `%s' in configuration.\n"),
1207 "HOME", serviceName);
1210 needed = strlen (pfx) + 2;
1211 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1213 va_start (ap, serviceName);
1216 c = va_arg (ap, const char *);
1219 needed += strlen (c);
1220 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1224 ret = GNUNET_malloc (needed);
1227 va_start (ap, serviceName);
1230 c = va_arg (ap, const char *);
1233 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1234 strcat (ret, DIR_SEPARATOR_STR);
1238 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1239 GNUNET_DISK_directory_create_for_file (ret);
1241 GNUNET_DISK_directory_create (ret);
1245 struct GNUNET_DISK_MapHandle
1257 * Map a file into memory
1258 * @param h open file handle
1259 * @param m handle to the new mapping
1260 * @param access access specification, GNUNET_DISK_MAP_xxx
1261 * @param len size of the mapping
1262 * @return pointer to the mapped memory region, NULL on failure
1265 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m,
1266 int access, size_t len)
1275 DWORD mapAccess, protect;
1278 if (access & GNUNET_DISK_MAP_READ && access & GNUNET_DISK_MAP_WRITE)
1280 protect = PAGE_READWRITE;
1281 mapAccess = FILE_MAP_ALL_ACCESS;
1283 else if (access & GNUNET_DISK_MAP_READ)
1285 protect = PAGE_READONLY;
1286 mapAccess = FILE_MAP_READ;
1288 else if (access & GNUNET_DISK_MAP_WRITE)
1290 protect = PAGE_READWRITE;
1291 mapAccess = FILE_MAP_WRITE;
1299 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1300 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1301 if ((*m)->h == INVALID_HANDLE_VALUE)
1303 SetErrnoFromWinError (GetLastError ());
1308 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1311 SetErrnoFromWinError (GetLastError ());
1312 CloseHandle ((*m)->h);
1321 if (access & GNUNET_DISK_MAP_READ)
1323 if (access & GNUNET_DISK_MAP_WRITE)
1325 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1326 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1334 * @param h mapping handle
1335 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1338 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1344 return GNUNET_SYSERR;
1348 ret = UnmapViewOfFile (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1349 if (ret != GNUNET_OK)
1350 SetErrnoFromWinError (GetLastError ());
1351 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1353 ret = GNUNET_SYSERR;
1354 SetErrnoFromWinError (GetLastError ());
1357 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1365 * Write file changes to disk
1366 * @param h handle to an open file
1367 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1370 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1375 return GNUNET_SYSERR;
1381 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1382 if (ret != GNUNET_OK)
1383 SetErrnoFromWinError (GetLastError ());
1386 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1391 * Creates an interprocess channel
1392 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
1393 * @return handle to the new pipe, NULL on error
1395 struct GNUNET_DISK_PipeHandle *
1396 GNUNET_DISK_pipe (int blocking)
1398 struct GNUNET_DISK_PipeHandle *p;
1402 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle));
1412 p->fd[0].fd = fd[0];
1413 p->fd[1].fd = fd[1];
1417 flags = fcntl (fd[0], F_GETFL);
1418 flags |= O_NONBLOCK;
1419 ret = fcntl (fd[0], F_SETFL, flags);
1422 flags = fcntl (fd[1], F_GETFL);
1423 flags |= O_NONBLOCK;
1424 ret = fcntl (fd[1], F_SETFL, flags);
1428 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl");
1440 ret = CreatePipe (&p->fd[0].h, &p->fd[1].h, NULL, 0);
1448 SetNamedPipeHandleState (p->fd[0].h, &mode, NULL, NULL);
1449 SetNamedPipeHandleState (p->fd[1].h, &mode, NULL, NULL);
1450 /* this always fails on Windows 95, so we don't care about error handling */
1457 if (GNUNET_YES == err)
1467 * Closes an interprocess channel
1469 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1472 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1479 if (!CloseHandle (p->fd[0].h))
1481 SetErrnoFromWinError (GetLastError ());
1482 ret = GNUNET_SYSERR;
1485 if (!CloseHandle (p->fd[1].h))
1487 SetErrnoFromWinError (GetLastError ());
1488 ret = GNUNET_SYSERR;
1494 if (close (p->fd[0].fd) != -1)
1496 ret = GNUNET_SYSERR;
1502 if (close (p->fd[1].fd) != -1)
1504 ret = GNUNET_SYSERR;
1516 * Get the handle to a particular pipe end
1518 * @param n number of the end
1520 const struct GNUNET_DISK_FileHandle *
1521 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, int n)
1527 * Retrieve OS file handle
1529 * @param fh GNUnet file descriptor
1530 * @param dst destination buffer
1531 * @param dst_len length of dst
1532 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1535 GNUNET_internal_disk_file_handle (const struct GNUNET_DISK_FileHandle *fh,
1536 void *dst, unsigned int dst_len)
1539 if (dst_len < sizeof (HANDLE))
1540 return GNUNET_SYSERR;
1541 *((HANDLE *) dst) = fh->h;
1543 if (dst_len < sizeof(int))
1544 return GNUNET_SYSERR;
1545 *((int *) dst) = fh->fd;