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_IO_Handle
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_IO_Handle *h)
130 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
132 return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO;
137 * Mark a handle as invalid
138 * @param h file handle
141 GNUNET_DISK_handle_invalidate (struct GNUNET_IO_Handle *h)
144 h->h = INVALID_HANDLE_VALUE;
151 * Move the read/write pointer in a file
152 * @param h handle of an open file
153 * @param offset position to move to
154 * @param whence specification to which position the offset parameter relates to
155 * @return the new position on success, GNUNET_SYSERR otherwise
158 GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset,
159 enum GNUNET_DISK_Seek whence)
164 return GNUNET_SYSERR;
169 static DWORD t[] = { [GNUNET_SEEK_SET] = FILE_BEGIN,
170 [GNUNET_SEEK_CUR] = FILE_CURRENT, [GNUNET_SEEK_END] = FILE_END };
172 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
173 if (ret == INVALID_SET_FILE_POINTER)
175 SetErrnoFromWinError (GetLastError ());
176 return GNUNET_SYSERR;
180 static int t[] = { [GNUNET_SEEK_SET] = SEEK_SET,
181 [GNUNET_SEEK_CUR] = SEEK_CUR, [GNUNET_SEEK_END] = SEEK_END };
183 return lseek (h->fd, offset, t[whence]);
188 * Get the size of the file (or directory)
189 * of the given file (in bytes).
191 * @return GNUNET_SYSERR on error, GNUNET_OK on success
194 GNUNET_DISK_file_size (const char *filename,
195 unsigned long long *size, int includeSymLinks)
197 GetFileSizeData gfsd;
200 GNUNET_assert (size != NULL);
202 gfsd.include_sym_links = includeSymLinks;
203 ret = getSizeRec (&gfsd, filename);
210 * Get the number of blocks that are left on the partition that
211 * contains the given file (for normal users).
213 * @param part a file on the partition to check
214 * @return -1 on errors, otherwise the number of free blocks
217 GNUNET_DISK_get_blocks_available (const char *part)
222 if (0 != statvfs (part, &buf))
224 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
233 memcpy (szDrive, part, 3);
235 if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
238 _("`%s' failed for drive `%s': %u\n"),
239 "GetDiskFreeSpace", szDrive, GetLastError ());
246 if (0 != statfs (part, &s))
248 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
256 * Test if fil is a directory.
258 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
262 GNUNET_DISK_directory_test (const char *fil)
264 struct stat filestat;
267 ret = STAT (fil, &filestat);
272 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
273 return GNUNET_SYSERR;
277 if (!S_ISDIR (filestat.st_mode))
279 if (ACCESS (fil, R_OK | X_OK) < 0)
281 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil);
282 return GNUNET_SYSERR;
288 * Check that fil corresponds to a filename
289 * (of a file that exists and that is not a directory).
290 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
291 * else (will print an error message in that case, too).
294 GNUNET_DISK_file_test (const char *fil)
296 struct stat filestat;
300 rdir = GNUNET_STRINGS_filename_expand (fil);
302 return GNUNET_SYSERR;
304 ret = STAT (rdir, &filestat);
309 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
311 return GNUNET_SYSERR;
316 if (!S_ISREG (filestat.st_mode))
321 if (ACCESS (rdir, R_OK) < 0)
323 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
325 return GNUNET_SYSERR;
332 * Implementation of "mkdir -p"
333 * @param dir the directory to create
334 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure
337 GNUNET_DISK_directory_create (const char *dir)
344 rdir = GNUNET_STRINGS_filename_expand (dir);
346 return GNUNET_SYSERR;
350 pos = 1; /* skip heading '/' */
352 /* Local or Network path? */
353 if (strncmp (rdir, "\\\\", 2) == 0)
358 if (rdir[pos] == '\\')
368 pos = 3; /* strlen("C:\\") */
373 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
376 ret = GNUNET_DISK_directory_test (rdir);
377 if (ret == GNUNET_SYSERR)
380 return GNUNET_SYSERR;
382 if (ret == GNUNET_NO)
385 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
389 if ((ret != 0) && (errno != EEXIST))
391 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir",
394 return GNUNET_SYSERR;
397 rdir[pos] = DIR_SEPARATOR;
407 * Create the directory structure for storing
410 * @param filename name of a file in the directory
411 * @returns GNUNET_OK on success,
412 * GNUNET_SYSERR on failure,
413 * GNUNET_NO if the directory
414 * exists but is not writeable for us
417 GNUNET_DISK_directory_create_for_file (const char *dir)
423 rdir = GNUNET_STRINGS_filename_expand (dir);
425 return GNUNET_SYSERR;
427 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
430 ret = GNUNET_DISK_directory_create (rdir);
431 if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
438 * Read the contents of a binary file into a buffer.
439 * @param h handle to an open file
440 * @param result the buffer to write the result to
441 * @param len the maximum number of bytes to read
442 * @return the number of bytes read on success, GNUNET_SYSERR on failure
445 GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len)
450 return GNUNET_SYSERR;
456 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
458 SetErrnoFromWinError (GetLastError ());
459 return GNUNET_SYSERR;
463 return read (h->fd, result, len);
469 * Read the contents of a binary file into a buffer.
470 * @param fn file name
471 * @param result the buffer to write the result to
472 * @param len the maximum number of bytes to read
473 * @return number of bytes read, GNUNET_SYSERR on failure
476 GNUNET_DISK_fn_read (const char * const fn, void *result, int len)
478 struct GNUNET_IO_Handle *fh;
481 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
483 return GNUNET_SYSERR;
484 ret = GNUNET_DISK_file_read (fh, result, len);
485 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
492 * Write a buffer to a file.
493 * @param h handle to open file
494 * @param buffer the data to write
495 * @param n number of bytes to write
496 * @return number of bytes written on success, GNUNET_SYSERR on error
499 GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer,
505 return GNUNET_SYSERR;
511 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
513 SetErrnoFromWinError (GetLastError ());
514 return GNUNET_SYSERR;
518 return write (h->fd, buffer, n);
523 * Write a buffer to a file.
524 * @param fn file name
525 * @param buffer the data to write
526 * @param n number of bytes to write
527 * @return GNUNET_OK on success, GNUNET_SYSERR on error
530 GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
531 unsigned int n, int mode)
533 struct GNUNET_IO_Handle *fh;
536 fh = GNUNET_DISK_file_open (fn,
537 GNUNET_DISK_OPEN_WRITE
538 | GNUNET_DISK_OPEN_TRUNCATE
539 | GNUNET_DISK_OPEN_CREATE, mode);
541 return GNUNET_SYSERR;
542 ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR;
543 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
549 * Scan a directory for files. The name of the directory
550 * must be expanded first (!).
551 * @param dirName the name of the directory
552 * @param callback the method to call for each file,
553 * can be NULL, in that case, we only count
554 * @param data argument to pass to callback
555 * @return the number of files found, GNUNET_SYSERR on error or
556 * ieration aborted by callback returning GNUNET_SYSERR
559 GNUNET_DISK_directory_scan (const char *dirName,
560 GNUNET_FileNameCallback callback, void *data)
563 struct dirent *finfo;
568 unsigned int name_len;
571 GNUNET_assert (dirName != NULL);
572 dname = GNUNET_STRINGS_filename_expand (dirName);
573 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
574 dname[strlen (dname) - 1] = '\0';
575 if (0 != STAT (dname, &istat))
577 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
579 return GNUNET_SYSERR;
581 if (!S_ISDIR (istat.st_mode))
583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
584 _("Expected `%s' to be a directory!\n"), dirName);
586 return GNUNET_SYSERR;
589 dinfo = OPENDIR (dname);
590 if ((errno == EACCES) || (dinfo == NULL))
592 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
596 return GNUNET_SYSERR;
599 n_size = strlen (dname) + name_len + 2;
600 name = GNUNET_malloc (n_size);
601 while ((finfo = readdir (dinfo)) != NULL)
603 if ((0 == strcmp (finfo->d_name, ".")) ||
604 (0 == strcmp (finfo->d_name, "..")))
606 if (callback != NULL)
608 if (name_len < strlen (finfo->d_name))
611 name_len = strlen (finfo->d_name);
612 n_size = strlen (dname) + name_len + 2;
613 name = GNUNET_malloc (n_size);
615 /* dname can end in "/" only if dname == "/";
616 if dname does not end in "/", we need to add
617 a "/" (otherwise, we must not!) */
618 GNUNET_snprintf (name,
622 (strcmp (dname, DIR_SEPARATOR_STR) ==
623 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
624 if (GNUNET_OK != callback (data, name))
629 return GNUNET_SYSERR;
642 * Opaque handle used for iterating over a directory.
644 struct GNUNET_DISK_DirectoryIterator
649 struct GNUNET_SCHEDULER_Handle *sched;
652 * Function to call on directory entries.
654 GNUNET_DISK_DirectoryIteratorCallback callback;
657 * Closure for callback.
662 * Reference to directory.
672 * Next filename to process.
679 enum GNUNET_SCHEDULER_Priority priority;
685 * Task used by the directory iterator.
688 directory_iterator_task (void *cls,
689 const struct GNUNET_SCHEDULER_TaskContext *tc)
691 struct GNUNET_DISK_DirectoryIterator *iter = cls;
694 name = iter->next_name;
695 GNUNET_assert (name != NULL);
696 iter->next_name = NULL;
697 iter->callback (iter->callback_cls, iter, name, iter->dirname);
703 * This function must be called during the DiskIteratorCallback
704 * (exactly once) to schedule the task to process the next
705 * filename in the directory (if there is one).
707 * @param iter opaque handle for the iterator
708 * @param can set to GNUNET_YES to terminate the iteration early
709 * @return GNUNET_YES if iteration will continue,
710 * GNUNET_NO if this was the last entry (and iteration is complete),
711 * GNUNET_SYSERR if abort was YES
714 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
717 struct dirent *finfo;
719 GNUNET_assert (iter->next_name == NULL);
720 if (can == GNUNET_YES)
722 closedir (iter->directory);
723 GNUNET_free (iter->dirname);
725 return GNUNET_SYSERR;
727 while (NULL != (finfo = readdir (iter->directory)))
729 if ((0 == strcmp (finfo->d_name, ".")) ||
730 (0 == strcmp (finfo->d_name, "..")))
732 GNUNET_asprintf (&iter->next_name,
734 iter->dirname, DIR_SEPARATOR_STR, finfo->d_name);
739 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
742 GNUNET_SCHEDULER_add_after (iter->sched,
745 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
746 &directory_iterator_task, iter);
752 * Scan a directory for files using the scheduler to run a task for
753 * each entry. The name of the directory must be expanded first (!).
754 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
755 * may provide a simpler API.
757 * @param sched scheduler to use
758 * @param prio priority to use
759 * @param dirName the name of the directory
760 * @param callback the method to call for each file
761 * @param callback_cls closure for callback
764 GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
765 enum GNUNET_SCHEDULER_Priority prio,
767 GNUNET_DISK_DirectoryIteratorCallback
768 callback, void *callback_cls)
770 struct GNUNET_DISK_DirectoryIterator *di;
772 di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
774 di->callback = callback;
775 di->callback_cls = callback_cls;
776 di->directory = OPENDIR (dirName);
777 di->dirname = GNUNET_strdup (dirName);
779 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
784 remove_helper (void *unused, const char *fn)
786 GNUNET_DISK_directory_remove (fn);
791 * Remove all files in a directory (rm -rf). Call with
795 * @param fileName the file to remove
796 * @return GNUNET_OK on success, GNUNET_SYSERR on error
799 GNUNET_DISK_directory_remove (const char *fileName)
803 if (0 != LSTAT (fileName, &istat))
804 return GNUNET_NO; /* file may not exist... */
805 if (UNLINK (fileName) == 0)
807 if ((errno != EISDIR) &&
808 /* EISDIR is not sufficient in all cases, e.g.
809 sticky /tmp directory may result in EPERM on BSD.
810 So we also explicitly check "isDirectory" */
811 (GNUNET_YES != GNUNET_DISK_directory_test (fileName)))
813 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
814 return GNUNET_SYSERR;
817 GNUNET_DISK_directory_scan (fileName, remove_helper, NULL))
818 return GNUNET_SYSERR;
819 if (0 != RMDIR (fileName))
821 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName);
822 return GNUNET_SYSERR;
827 #define COPY_BLK_SIZE 65536
831 * @return GNUNET_OK on success, GNUNET_SYSERR on error
834 GNUNET_DISK_file_copy (const char *src, const char *dst)
837 unsigned long long pos;
838 unsigned long long size;
839 unsigned long long len;
840 struct GNUNET_IO_Handle *in, *out;
842 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
843 return GNUNET_SYSERR;
845 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ);
847 return GNUNET_SYSERR;
848 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
849 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS,
850 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
851 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
854 GNUNET_DISK_file_close (&in);
855 return GNUNET_SYSERR;
857 buf = GNUNET_malloc (COPY_BLK_SIZE);
861 if (len > size - pos)
863 if (len != GNUNET_DISK_file_read (in, buf, len))
865 if (len != GNUNET_DISK_file_write (out, buf, len))
870 GNUNET_DISK_file_close (&in);
871 GNUNET_DISK_file_close (&out);
875 GNUNET_DISK_file_close (&in);
876 GNUNET_DISK_file_close (&out);
877 return GNUNET_SYSERR;
882 * @brief Removes special characters as ':' from a filename.
883 * @param fn the filename to canonicalize
886 GNUNET_DISK_filename_canonicalize (char *fn)
896 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
897 c == '"' || c == '<' || c == '>' || c == '|')
909 * @brief Change owner of a file
912 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
917 pws = getpwnam (user);
920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
921 _("Cannot obtain information about user `%s': %s\n"),
922 user, STRERROR (errno));
923 return GNUNET_SYSERR;
925 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
926 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
933 * Lock a part of a file
934 * @param fh file handle
935 * @lockStart absolute position from where to lock
936 * @lockEnd absolute position until where to lock
937 * @return GNUNET_OK on success, GNUNET_SYSERR on error
940 GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart,
946 return GNUNET_SYSERR;
952 memset(&fl, 0, sizeof(struct flock));
954 fl.l_whence = SEEK_SET;
955 fl.l_start = lockStart;
958 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
960 if (!LockFile(fh->h, 0, lockStart, 0, lockEnd))
962 SetErrnoFromWinError(GetLastError());
963 return GNUNET_SYSERR;
973 * @param fn file name to be opened
974 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
975 * @param perm permissions for the newly created file
976 * @return IO handle on success, NULL on error
978 struct GNUNET_IO_Handle *
979 GNUNET_DISK_file_open (const char *fn, int flags, ...)
982 struct GNUNET_IO_Handle *ret;
993 expfn = GNUNET_STRINGS_filename_expand (fn);
998 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
999 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1000 else if (flags & GNUNET_DISK_OPEN_READ)
1002 else if (flags & GNUNET_DISK_OPEN_WRITE)
1009 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1010 oflags |= (O_CREAT & O_EXCL);
1011 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1013 if (flags & GNUNET_DISK_OPEN_APPEND)
1015 if (flags & GNUNET_DISK_OPEN_CREATE)
1022 va_start (arg, flags);
1023 perm = va_arg (arg, int);
1026 if (perm & GNUNET_DISK_PERM_USER_READ)
1028 if (perm & GNUNET_DISK_PERM_USER_WRITE)
1030 if (perm & GNUNET_DISK_PERM_USER_EXEC)
1032 if (perm & GNUNET_DISK_PERM_GROUP_READ)
1034 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
1036 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
1038 if (perm & GNUNET_DISK_PERM_OTHER_READ)
1040 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
1042 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
1046 fd = open (expfn, oflags | O_LARGEFILE, mode);
1049 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
1056 if (flags & GNUNET_DISK_OPEN_READ)
1057 access = FILE_READ_DATA;
1058 if (flags & GNUNET_DISK_OPEN_WRITE)
1059 access = FILE_WRITE_DATA;
1061 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1063 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1064 disp = TRUNCATE_EXISTING;
1065 if (flags & GNUNET_DISK_OPEN_CREATE)
1066 disp |= OPEN_ALWAYS;
1068 /* TODO: access priviledges? */
1069 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1070 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1071 if (h == INVALID_HANDLE_VALUE)
1073 SetErrnoFromWinError (GetLastError ());
1074 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
1078 if (flags & GNUNET_DISK_OPEN_APPEND)
1079 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1081 SetErrnoFromWinError (GetLastError ());
1082 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", fn);
1088 ret = GNUNET_malloc(sizeof(struct GNUNET_IO_Handle));
1099 * Close an open file
1100 * @param h file handle
1101 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1104 GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h)
1109 return GNUNET_SYSERR;
1113 if (!CloseHandle ((*h)->h))
1115 SetErrnoFromWinError (GetLastError ());
1116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1117 return GNUNET_SYSERR;
1120 if (close ((*h)->fd) != 0)
1122 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1123 return GNUNET_SYSERR;
1127 GNUNET_DISK_handle_invalidate (*h);
1135 * Construct full path to a file inside of the private
1136 * directory used by GNUnet. Also creates the corresponding
1137 * directory. If the resulting name is supposed to be
1138 * a directory, end the last argument in '/' (or pass
1139 * DIR_SEPARATOR_STR as the last argument before NULL).
1141 * @param serviceName name of the service
1142 * @param varargs is NULL-terminated list of
1143 * path components to append to the
1144 * private directory name.
1145 * @return the constructed filename
1148 GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1149 const char *serviceName, ...)
1155 unsigned int needed;
1158 GNUNET_CONFIGURATION_get_value_filename (cfg,
1159 serviceName, "HOME", &pfx))
1163 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1164 _("No `%s' specified for service `%s' in configuration.\n"),
1165 "HOME", serviceName);
1168 needed = strlen (pfx) + 2;
1169 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
1171 va_start (ap, serviceName);
1174 c = va_arg (ap, const char *);
1177 needed += strlen (c);
1178 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1182 ret = GNUNET_malloc (needed);
1185 va_start (ap, serviceName);
1188 c = va_arg (ap, const char *);
1191 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
1192 strcat (ret, DIR_SEPARATOR_STR);
1196 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
1197 GNUNET_DISK_directory_create_for_file (ret);
1199 GNUNET_DISK_directory_create (ret);
1204 * Map a file into memory
1205 * @param h open file handle
1206 * @param m handle to the new mapping
1207 * @param access access specification, GNUNET_DISK_MAP_xxx
1208 * @param len size of the mapping
1209 * @return pointer to the mapped memory region, NULL on failure
1212 GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m,
1213 int access, size_t len)
1222 DWORD mapAccess, protect;
1225 if (access & GNUNET_DISK_MAP_READ && access & GNUNET_DISK_MAP_WRITE)
1227 protect = PAGE_READWRITE;
1228 mapAccess = FILE_MAP_ALL_ACCESS;
1230 else if (access & GNUNET_DISK_MAP_READ)
1232 protect = PAGE_READONLY;
1233 mapAccess = FILE_MAP_READ;
1235 else if (access & GNUNET_DISK_MAP_WRITE)
1237 protect = PAGE_READWRITE;
1238 mapAccess = FILE_MAP_WRITE;
1246 *m = GNUNET_malloc (sizeof (struct GNUNET_IO_Handle));
1247 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1248 if ((*m)->h == INVALID_HANDLE_VALUE)
1250 SetErrnoFromWinError (GetLastError ());
1255 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1258 SetErrnoFromWinError (GetLastError ());
1259 CloseHandle ((*m)->h);
1268 if (access & GNUNET_DISK_MAP_READ)
1270 if (access & GNUNET_DISK_MAP_WRITE)
1273 return mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1279 * @param h mapping handle
1280 * @param addr pointer to the mapped memory region
1281 * @param len size of the mapping
1282 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1285 GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len)
1290 if ( (h == NULL) || (*h == NULL) )
1293 return GNUNET_SYSERR;
1296 ret = UnmapViewOfFile (addr) ? GNUNET_OK : GNUNET_SYSERR;
1297 if (ret != GNUNET_OK)
1298 SetErrnoFromWinError (GetLastError ());
1299 if (!CloseHandle ((*h)->h) && ret == GNUNET_OK)
1301 ret = GNUNET_SYSERR;
1302 SetErrnoFromWinError (GetLastError ());
1305 GNUNET_DISK_handle_invalidate (*h);
1311 return munmap (addr, len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1316 * Write file changes to disk
1317 * @param h handle to an open file
1318 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1321 GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h)
1326 return GNUNET_SYSERR;
1332 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1333 if (ret != GNUNET_OK)
1334 SetErrnoFromWinError (GetLastError ());
1337 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;