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?)
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,
182 GetFileSizeData gfsd;
185 GNUNET_assert (size != NULL);
187 gfsd.include_sym_links = includeSymLinks;
188 ret = getSizeRec (&gfsd, filename);
195 * Create an (empty) temporary file on disk.
197 * @param template component to use for the name;
198 * does NOT contain "XXXXXX" or "/tmp/".
199 * @return NULL on error, otherwise name of fresh
200 * file on disk in directory for temporary files
203 GNUNET_DISK_mktemp (const char *template)
210 tmpdir = getenv ("TMPDIR");
211 tmpdir = tmpdir ? tmpdir : "/tmp";
213 GNUNET_asprintf (&tmpl,
220 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
221 plibc_conv_to_win_path (tmpl, fn);
229 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
236 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
244 * Get the number of blocks that are left on the partition that
245 * contains the given file (for normal users).
247 * @param part a file on the partition to check
248 * @return -1 on errors, otherwise the number of free blocks
251 GNUNET_DISK_get_blocks_available (const char *part)
256 if (0 != statvfs (part, &buf))
258 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
268 path = GNUNET_STRINGS_filename_expand (part);
269 memcpy (szDrive, path, 3);
272 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
275 _("`%s' failed for drive `%s': %u\n"),
276 "GetDiskFreeSpace", szDrive, GetLastError ());
283 if (0 != statfs (part, &s))
285 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
293 * Test if fil is a directory.
295 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
299 GNUNET_DISK_directory_test (const char *fil)
301 struct stat filestat;
304 ret = STAT (fil, &filestat);
309 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
310 return GNUNET_SYSERR;
314 if (!S_ISDIR (filestat.st_mode))
316 if (ACCESS (fil, R_OK | X_OK) < 0)
318 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil);
319 return GNUNET_SYSERR;
325 * Check that fil corresponds to a filename
326 * (of a file that exists and that is not a directory).
327 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
328 * else (will print an error message in that case, too).
331 GNUNET_DISK_file_test (const char *fil)
333 struct stat filestat;
337 rdir = GNUNET_STRINGS_filename_expand (fil);
339 return GNUNET_SYSERR;
341 ret = STAT (rdir, &filestat);
346 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
348 return GNUNET_SYSERR;
353 if (!S_ISREG (filestat.st_mode))
358 if (ACCESS (rdir, R_OK) < 0)
360 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
362 return GNUNET_SYSERR;
369 * Implementation of "mkdir -p"
370 * @param dir the directory to create
371 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure
374 GNUNET_DISK_directory_create (const char *dir)
381 rdir = GNUNET_STRINGS_filename_expand (dir);
383 return GNUNET_SYSERR;
387 pos = 1; /* skip heading '/' */
389 /* Local or Network path? */
390 if (strncmp (rdir, "\\\\", 2) == 0)
395 if (rdir[pos] == '\\')
405 pos = 3; /* strlen("C:\\") */
410 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
413 ret = GNUNET_DISK_directory_test (rdir);
414 if (ret == GNUNET_SYSERR)
417 return GNUNET_SYSERR;
419 if (ret == GNUNET_NO)
422 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
426 if ((ret != 0) && (errno != EEXIST))
428 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir",
431 return GNUNET_SYSERR;
434 rdir[pos] = DIR_SEPARATOR;
444 * Create the directory structure for storing
447 * @param filename name of a file in the directory
448 * @returns GNUNET_OK on success,
449 * GNUNET_SYSERR on failure,
450 * GNUNET_NO if the directory
451 * exists but is not writeable for us
454 GNUNET_DISK_directory_create_for_file (const char *dir)
460 rdir = GNUNET_STRINGS_filename_expand (dir);
462 return GNUNET_SYSERR;
464 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
467 ret = GNUNET_DISK_directory_create (rdir);
468 if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
475 * Read the contents of a binary file into a buffer.
476 * @param h handle to an open file
477 * @param result the buffer to write the result to
478 * @param len the maximum number of bytes to read
479 * @return the number of bytes read on success, GNUNET_SYSERR on failure
482 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
488 return GNUNET_SYSERR;
494 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
496 SetErrnoFromWinError (GetLastError ());
497 return GNUNET_SYSERR;
501 return read (h->fd, result, len);
507 * Read the contents of a binary file into a buffer.
509 * @param fn file name
510 * @param result the buffer to write the result to
511 * @param len the maximum number of bytes to read
512 * @return number of bytes read, GNUNET_SYSERR on failure
515 GNUNET_DISK_fn_read (const char * const fn,
519 struct GNUNET_DISK_FileHandle *fh;
522 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
524 return GNUNET_SYSERR;
525 ret = GNUNET_DISK_file_read (fh, result, len);
526 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
533 * Write a buffer to a file.
534 * @param h handle to open file
535 * @param buffer the data to write
536 * @param n number of bytes to write
537 * @return number of bytes written on success, GNUNET_SYSERR on error
540 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buffer,
546 return GNUNET_SYSERR;
552 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
554 SetErrnoFromWinError (GetLastError ());
555 return GNUNET_SYSERR;
559 return write (h->fd, buffer, n);
564 * Write a buffer to a file. If the file is longer than the
565 * number of bytes that will be written, iit will be truncated.
567 * @param fn file name
568 * @param buffer the data to write
569 * @param n number of bytes to write
570 * @return GNUNET_OK on success, GNUNET_SYSERR on error
573 GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
576 struct GNUNET_DISK_FileHandle *fh;
579 fh = GNUNET_DISK_file_open (fn,
580 GNUNET_DISK_OPEN_WRITE
581 | GNUNET_DISK_OPEN_TRUNCATE
582 | GNUNET_DISK_OPEN_CREATE, mode);
584 return GNUNET_SYSERR;
585 ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR;
586 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
592 * Scan a directory for files. The name of the directory
593 * must be expanded first (!).
594 * @param dirName the name of the directory
595 * @param callback the method to call for each file,
596 * can be NULL, in that case, we only count
597 * @param data argument to pass to callback
598 * @return the number of files found, GNUNET_SYSERR on error or
599 * ieration aborted by callback returning GNUNET_SYSERR
602 GNUNET_DISK_directory_scan (const char *dirName,
603 GNUNET_FileNameCallback callback, void *data)
606 struct dirent *finfo;
611 unsigned int name_len;
614 GNUNET_assert (dirName != NULL);
615 dname = GNUNET_STRINGS_filename_expand (dirName);
616 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
617 dname[strlen (dname) - 1] = '\0';
618 if (0 != STAT (dname, &istat))
620 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
622 return GNUNET_SYSERR;
624 if (!S_ISDIR (istat.st_mode))
626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
627 _("Expected `%s' to be a directory!\n"), dirName);
629 return GNUNET_SYSERR;
632 dinfo = OPENDIR (dname);
633 if ((errno == EACCES) || (dinfo == NULL))
635 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
639 return GNUNET_SYSERR;
642 n_size = strlen (dname) + name_len + 2;
643 name = GNUNET_malloc (n_size);
644 while ((finfo = readdir (dinfo)) != NULL)
646 if ((0 == strcmp (finfo->d_name, ".")) ||
647 (0 == strcmp (finfo->d_name, "..")))
649 if (callback != NULL)
651 if (name_len < strlen (finfo->d_name))
654 name_len = strlen (finfo->d_name);
655 n_size = strlen (dname) + name_len + 2;
656 name = GNUNET_malloc (n_size);
658 /* dname can end in "/" only if dname == "/";
659 if dname does not end in "/", we need to add
660 a "/" (otherwise, we must not!) */
661 GNUNET_snprintf (name,
665 (strcmp (dname, DIR_SEPARATOR_STR) ==
666 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
667 if (GNUNET_OK != callback (data, name))
672 return GNUNET_SYSERR;
685 * Opaque handle used for iterating over a directory.
687 struct GNUNET_DISK_DirectoryIterator
692 struct GNUNET_SCHEDULER_Handle *sched;
695 * Function to call on directory entries.
697 GNUNET_DISK_DirectoryIteratorCallback callback;
700 * Closure for callback.
705 * Reference to directory.
715 * Next filename to process.
722 enum GNUNET_SCHEDULER_Priority priority;
728 * Task used by the directory iterator.
731 directory_iterator_task (void *cls,
732 const struct GNUNET_SCHEDULER_TaskContext *tc)
734 struct GNUNET_DISK_DirectoryIterator *iter = cls;
737 name = iter->next_name;
738 GNUNET_assert (name != NULL);
739 iter->next_name = NULL;
740 iter->callback (iter->callback_cls, iter, name, iter->dirname);
746 * This function must be called during the DiskIteratorCallback
747 * (exactly once) to schedule the task to process the next
748 * filename in the directory (if there is one).
750 * @param iter opaque handle for the iterator
751 * @param can set to GNUNET_YES to terminate the iteration early
752 * @return GNUNET_YES if iteration will continue,
753 * GNUNET_NO if this was the last entry (and iteration is complete),
754 * GNUNET_SYSERR if abort was YES
757 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
760 struct dirent *finfo;
762 GNUNET_assert (iter->next_name == NULL);
763 if (can == GNUNET_YES)
765 closedir (iter->directory);
766 GNUNET_free (iter->dirname);
768 return GNUNET_SYSERR;
770 while (NULL != (finfo = readdir (iter->directory)))
772 if ((0 == strcmp (finfo->d_name, ".")) ||
773 (0 == strcmp (finfo->d_name, "..")))
775 GNUNET_asprintf (&iter->next_name,
777 iter->dirname, DIR_SEPARATOR_STR, finfo->d_name);
782 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
785 GNUNET_SCHEDULER_add_after (iter->sched,
788 GNUNET_SCHEDULER_NO_TASK,
789 &directory_iterator_task, iter);
795 * Scan a directory for files using the scheduler to run a task for
796 * each entry. The name of the directory must be expanded first (!).
797 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
798 * may provide a simpler API.
800 * @param sched scheduler to use
801 * @param prio priority to use
802 * @param dirName the name of the directory
803 * @param callback the method to call for each file
804 * @param callback_cls closure for callback
807 GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
808 enum GNUNET_SCHEDULER_Priority prio,
810 GNUNET_DISK_DirectoryIteratorCallback
811 callback, void *callback_cls)
813 struct GNUNET_DISK_DirectoryIterator *di;
815 di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
817 di->callback = callback;
818 di->callback_cls = callback_cls;
819 di->directory = OPENDIR (dirName);
820 di->dirname = GNUNET_strdup (dirName);
822 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
827 remove_helper (void *unused, const char *fn)
829 GNUNET_DISK_directory_remove (fn);
834 * Remove all files in a directory (rm -rf). Call with
838 * @param fileName the file to remove
839 * @return GNUNET_OK on success, GNUNET_SYSERR on error
842 GNUNET_DISK_directory_remove (const char *fileName)
846 if (0 != LSTAT (fileName, &istat))
847 return GNUNET_NO; /* file may not exist... */
848 if (UNLINK (fileName) == 0)
850 if ((errno != EISDIR) &&
851 /* EISDIR is not sufficient in all cases, e.g.
852 sticky /tmp directory may result in EPERM on BSD.
853 So we also explicitly check "isDirectory" */
854 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
856 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
857 return GNUNET_SYSERR;
860 GNUNET_DISK_directory_scan (fileName, remove_helper, NULL))
861 return GNUNET_SYSERR;
862 if (0 != RMDIR (fileName))
864 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
865 return GNUNET_SYSERR;
870 #define COPY_BLK_SIZE 65536
874 * @return GNUNET_OK on success, GNUNET_SYSERR on error
877 GNUNET_DISK_file_copy (const char *src, const char *dst)
880 unsigned long long pos;
881 unsigned long long size;
882 unsigned long long len;
883 struct GNUNET_DISK_FileHandle *in, *out;
885 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
886 return GNUNET_SYSERR;
888 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ);
890 return GNUNET_SYSERR;
891 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
892 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS,
893 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
894 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
897 GNUNET_DISK_file_close (in);
898 return GNUNET_SYSERR;
900 buf = GNUNET_malloc (COPY_BLK_SIZE);
904 if (len > size - pos)
906 if (len != GNUNET_DISK_file_read (in, buf, len))
908 if (len != GNUNET_DISK_file_write (out, buf, len))
913 GNUNET_DISK_file_close (in);
914 GNUNET_DISK_file_close (out);
918 GNUNET_DISK_file_close (in);
919 GNUNET_DISK_file_close (out);
920 return GNUNET_SYSERR;
925 * @brief Removes special characters as ':' from a filename.
926 * @param fn the filename to canonicalize
929 GNUNET_DISK_filename_canonicalize (char *fn)
939 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
940 c == '"' || c == '<' || c == '>' || c == '|')
952 * @brief Change owner of a file
955 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
960 pws = getpwnam (user);
963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
964 _("Cannot obtain information about user `%s': %s\n"),
965 user, STRERROR (errno));
966 return GNUNET_SYSERR;
968 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
969 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
976 * Lock a part of a file
977 * @param fh file handle
978 * @lockStart absolute position from where to lock
979 * @lockEnd absolute position until where to lock
980 * @return GNUNET_OK on success, GNUNET_SYSERR on error
983 GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
989 return GNUNET_SYSERR;
995 memset(&fl, 0, sizeof(struct flock));
997 fl.l_whence = SEEK_SET;
998 fl.l_start = lockStart;
1001 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1003 if (!LockFile(fh->h, 0, lockStart, 0, lockEnd))
1005 SetErrnoFromWinError(GetLastError());
1006 return GNUNET_SYSERR;
1016 * @param fn file name to be opened
1017 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1018 * @param perm permissions for the newly created file
1019 * @return IO handle on success, NULL on error
1021 struct GNUNET_DISK_FileHandle *
1022 GNUNET_DISK_file_open (const char *fn, int flags, ...)
1025 struct GNUNET_DISK_FileHandle *ret;
1036 expfn = GNUNET_STRINGS_filename_expand (fn);
1041 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1042 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1043 else if (flags & GNUNET_DISK_OPEN_READ)
1045 else if (flags & GNUNET_DISK_OPEN_WRITE)
1050 GNUNET_free (expfn);
1053 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1054 oflags |= (O_CREAT & O_EXCL);
1055 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1057 if (flags & GNUNET_DISK_OPEN_APPEND)
1059 if (flags & GNUNET_DISK_OPEN_CREATE)
1066 va_start (arg, flags);
1067 perm = va_arg (arg, int);
1070 if (perm & GNUNET_DISK_PERM_USER_READ)
1072 if (perm & GNUNET_DISK_PERM_USER_WRITE)
1074 if (perm & GNUNET_DISK_PERM_USER_EXEC)
1076 if (perm & GNUNET_DISK_PERM_GROUP_READ)
1078 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
1080 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
1082 if (perm & GNUNET_DISK_PERM_OTHER_READ)
1084 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
1086 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
1090 fd = open (expfn, oflags | O_LARGEFILE, mode);
1093 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1094 GNUNET_free (expfn);
1101 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1102 access = FILE_READ_DATA | FILE_WRITE_DATA;
1103 else if (flags & GNUNET_DISK_OPEN_READ)
1104 access = FILE_READ_DATA;
1105 else if (flags & GNUNET_DISK_OPEN_WRITE)
1106 access = FILE_WRITE_DATA;
1108 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1112 else if (flags & GNUNET_DISK_OPEN_CREATE)
1114 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1115 disp = CREATE_ALWAYS;
1119 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1121 disp = TRUNCATE_EXISTING;
1128 /* TODO: access priviledges? */
1129 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1130 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1131 if (h == INVALID_HANDLE_VALUE)
1133 SetErrnoFromWinError (GetLastError ());
1134 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1135 GNUNET_free (expfn);
1139 if (flags & GNUNET_DISK_OPEN_APPEND)
1140 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1142 SetErrnoFromWinError (GetLastError ());
1143 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1145 GNUNET_free (expfn);
1150 ret = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1156 GNUNET_free (expfn);
1161 * Close an open file
1162 * @param h file handle
1163 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1166 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1171 return GNUNET_SYSERR;
1175 if (!CloseHandle (h->h))
1177 SetErrnoFromWinError (GetLastError ());
1178 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1180 return GNUNET_SYSERR;
1183 if (close (h->fd) != 0)
1185 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1187 return GNUNET_SYSERR;
1195 * Construct full path to a file inside of the private
1196 * directory used by GNUnet. Also creates the corresponding
1197 * directory. If the resulting name is supposed to be
1198 * a directory, end the last argument in '/' (or pass
1199 * DIR_SEPARATOR_STR as the last argument before NULL).
1201 * @param cfg configuration to use (determines HOME)
1202 * @param serviceName name of the service
1203 * @param varargs is NULL-terminated list of
1204 * path components to append to the
1205 * private directory name.
1206 * @return the constructed filename
1209 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1210 const char *serviceName, ...)
1216 unsigned int needed;
1219 GNUNET_CONFIGURATION_get_value_filename (cfg,
1220 serviceName, "HOME", &pfx))
1224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1225 _("No `%s' specified for service `%s' in configuration.\n"),
1226 "HOME", serviceName);
1229 needed = strlen (pfx) + 2;
1230 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1232 va_start (ap, serviceName);
1235 c = va_arg (ap, const char *);
1238 needed += strlen (c);
1239 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1243 ret = GNUNET_malloc (needed);
1246 va_start (ap, serviceName);
1249 c = va_arg (ap, const char *);
1252 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1253 strcat (ret, DIR_SEPARATOR_STR);
1257 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1258 GNUNET_DISK_directory_create_for_file (ret);
1260 GNUNET_DISK_directory_create (ret);
1264 struct GNUNET_DISK_MapHandle
1276 * Map a file into memory
1277 * @param h open file handle
1278 * @param m handle to the new mapping
1279 * @param access access specification, GNUNET_DISK_MAP_xxx
1280 * @param len size of the mapping
1281 * @return pointer to the mapped memory region, NULL on failure
1284 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m,
1285 int access, size_t len)
1294 DWORD mapAccess, protect;
1297 if (access & GNUNET_DISK_MAP_READ && access & GNUNET_DISK_MAP_WRITE)
1299 protect = PAGE_READWRITE;
1300 mapAccess = FILE_MAP_ALL_ACCESS;
1302 else if (access & GNUNET_DISK_MAP_READ)
1304 protect = PAGE_READONLY;
1305 mapAccess = FILE_MAP_READ;
1307 else if (access & GNUNET_DISK_MAP_WRITE)
1309 protect = PAGE_READWRITE;
1310 mapAccess = FILE_MAP_WRITE;
1318 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1319 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1320 if ((*m)->h == INVALID_HANDLE_VALUE)
1322 SetErrnoFromWinError (GetLastError ());
1327 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1330 SetErrnoFromWinError (GetLastError ());
1331 CloseHandle ((*m)->h);
1340 if (access & GNUNET_DISK_MAP_READ)
1342 if (access & GNUNET_DISK_MAP_WRITE)
1344 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1345 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1353 * @param h mapping handle
1354 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1357 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1363 return GNUNET_SYSERR;
1367 ret = UnmapViewOfFile (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1368 if (ret != GNUNET_OK)
1369 SetErrnoFromWinError (GetLastError ());
1370 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1372 ret = GNUNET_SYSERR;
1373 SetErrnoFromWinError (GetLastError ());
1376 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1384 * Write file changes to disk
1385 * @param h handle to an open file
1386 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1389 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1394 return GNUNET_SYSERR;
1400 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1401 if (ret != GNUNET_OK)
1402 SetErrnoFromWinError (GetLastError ());
1405 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1410 * Creates an interprocess channel
1411 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
1412 * @return handle to the new pipe, NULL on error
1414 struct GNUNET_DISK_PipeHandle *
1415 GNUNET_DISK_pipe (int blocking)
1417 struct GNUNET_DISK_PipeHandle *p;
1421 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle));
1431 p->fd[0].fd = fd[0];
1432 p->fd[1].fd = fd[1];
1436 flags = fcntl (fd[0], F_GETFL);
1437 flags |= O_NONBLOCK;
1438 ret = fcntl (fd[0], F_SETFL, flags);
1441 flags = fcntl (fd[1], F_GETFL);
1442 flags |= O_NONBLOCK;
1443 ret = fcntl (fd[1], F_SETFL, flags);
1447 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl");
1459 ret = CreatePipe (&p->fd[0].h, &p->fd[1].h, NULL, 0);
1467 SetNamedPipeHandleState (p->fd[0].h, &mode, NULL, NULL);
1468 SetNamedPipeHandleState (p->fd[1].h, &mode, NULL, NULL);
1469 /* this always fails on Windows 95, so we don't care about error handling */
1476 if (GNUNET_YES == err)
1487 * Closes an interprocess channel
1489 * @param p pipe to close
1490 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1493 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1495 int ret = GNUNET_OK;
1497 if (!CloseHandle (p->fd[0].h))
1499 SetErrnoFromWinError (GetLastError ());
1500 ret = GNUNET_SYSERR;
1503 if (!CloseHandle (p->fd[1].h))
1505 SetErrnoFromWinError (GetLastError ());
1506 ret = GNUNET_SYSERR;
1511 if (0 != close (p->fd[0].fd))
1513 ret = GNUNET_SYSERR;
1519 if (0 != close (p->fd[1].fd))
1520 ret = GNUNET_SYSERR;
1530 * Get the handle to a particular pipe end
1532 * @param n number of the end
1534 const struct GNUNET_DISK_FileHandle *
1535 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, int n)
1541 * Retrieve OS file handle
1543 * @param fh GNUnet file descriptor
1544 * @param dst destination buffer
1545 * @param dst_len length of dst
1546 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1549 GNUNET_internal_disk_file_handle (const struct GNUNET_DISK_FileHandle *fh,
1550 void *dst, unsigned int dst_len)
1553 if (dst_len < sizeof (HANDLE))
1554 return GNUNET_SYSERR;
1555 *((HANDLE *) dst) = fh->h;
1557 if (dst_len < sizeof(int))
1558 return GNUNET_SYSERR;
1559 *((int *) dst) = fh->fd;