2 This file is part of GNUnet.
3 Copyright (C) 2001--2013, 2016 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief disk IO convenience methods
23 * @author Christian Grothoff
28 #include "gnunet_strings_lib.h"
29 #include "gnunet_disk_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-disk", syscall)
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)
38 * Block size for IO for copying files.
40 #define COPY_BLK_SIZE 65536
42 #include <sys/types.h>
47 #include <sys/param.h>
50 #include <sys/mount.h>
52 #if HAVE_SYS_STATVFS_H
53 #include <sys/statvfs.h>
57 #define _IFMT 0170000 /* type of file */
58 #define _IFLNK 0120000 /* symbolic link */
59 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
64 * Handle used to manage a pipe.
66 struct GNUNET_DISK_PipeHandle
69 * File descriptors for the pipe.
70 * One or both of them could be NULL.
72 struct GNUNET_DISK_FileHandle *fd[2];
77 * Closure for the recursion to determine the file size
80 struct GetFileSizeData
83 * Set to the total file size.
88 * GNUNET_YES if symbolic links should be included.
90 int include_sym_links;
93 * GNUNET_YES if mode is file-only (return total == -1 for directories).
101 * Translate GNUnet-internal permission bitmap to UNIX file
102 * access permission bitmap.
104 * @param perm file permissions, GNUnet style
105 * @return file permissions, UNIX style
108 translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
113 if (perm & GNUNET_DISK_PERM_USER_READ)
115 if (perm & GNUNET_DISK_PERM_USER_WRITE)
117 if (perm & GNUNET_DISK_PERM_USER_EXEC)
119 if (perm & GNUNET_DISK_PERM_GROUP_READ)
121 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
123 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
125 if (perm & GNUNET_DISK_PERM_OTHER_READ)
127 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
129 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
138 * Iterate over all files in the given directory and
139 * accumulate their size.
141 * @param cls closure of type `struct GetFileSizeData`
142 * @param fn current filename we are looking at
143 * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK
146 getSizeRec (void *cls, const char *fn)
148 struct GetFileSizeData *gfsd = cls;
150 #if defined (HAVE_STAT64) && !(defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
153 if (0 != STAT64 (fn, &buf))
155 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat64", fn);
156 return GNUNET_SYSERR;
161 if (0 != STAT (fn, &buf))
163 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn);
164 return GNUNET_SYSERR;
167 if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
170 return GNUNET_SYSERR;
172 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
173 gfsd->total += buf.st_size;
174 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) &&
175 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
177 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
178 return GNUNET_SYSERR;
185 * Checks whether a handle is invalid
187 * @param h handle to check
188 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
191 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
194 return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO;
196 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
201 * Get the size of an open file.
203 * @param fh open file handle
204 * @param size where to write size of the file
205 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
208 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
214 b = GetFileSizeEx (fh->h, &li);
217 SetErrnoFromWinError (GetLastError ());
218 return GNUNET_SYSERR;
220 *size = (off_t) li.QuadPart;
224 if (0 != FSTAT (fh->fd, &sbuf))
225 return GNUNET_SYSERR;
226 *size = sbuf.st_size;
233 * Move the read/write pointer in a file
235 * @param h handle of an open file
236 * @param offset position to move to
237 * @param whence specification to which position the offset parameter relates to
238 * @return the new position on success, #GNUNET_SYSERR otherwise
241 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
242 enum GNUNET_DISK_Seek whence)
247 return GNUNET_SYSERR;
252 LARGE_INTEGER new_pos;
255 static DWORD t[] = { FILE_BEGIN, FILE_CURRENT, FILE_END };
256 li.QuadPart = offset;
258 b = SetFilePointerEx (h->h, li, &new_pos, t[whence]);
261 SetErrnoFromWinError (GetLastError ());
262 return GNUNET_SYSERR;
264 return (off_t) new_pos.QuadPart;
266 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
268 return lseek (h->fd, offset, t[whence]);
274 * Get the size of the file (or directory) of the given file (in
277 * @param filename name of the file or directory
278 * @param size set to the size of the file (or,
279 * in the case of directories, the sum
280 * of all sizes of files in the directory)
281 * @param include_symbolic_links should symbolic links be
283 * @param single_file_mode #GNUNET_YES to only get size of one file
284 * and return #GNUNET_SYSERR for directories.
285 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
288 GNUNET_DISK_file_size (const char *filename,
290 int include_symbolic_links,
291 int single_file_mode)
293 struct GetFileSizeData gfsd;
296 GNUNET_assert (size != NULL);
298 gfsd.include_sym_links = include_symbolic_links;
299 gfsd.single_file_mode = single_file_mode;
300 ret = getSizeRec (&gfsd, filename);
307 * Obtain some unique identifiers for the given file
308 * that can be used to identify it in the local system.
309 * This function is used between GNUnet processes to
310 * quickly check if two files with the same absolute path
311 * are actually identical. The two processes represent
312 * the same peer but may communicate over the network
313 * (and the file may be on an NFS volume). This function
314 * may not be supported on all operating systems.
316 * @param filename name of the file
317 * @param dev set to the device ID
318 * @param ino set to the inode ID
319 * @return #GNUNET_OK on success
322 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
327 // FIXME NILS: test this
328 struct GNUNET_DISK_FileHandle *fh;
329 BY_HANDLE_FILE_INFORMATION info;
332 fh = GNUNET_DISK_file_open (filename,
333 GNUNET_DISK_OPEN_READ,
334 GNUNET_DISK_PERM_NONE);
336 return GNUNET_SYSERR;
337 succ = GetFileInformationByHandle (fh->h, &info);
338 GNUNET_DISK_file_close (fh);
341 return GNUNET_SYSERR;
343 *dev = info.dwVolumeSerialNumber;
344 *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow);
351 if (0 != stat (filename, &sbuf))
353 return GNUNET_SYSERR;
355 *ino = (uint64_t) sbuf.st_ino;
364 if (0 != statvfs (filename, &fbuf))
366 return GNUNET_SYSERR;
368 *dev = (uint64_t) fbuf.f_fsid;
374 if (0 != statfs (filename, &fbuf))
376 return GNUNET_SYSERR;
378 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 ||
379 ((uint64_t) fbuf.f_fsid.val[1]);
384 #endif /* !WINDOWS */
390 * Create the name for a temporary file or directory from a template.
392 * @param t template (without XXXXX or "/tmp/")
393 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error
396 mktemp_name (const char *t)
402 if ((t[0] != '/') && (t[0] != '\\')
404 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':'))
408 /* FIXME: This uses system codepage on W32, not UTF-8 */
409 tmpdir = getenv ("TMPDIR");
411 tmpdir = getenv ("TMP");
413 tmpdir = getenv ("TEMP");
416 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
420 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
423 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
424 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
447 tfn = GNUNET_strdup (fn);
448 random_fn = _mktemp (tfn);
449 if (NULL == random_fn)
454 /* FIXME: assume fn to be UTF-8-encoded and do the right thing */
455 if (0 == CreateDirectoryA (tfn, NULL))
457 DWORD error = GetLastError ();
459 if (ERROR_ALREADY_EXISTS == error)
470 * Update POSIX permissions mask of a file on disk. If both argumets
471 * are #GNUNET_NO, the file is made world-read-write-executable (777).
472 * Does nothing on W32.
474 * @param fn name of the file to update
475 * @param require_uid_match #GNUNET_YES means 700
476 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
479 GNUNET_DISK_fix_permissions (const char *fn,
480 int require_uid_match,
481 int require_gid_match)
489 * Update POSIX permissions mask of a file on disk. If both argumets
490 * are #GNUNET_NO, the file is made world-read-write-executable (777).
492 * @param fn name of the file to update
493 * @param require_uid_match #GNUNET_YES means 700
494 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
497 GNUNET_DISK_fix_permissions (const char *fn,
498 int require_uid_match,
499 int require_gid_match)
503 if (GNUNET_YES == require_uid_match)
504 mode = S_IRUSR | S_IWUSR | S_IXUSR;
505 else if (GNUNET_YES == require_gid_match)
506 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
508 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
509 if (0 != chmod (fn, mode))
510 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
518 * Create an (empty) temporary directory on disk. If the given name is not
519 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
520 * 6 random characters will be appended to the name to create a unique
523 * @param t component to use for the name;
524 * does NOT contain "XXXXXX" or "/tmp/".
525 * @return NULL on error, otherwise name of fresh
526 * file on disk in directory for temporary files
529 GNUNET_DISK_mkdtemp (const char *t)
534 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
535 fn = mktemp_name (t);
536 if (fn != mkdtemp (fn))
538 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn);
549 * Move a file out of the way (create a backup) by
550 * renaming it to "orig.NUM~" where NUM is the smallest
551 * number that is not used yet.
553 * @param fil name of the file to back up
556 GNUNET_DISK_file_backup (const char *fil)
562 slen = strlen (fil) + 20;
563 target = GNUNET_malloc (slen);
567 GNUNET_snprintf (target, slen,
571 } while (0 == access (target, F_OK));
572 if (0 != rename (fil, target))
573 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
576 GNUNET_free (target);
581 * Create an (empty) temporary file on disk. If the given name is not
582 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
583 * 6 random characters will be appended to the name to create a unique
586 * @param t component to use for the name;
587 * does NOT contain "XXXXXX" or "/tmp/".
588 * @return NULL on error, otherwise name of fresh
589 * file on disk in directory for temporary files
592 GNUNET_DISK_mktemp (const char *t)
598 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
599 fn = mktemp_name (t);
600 if (-1 == (fd = mkstemp (fn)))
602 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
609 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
615 * Test if @a fil is a directory and listable. Optionally, also check if the
616 * directory is readable. Will not print an error message if the directory does
617 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
618 * with the same name).
620 * @param fil filename to test
621 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
622 * #GNUNET_NO to disable this check
623 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
624 * does not exist or stat'ed
627 GNUNET_DISK_directory_test (const char *fil, int is_readable)
629 struct stat filestat;
632 ret = STAT (fil, &filestat);
636 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
637 return GNUNET_SYSERR;
639 if (!S_ISDIR (filestat.st_mode))
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "A file already exits with the same name %s\n", fil);
645 if (GNUNET_YES == is_readable)
646 ret = ACCESS (fil, R_OK | X_OK);
648 ret = ACCESS (fil, X_OK);
651 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
659 * Check that fil corresponds to a filename
660 * (of a file that exists and that is not a directory).
662 * @param fil filename to check
663 * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something
664 * else (will print an error message in that case, too).
667 GNUNET_DISK_file_test (const char *fil)
669 struct stat filestat;
673 rdir = GNUNET_STRINGS_filename_expand (fil);
675 return GNUNET_SYSERR;
677 ret = STAT (rdir, &filestat);
682 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
684 return GNUNET_SYSERR;
689 if (!S_ISREG (filestat.st_mode))
694 if (ACCESS (rdir, F_OK) < 0)
696 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
698 return GNUNET_SYSERR;
706 * Implementation of "mkdir -p"
708 * @param dir the directory to create
709 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
712 GNUNET_DISK_directory_create (const char *dir)
720 rdir = GNUNET_STRINGS_filename_expand (dir);
722 return GNUNET_SYSERR;
726 pos = 1; /* skip heading '/' */
728 /* Local or Network path? */
729 if (strncmp (rdir, "\\\\", 2) == 0)
734 if (rdir[pos] == '\\')
744 pos = 3; /* strlen("C:\\") */
747 /* Check which low level directories already exist */
749 rdir[len] = DIR_SEPARATOR;
752 if (DIR_SEPARATOR == rdir[pos2])
755 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
756 if (GNUNET_NO == ret)
759 return GNUNET_SYSERR;
761 rdir[pos2] = DIR_SEPARATOR;
762 if (GNUNET_YES == ret)
773 /* Start creating directories */
776 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
779 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
780 if (GNUNET_NO == ret)
783 return GNUNET_SYSERR;
785 if (GNUNET_SYSERR == ret)
788 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
790 wchar_t wrdir[MAX_PATH + 1];
791 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(rdir, wrdir))
792 ret = !CreateDirectoryW (wrdir, NULL);
796 if ((ret != 0) && (errno != EEXIST))
798 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
800 return GNUNET_SYSERR;
803 rdir[pos] = DIR_SEPARATOR;
813 * Create the directory structure for storing a file.
815 * @param filename name of a file in the directory
816 * @returns #GNUNET_OK on success,
817 * #GNUNET_SYSERR on failure,
818 * #GNUNET_NO if the directory
819 * exists but is not writeable for us
822 GNUNET_DISK_directory_create_for_file (const char *filename)
829 rdir = GNUNET_STRINGS_filename_expand (filename);
833 return GNUNET_SYSERR;
836 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
839 /* The empty path is invalid and in this case refers to / */
843 rdir = GNUNET_strdup ("/");
845 ret = GNUNET_DISK_directory_create (rdir);
846 if ((GNUNET_OK == ret) && (0 != ACCESS (rdir, W_OK)))
856 * Read the contents of a binary file into a buffer.
858 * @param h handle to an open file
859 * @param result the buffer to write the result to
860 * @param len the maximum number of bytes to read
861 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
864 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
871 return GNUNET_SYSERR;
877 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE)
879 if (!ReadFile (h->h, result, len, &bytes_read, NULL))
881 SetErrnoFromWinError (GetLastError ());
882 return GNUNET_SYSERR;
885 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
887 if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead))
889 if (GetLastError () != ERROR_IO_PENDING)
891 LOG (GNUNET_ERROR_TYPE_DEBUG,
892 "Error reading from pipe: %u\n",
894 SetErrnoFromWinError (GetLastError ());
895 return GNUNET_SYSERR;
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
898 GetOverlappedResult (h->h, h->oOverlapRead, &bytes_read, TRUE);
900 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytes_read);
908 return read (h->fd, result, len);
914 * Read the contents of a binary file into a buffer.
915 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
916 * when no data can be read).
918 * @param h handle to an open file
919 * @param result the buffer to write the result to
920 * @param len the maximum number of bytes to read
921 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
924 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
931 return GNUNET_SYSERR;
937 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE)
939 if (!ReadFile (h->h, result, len, &bytes_read, NULL))
941 SetErrnoFromWinError (GetLastError ());
942 return GNUNET_SYSERR;
945 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
947 if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead))
949 if (GetLastError () != ERROR_IO_PENDING)
951 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ());
952 SetErrnoFromWinError (GetLastError ());
953 return GNUNET_SYSERR;
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "ReadFile() queued a read, cancelling\n");
961 return GNUNET_SYSERR;
964 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 /* set to non-blocking, read, then set back */
978 flags = fcntl (h->fd, F_GETFL);
979 if (0 == (flags & O_NONBLOCK))
980 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
981 ret = read (h->fd, result, len);
982 if (0 == (flags & O_NONBLOCK))
985 (void) fcntl (h->fd, F_SETFL, flags);
994 * Read the contents of a binary file into a buffer.
996 * @param fn file name
997 * @param result the buffer to write the result to
998 * @param len the maximum number of bytes to read
999 * @return number of bytes read, #GNUNET_SYSERR on failure
1002 GNUNET_DISK_fn_read (const char *fn,
1006 struct GNUNET_DISK_FileHandle *fh;
1010 fh = GNUNET_DISK_file_open (fn,
1011 GNUNET_DISK_OPEN_READ,
1012 GNUNET_DISK_PERM_NONE);
1014 return GNUNET_SYSERR;
1015 ret = GNUNET_DISK_file_read (fh, result, len);
1017 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1024 * Write a buffer to a file.
1026 * @param h handle to open file
1027 * @param buffer the data to write
1028 * @param n number of bytes to write
1029 * @return number of bytes written on success, #GNUNET_SYSERR on error
1032 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
1033 const void *buffer, size_t n)
1038 return GNUNET_SYSERR;
1042 DWORD bytes_written;
1044 if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1046 if (!WriteFile (h->h, buffer, n, &bytes_written, NULL))
1048 SetErrnoFromWinError (GetLastError ());
1049 return GNUNET_SYSERR;
1052 else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1054 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n);
1055 if (!WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite))
1057 if (GetLastError () != ERROR_IO_PENDING)
1059 SetErrnoFromWinError (GetLastError ());
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1062 return GNUNET_SYSERR;
1064 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
1065 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytes_written, TRUE))
1067 SetErrnoFromWinError (GetLastError ());
1068 LOG (GNUNET_ERROR_TYPE_DEBUG,
1069 "Error getting overlapped result while writing to pipe: %u\n",
1071 return GNUNET_SYSERR;
1077 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE))
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "Error getting control overlapped result while writing to pipe: %u\n",
1085 LOG (GNUNET_ERROR_TYPE_DEBUG,
1086 "Wrote %u bytes (ovr says %u), picking the greatest\n",
1087 bytes_written, ovr);
1090 if (bytes_written == 0)
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytes_written);
1096 return GNUNET_SYSERR;
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written);
1105 return bytes_written;
1107 return write (h->fd, buffer, n);
1113 * Write a buffer to a file, blocking, if necessary.
1115 * @param h handle to open file
1116 * @param buffer the data to write
1117 * @param n number of bytes to write
1118 * @return number of bytes written on success, #GNUNET_SYSERR on error
1121 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
1128 return GNUNET_SYSERR;
1132 DWORD bytes_written;
1133 /* We do a non-overlapped write, which is as blocking as it gets */
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n);
1135 if (!WriteFile (h->h, buffer, n, &bytes_written, NULL))
1137 SetErrnoFromWinError (GetLastError ());
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1140 return GNUNET_SYSERR;
1142 if (bytes_written == 0 && n > 0)
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n");
1145 WaitForSingleObject (h->h, INFINITE);
1146 if (!WriteFile (h->h, buffer, n, &bytes_written, NULL))
1148 SetErrnoFromWinError (GetLastError ());
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1151 return GNUNET_SYSERR;
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 return bytes_written;
1162 /* set to blocking, write, then set back */
1163 flags = fcntl (h->fd, F_GETFL);
1164 if (0 != (flags & O_NONBLOCK))
1165 (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
1166 ret = write (h->fd, buffer, n);
1167 if (0 == (flags & O_NONBLOCK))
1168 (void) fcntl (h->fd, F_SETFL, flags);
1175 * Write a buffer to a file. If the file is longer than the
1176 * number of bytes that will be written, it will be truncated.
1178 * @param fn file name
1179 * @param buffer the data to write
1180 * @param n number of bytes to write
1181 * @param mode file permissions
1182 * @return number of bytes written on success, #GNUNET_SYSERR on error
1185 GNUNET_DISK_fn_write (const char *fn,
1188 enum GNUNET_DISK_AccessPermissions mode)
1190 struct GNUNET_DISK_FileHandle *fh;
1193 fh = GNUNET_DISK_file_open (fn,
1194 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
1195 | GNUNET_DISK_OPEN_CREATE, mode);
1197 return GNUNET_SYSERR;
1198 ret = GNUNET_DISK_file_write (fh, buffer, n);
1199 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1205 * Scan a directory for files.
1207 * @param dir_name the name of the directory
1208 * @param callback the method to call for each file,
1209 * can be NULL, in that case, we only count
1210 * @param callback_cls closure for @a callback
1211 * @return the number of files found, #GNUNET_SYSERR on error or
1212 * ieration aborted by callback returning #GNUNET_SYSERR
1215 GNUNET_DISK_directory_scan (const char *dir_name,
1216 GNUNET_FileNameCallback callback,
1220 struct dirent *finfo;
1226 unsigned int name_len;
1227 unsigned int n_size;
1229 GNUNET_assert (NULL != dir_name);
1230 dname = GNUNET_STRINGS_filename_expand (dir_name);
1232 return GNUNET_SYSERR;
1233 while ( (strlen (dname) > 0) &&
1234 (dname[strlen (dname) - 1] == DIR_SEPARATOR) )
1235 dname[strlen (dname) - 1] = '\0';
1236 if (0 != STAT (dname, &istat))
1238 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1241 GNUNET_free (dname);
1242 return GNUNET_SYSERR;
1244 if (! S_ISDIR (istat.st_mode))
1246 LOG (GNUNET_ERROR_TYPE_WARNING,
1247 _("Expected `%s' to be a directory!\n"),
1249 GNUNET_free (dname);
1250 return GNUNET_SYSERR;
1253 dinfo = OPENDIR (dname);
1254 if ( (EACCES == errno) ||
1257 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1262 GNUNET_free (dname);
1263 return GNUNET_SYSERR;
1266 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
1267 name = GNUNET_malloc (n_size);
1268 while (NULL != (finfo = READDIR (dinfo)))
1270 if ( (0 == strcmp (finfo->d_name, ".")) ||
1271 (0 == strcmp (finfo->d_name, "..")) )
1273 if (NULL != callback)
1275 if (name_len < strlen (finfo->d_name))
1278 name_len = strlen (finfo->d_name);
1279 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
1280 name = GNUNET_malloc (n_size);
1282 /* dname can end in "/" only if dname == "/";
1283 * if dname does not end in "/", we need to add
1284 * a "/" (otherwise, we must not!) */
1285 GNUNET_snprintf (name,
1289 (0 == strcmp (dname,
1292 : DIR_SEPARATOR_STR,
1294 ret = callback (callback_cls,
1296 if (GNUNET_OK != ret)
1300 GNUNET_free (dname);
1301 if (GNUNET_NO == ret)
1303 return GNUNET_SYSERR;
1310 GNUNET_free (dname);
1316 * Function that removes the given directory by calling
1317 * #GNUNET_DISK_directory_remove().
1319 * @param unused not used
1320 * @param fn directory to remove
1321 * @return #GNUNET_OK
1324 remove_helper (void *unused,
1327 (void) GNUNET_DISK_directory_remove (fn);
1333 * Remove all files in a directory (rm -r). Call with
1336 * @param filename the file to remove
1337 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1340 GNUNET_DISK_directory_remove (const char *filename)
1344 if (NULL == filename)
1347 return GNUNET_SYSERR;
1349 if (0 != LSTAT (filename, &istat))
1350 return GNUNET_NO; /* file may not exist... */
1351 (void) CHMOD (filename,
1352 S_IWUSR | S_IRUSR | S_IXUSR);
1353 if (0 == UNLINK (filename))
1355 if ( (errno != EISDIR) &&
1356 /* EISDIR is not sufficient in all cases, e.g.
1357 * sticky /tmp directory may result in EPERM on BSD.
1358 * So we also explicitly check "isDirectory" */
1360 GNUNET_DISK_directory_test (filename,
1363 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1366 return GNUNET_SYSERR;
1368 if (GNUNET_SYSERR ==
1369 GNUNET_DISK_directory_scan (filename,
1372 return GNUNET_SYSERR;
1373 if (0 != RMDIR (filename))
1375 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1378 return GNUNET_SYSERR;
1387 * @param src file to copy
1388 * @param dst destination file name
1389 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1392 GNUNET_DISK_file_copy (const char *src,
1399 struct GNUNET_DISK_FileHandle *in;
1400 struct GNUNET_DISK_FileHandle *out;
1402 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1403 return GNUNET_SYSERR;
1405 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
1406 GNUNET_DISK_PERM_NONE);
1408 return GNUNET_SYSERR;
1410 GNUNET_DISK_file_open (dst,
1411 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
1412 GNUNET_DISK_OPEN_FAILIFEXISTS,
1413 GNUNET_DISK_PERM_USER_READ |
1414 GNUNET_DISK_PERM_USER_WRITE |
1415 GNUNET_DISK_PERM_GROUP_READ |
1416 GNUNET_DISK_PERM_GROUP_WRITE);
1419 GNUNET_DISK_file_close (in);
1420 return GNUNET_SYSERR;
1422 buf = GNUNET_malloc (COPY_BLK_SIZE);
1425 len = COPY_BLK_SIZE;
1426 if (len > size - pos)
1428 if (len != GNUNET_DISK_file_read (in, buf, len))
1430 if (len != GNUNET_DISK_file_write (out, buf, len))
1435 GNUNET_DISK_file_close (in);
1436 GNUNET_DISK_file_close (out);
1440 GNUNET_DISK_file_close (in);
1441 GNUNET_DISK_file_close (out);
1442 return GNUNET_SYSERR;
1447 * @brief Removes special characters as ':' from a filename.
1448 * @param fn the filename to canonicalize
1451 GNUNET_DISK_filename_canonicalize (char *fn)
1456 for (idx = fn; *idx; idx++)
1460 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' ||
1461 c == '<' || c == '>' || c == '|')
1471 * @brief Change owner of a file
1473 * @param filename name of file to change the owner of
1474 * @param user name of the new owner
1475 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1478 GNUNET_DISK_file_change_owner (const char *filename,
1484 pws = getpwnam (user);
1487 LOG (GNUNET_ERROR_TYPE_ERROR,
1488 _("Cannot obtain information about user `%s': %s\n"),
1491 return GNUNET_SYSERR;
1493 if (0 != chown (filename,
1497 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1500 return GNUNET_SYSERR;
1508 * Lock a part of a file
1510 * @param fh file handle
1511 * @param lock_start absolute position from where to lock
1512 * @param lock_end absolute position until where to lock
1513 * @param excl #GNUNET_YES for an exclusive lock
1514 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1517 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
1525 return GNUNET_SYSERR;
1531 memset (&fl, 0, sizeof (struct flock));
1532 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1533 fl.l_whence = SEEK_SET;
1534 fl.l_start = lock_start;
1535 fl.l_len = lock_end;
1537 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1540 off_t diff = lock_end - lock_start;
1541 DWORD diff_low, diff_high;
1542 diff_low = (DWORD) (diff & 0xFFFFFFFF);
1543 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1545 memset (&o, 0, sizeof (OVERLAPPED));
1546 o.Offset = (DWORD) (lock_start & 0xFFFFFFFF);;
1547 o.OffsetHigh = (DWORD) (((lock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1550 (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY,
1551 0, diff_low, diff_high, &o))
1553 SetErrnoFromWinError (GetLastError ());
1554 return GNUNET_SYSERR;
1563 * Unlock a part of a file
1565 * @param fh file handle
1566 * @param unlock_start absolute position from where to unlock
1567 * @param unlock_end absolute position until where to unlock
1568 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1571 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
1578 return GNUNET_SYSERR;
1584 memset (&fl, 0, sizeof (struct flock));
1585 fl.l_type = F_UNLCK;
1586 fl.l_whence = SEEK_SET;
1587 fl.l_start = unlock_start;
1588 fl.l_len = unlock_end;
1590 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1593 off_t diff = unlock_end - unlock_start;
1594 DWORD diff_low, diff_high;
1595 diff_low = (DWORD) (diff & 0xFFFFFFFF);
1596 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1598 memset (&o, 0, sizeof (OVERLAPPED));
1599 o.Offset = (DWORD) (unlock_start & 0xFFFFFFFF);;
1600 o.OffsetHigh = (DWORD) (((unlock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1602 if (!UnlockFileEx (fh->h, 0, diff_low, diff_high, &o))
1604 SetErrnoFromWinError (GetLastError ());
1605 return GNUNET_SYSERR;
1614 * Open a file. Note that the access permissions will only be
1615 * used if a new file is created and if the underlying operating
1616 * system supports the given permissions.
1618 * @param fn file name to be opened
1619 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1620 * @param perm permissions for the newly created file, use
1621 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
1622 * call (because of flags)
1623 * @return IO handle on success, NULL on error
1625 struct GNUNET_DISK_FileHandle *
1626 GNUNET_DISK_file_open (const char *fn,
1627 enum GNUNET_DISK_OpenFlags flags,
1628 enum GNUNET_DISK_AccessPermissions perm)
1631 struct GNUNET_DISK_FileHandle *ret;
1637 wchar_t wexpfn[MAX_PATH + 1];
1644 expfn = GNUNET_STRINGS_filename_expand (fn);
1649 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1650 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1651 else if (flags & GNUNET_DISK_OPEN_READ)
1653 else if (flags & GNUNET_DISK_OPEN_WRITE)
1658 GNUNET_free (expfn);
1661 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1662 oflags |= (O_CREAT | O_EXCL);
1663 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1665 if (flags & GNUNET_DISK_OPEN_APPEND)
1667 if (flags & GNUNET_DISK_OPEN_CREATE)
1669 (void) GNUNET_DISK_directory_create_for_file (expfn);
1671 mode = translate_unix_perms (perm);
1674 fd = open (expfn, oflags
1678 | O_LARGEFILE, mode);
1681 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1682 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1684 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1685 GNUNET_free (expfn);
1692 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1693 access = FILE_READ_DATA | FILE_WRITE_DATA;
1694 else if (flags & GNUNET_DISK_OPEN_READ)
1695 access = FILE_READ_DATA;
1696 else if (flags & GNUNET_DISK_OPEN_WRITE)
1697 access = FILE_WRITE_DATA;
1699 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1703 else if (flags & GNUNET_DISK_OPEN_CREATE)
1705 (void) GNUNET_DISK_directory_create_for_file (expfn);
1706 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1707 disp = CREATE_ALWAYS;
1711 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1713 disp = TRUNCATE_EXISTING;
1717 disp = OPEN_EXISTING;
1720 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(expfn, wexpfn))
1721 h = CreateFileW (wexpfn, access,
1722 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1723 disp, FILE_ATTRIBUTE_NORMAL, NULL);
1725 h = INVALID_HANDLE_VALUE;
1726 if (h == INVALID_HANDLE_VALUE)
1729 SetErrnoFromWinError (GetLastError ());
1731 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_INFO, "open", expfn);
1732 GNUNET_free (expfn);
1737 if (flags & GNUNET_DISK_OPEN_APPEND)
1738 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1740 SetErrnoFromWinError (GetLastError ());
1741 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1743 GNUNET_free (expfn);
1748 ret = GNUNET_new (struct GNUNET_DISK_FileHandle);
1751 ret->type = GNUNET_DISK_HANLDE_TYPE_FILE;
1755 GNUNET_free (expfn);
1761 * Close an open file.
1763 * @param h file handle
1764 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1767 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1773 return GNUNET_SYSERR;
1779 if (! CloseHandle (h->h))
1781 SetErrnoFromWinError (GetLastError ());
1782 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1783 ret = GNUNET_SYSERR;
1785 if (h->oOverlapRead)
1787 if (! CloseHandle (h->oOverlapRead->hEvent))
1789 SetErrnoFromWinError (GetLastError ());
1790 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1791 ret = GNUNET_SYSERR;
1793 GNUNET_free (h->oOverlapRead);
1795 if (h->oOverlapWrite)
1797 if (!CloseHandle (h->oOverlapWrite->hEvent))
1799 SetErrnoFromWinError (GetLastError ());
1800 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1801 ret = GNUNET_SYSERR;
1803 GNUNET_free (h->oOverlapWrite);
1806 if (close (h->fd) != 0)
1808 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1809 ret = GNUNET_SYSERR;
1819 * Get a GNUnet file handle from a W32 handle.
1821 * @param handle native handle
1822 * @return GNUnet file handle corresponding to the W32 handle
1824 struct GNUNET_DISK_FileHandle *
1825 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh)
1827 struct GNUNET_DISK_FileHandle *fh;
1829 enum GNUNET_FILE_Type ftype;
1831 dwret = GetFileType (osfh);
1834 case FILE_TYPE_DISK:
1835 ftype = GNUNET_DISK_HANLDE_TYPE_FILE;
1837 case FILE_TYPE_PIPE:
1838 ftype = GNUNET_DISK_HANLDE_TYPE_PIPE;
1840 case FILE_TYPE_UNKNOWN:
1841 if ( (GetLastError () == NO_ERROR) ||
1842 (GetLastError () == ERROR_INVALID_HANDLE) )
1844 if (0 != ResetEvent (osfh))
1845 ftype = GNUNET_DISK_HANLDE_TYPE_EVENT;
1856 fh = GNUNET_new (struct GNUNET_DISK_FileHandle);
1860 if (ftype == GNUNET_DISK_HANLDE_TYPE_PIPE)
1863 * Note that we can't make it overlapped if it isn't already.
1864 * (ReOpenFile() is only available in 2003/Vista).
1865 * The process that opened this file in the first place (usually a parent
1866 * process, if this is stdin/stdout/stderr) must make it overlapped,
1867 * otherwise we're screwed, as selecting on non-overlapped handle
1870 fh->oOverlapRead = GNUNET_new (OVERLAPPED);
1871 fh->oOverlapWrite = GNUNET_new (OVERLAPPED);
1872 fh->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
1873 fh->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
1881 * Get a handle from a native integer FD.
1883 * @param fno native integer file descriptor
1884 * @return file handle corresponding to the descriptor, NULL on error
1886 struct GNUNET_DISK_FileHandle *
1887 GNUNET_DISK_get_handle_from_int_fd (int fno)
1889 struct GNUNET_DISK_FileHandle *fh;
1891 if ( (((off_t) -1) == lseek (fno, 0, SEEK_CUR)) &&
1893 return NULL; /* invalid FD */
1896 fh = GNUNET_new (struct GNUNET_DISK_FileHandle);
1902 osfh = _get_osfhandle (fno);
1903 if (INVALID_HANDLE_VALUE == (HANDLE) osfh)
1906 fh = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) osfh);
1914 * Get a handle from a native streaming FD.
1916 * @param fd native streaming file descriptor
1917 * @return file handle corresponding to the descriptor
1919 struct GNUNET_DISK_FileHandle *
1920 GNUNET_DISK_get_handle_from_native (FILE *fd)
1928 return GNUNET_DISK_get_handle_from_int_fd (fno);
1933 * Handle for a memory-mapping operation.
1935 struct GNUNET_DISK_MapHandle
1938 * Address where the map is in memory.
1944 * Underlying OS handle.
1949 * Number of bytes mapped.
1957 #define MAP_FAILED ((void *) -1)
1961 * Map a file into memory
1963 * @param h open file handle
1964 * @param m handle to the new mapping
1965 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
1966 * @param len size of the mapping
1967 * @return pointer to the mapped memory region, NULL on failure
1970 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1971 struct GNUNET_DISK_MapHandle **m,
1972 enum GNUNET_DISK_MapType access, size_t len)
1981 DWORD mapAccess, protect;
1983 if ((access & GNUNET_DISK_MAP_TYPE_READ) &&
1984 (access & GNUNET_DISK_MAP_TYPE_WRITE))
1986 protect = PAGE_READWRITE;
1987 mapAccess = FILE_MAP_ALL_ACCESS;
1989 else if (access & GNUNET_DISK_MAP_TYPE_READ)
1991 protect = PAGE_READONLY;
1992 mapAccess = FILE_MAP_READ;
1994 else if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1996 protect = PAGE_READWRITE;
1997 mapAccess = FILE_MAP_WRITE;
2005 *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
2006 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
2007 if ((*m)->h == INVALID_HANDLE_VALUE)
2009 SetErrnoFromWinError (GetLastError ());
2014 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
2017 SetErrnoFromWinError (GetLastError ());
2018 CloseHandle ((*m)->h);
2027 if (access & GNUNET_DISK_MAP_TYPE_READ)
2029 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
2031 *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
2032 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
2033 GNUNET_assert (NULL != (*m)->addr);
2034 if (MAP_FAILED == (*m)->addr)
2046 * @param h mapping handle
2047 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2050 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
2057 return GNUNET_SYSERR;
2061 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR;
2062 if (ret != GNUNET_OK)
2063 SetErrnoFromWinError (GetLastError ());
2064 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
2066 ret = GNUNET_SYSERR;
2067 SetErrnoFromWinError (GetLastError ());
2070 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
2078 * Write file changes to disk
2079 * @param h handle to an open file
2080 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2083 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
2088 return GNUNET_SYSERR;
2094 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
2095 if (ret != GNUNET_OK)
2096 SetErrnoFromWinError (GetLastError ());
2098 #elif defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN)
2099 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
2101 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
2108 #define PIPE_BUF 512
2110 /* Copyright Bob Byrnes <byrnes <at> curl.com>
2111 http://permalink.gmane.org/gmane.os.cygwin.patches/2121
2113 /* Create a pipe, and return handles to the read and write ends,
2114 just like CreatePipe, but ensure that the write end permits
2115 FILE_READ_ATTRIBUTES access, on later versions of win32 where
2116 this is supported. This access is needed by NtQueryInformationFile,
2117 which is used to implement select and nonblocking writes.
2118 Note that the return value is either NO_ERROR or GetLastError,
2119 unlike CreatePipe, which returns a bool for success or failure. */
2121 create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2122 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize,
2123 DWORD dwReadMode, DWORD dwWriteMode)
2125 /* Default to error. */
2126 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;
2131 /* Ensure that there is enough pipe buffer space for atomic writes. */
2132 if (psize < PIPE_BUF)
2135 char pipename[MAX_PATH];
2137 /* Retry CreateNamedPipe as long as the pipe name is in use.
2138 * Retrying will probably never be necessary, but we want
2139 * to be as robust as possible. */
2142 static volatile LONG pipe_unique_id;
2144 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld",
2145 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id));
2146 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n",
2148 /* Use CreateNamedPipe instead of CreatePipe, because the latter
2149 * returns a write handle that does not permit FILE_READ_ATTRIBUTES
2150 * access, on versions of win32 earlier than WinXP SP2.
2151 * CreatePipe also stupidly creates a full duplex pipe, which is
2152 * a waste, since only a single direction is actually used.
2153 * It's important to only allow a single instance, to ensure that
2154 * the pipe was not created earlier by some other process, even if
2155 * the pid has been reused. */
2156 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */
2157 psize, /* output buffer size */
2158 psize, /* input buffer size */
2159 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
2161 if (read_pipe != INVALID_HANDLE_VALUE)
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe);
2167 DWORD err = GetLastError ();
2171 case ERROR_PIPE_BUSY:
2172 /* The pipe is already open with compatible parameters.
2173 * Pick a new name and retry. */
2174 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n");
2176 case ERROR_ACCESS_DENIED:
2177 /* The pipe is already open with incompatible parameters.
2178 * Pick a new name and retry. */
2179 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n");
2181 case ERROR_CALL_NOT_IMPLEMENTED:
2182 /* We are on an older Win9x platform without named pipes.
2183 * Return an anonymous pipe as the best approximation. */
2184 LOG (GNUNET_ERROR_TYPE_DEBUG,
2185 "CreateNamedPipe not implemented, resorting to "
2186 "CreatePipe: size = %lu\n", psize);
2187 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
2189 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p, write handle = %p\n",
2194 err = GetLastError ();
2195 LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err);
2198 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err);
2203 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename);
2205 /* Open the named pipe for writing.
2206 * Be sure to permit FILE_READ_ATTRIBUTES access. */
2207 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */
2208 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */
2209 0); /* handle to template file */
2211 if (write_pipe == INVALID_HANDLE_VALUE)
2214 DWORD err = GetLastError ();
2216 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err);
2217 CloseHandle (read_pipe);
2220 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe);
2222 *read_pipe_ptr = read_pipe;
2223 *write_pipe_ptr = write_pipe;
2230 * Creates an interprocess channel
2232 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
2233 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
2234 * @param inherit_read inherit the parent processes stdin (only for windows)
2235 * @param inherit_write inherit the parent processes stdout (only for windows)
2236 * @return handle to the new pipe, NULL on error
2238 struct GNUNET_DISK_PipeHandle *
2239 GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write)
2250 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
2254 return GNUNET_DISK_pipe_from_fd (blocking_read,
2258 struct GNUNET_DISK_PipeHandle *p;
2263 p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
2264 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2265 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2267 /* All pipes are overlapped. If you want them to block - just
2268 * call WriteFile() and ReadFile() with NULL overlapped pointer.
2269 * NOTE: calling with NULL overlapped pointer works only
2270 * for pipes, and doesn't seem to be a documented feature.
2271 * It will NOT work for files, because overlapped files need
2272 * to read offsets from the overlapped structure, regardless.
2273 * Pipes are not seekable, and need no offsets, which is
2274 * probably why it works for them.
2277 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0,
2278 FILE_FLAG_OVERLAPPED,
2279 FILE_FLAG_OVERLAPPED);
2282 SetErrnoFromWinError (GetLastError ());
2284 GNUNET_free (p->fd[0]);
2285 GNUNET_free (p->fd[1]);
2290 if (!DuplicateHandle
2291 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0,
2292 inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
2294 SetErrnoFromWinError (GetLastError ());
2296 CloseHandle (p->fd[0]->h);
2297 CloseHandle (p->fd[1]->h);
2298 GNUNET_free (p->fd[0]);
2299 GNUNET_free (p->fd[1]);
2304 CloseHandle (p->fd[0]->h);
2305 p->fd[0]->h = tmp_handle;
2307 if (!DuplicateHandle
2308 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0,
2309 inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
2311 SetErrnoFromWinError (GetLastError ());
2313 CloseHandle (p->fd[0]->h);
2314 CloseHandle (p->fd[1]->h);
2315 GNUNET_free (p->fd[0]);
2316 GNUNET_free (p->fd[1]);
2321 CloseHandle (p->fd[1]->h);
2322 p->fd[1]->h = tmp_handle;
2324 p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2325 p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2327 p->fd[0]->oOverlapRead = GNUNET_new (OVERLAPPED);
2328 p->fd[0]->oOverlapWrite = GNUNET_new (OVERLAPPED);
2329 p->fd[1]->oOverlapRead = GNUNET_new (OVERLAPPED);
2330 p->fd[1]->oOverlapWrite = GNUNET_new (OVERLAPPED);
2332 p->fd[0]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2333 p->fd[0]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2335 p->fd[1]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2336 p->fd[1]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2344 * Creates a pipe object from a couple of file descriptors.
2345 * Useful for wrapping existing pipe FDs.
2347 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
2348 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
2349 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
2351 * @return handle to the new pipe, NULL on error
2353 struct GNUNET_DISK_PipeHandle *
2354 GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
2356 struct GNUNET_DISK_PipeHandle *p;
2358 p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
2363 int eno = 0; /* make gcc happy */
2368 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2369 p->fd[0]->fd = fd[0];
2372 flags = fcntl (fd[0], F_GETFL);
2373 flags |= O_NONBLOCK;
2374 if (0 > fcntl (fd[0], F_SETFL, flags))
2380 flags = fcntl (fd[0], F_GETFD);
2381 flags |= FD_CLOEXEC;
2382 if (0 > fcntl (fd[0], F_SETFD, flags))
2391 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2392 p->fd[1]->fd = fd[1];
2393 if (!blocking_write)
2395 flags = fcntl (fd[1], F_GETFL);
2396 flags |= O_NONBLOCK;
2397 if (0 > fcntl (fd[1], F_SETFL, flags))
2403 flags = fcntl (fd[1], F_GETFD);
2404 flags |= FD_CLOEXEC;
2405 if (0 > fcntl (fd[1], F_SETFD, flags))
2414 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
2415 if (p->fd[0]->fd >= 0)
2416 GNUNET_break (0 == close (p->fd[0]->fd));
2417 if (p->fd[1]->fd >= 0)
2418 GNUNET_break (0 == close (p->fd[1]->fd));
2419 GNUNET_free_non_null (p->fd[0]);
2420 GNUNET_free_non_null (p->fd[1]);
2428 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2429 p->fd[0]->h = (HANDLE) _get_osfhandle (fd[0]);
2430 if (p->fd[0]->h != INVALID_HANDLE_VALUE)
2432 p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2433 p->fd[0]->oOverlapRead = GNUNET_new (OVERLAPPED);
2434 p->fd[0]->oOverlapWrite = GNUNET_new (OVERLAPPED);
2435 p->fd[0]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2436 p->fd[0]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2440 GNUNET_free (p->fd[0]);
2446 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
2447 p->fd[1]->h = (HANDLE) _get_osfhandle (fd[1]);
2448 if (p->fd[1]->h != INVALID_HANDLE_VALUE)
2450 p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2451 p->fd[1]->oOverlapRead = GNUNET_new (OVERLAPPED);
2452 p->fd[1]->oOverlapWrite = GNUNET_new (OVERLAPPED);
2453 p->fd[1]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2454 p->fd[1]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2458 GNUNET_free (p->fd[1]);
2469 * Closes an interprocess channel
2471 * @param p pipe to close
2472 * @param end which end of the pipe to close
2473 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2476 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
2477 enum GNUNET_DISK_PipeEnd end)
2479 int ret = GNUNET_OK;
2481 if (end == GNUNET_DISK_PIPE_END_READ)
2485 ret = GNUNET_DISK_file_close (p->fd[0]);
2489 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2493 ret = GNUNET_DISK_file_close (p->fd[1]);
2502 * Detaches one of the ends from the pipe.
2503 * Detached end is a fully-functional FileHandle, it will
2504 * not be affected by anything you do with the pipe afterwards.
2505 * Each end of a pipe can only be detched from it once (i.e.
2506 * it is not duplicated).
2508 * @param p pipe to detach an end from
2509 * @param end which end of the pipe to detach
2510 * @return Detached end on success, NULL on failure
2511 * (or if that end is not present or is closed).
2513 struct GNUNET_DISK_FileHandle *
2514 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
2515 enum GNUNET_DISK_PipeEnd end)
2517 struct GNUNET_DISK_FileHandle *ret = NULL;
2519 if (end == GNUNET_DISK_PIPE_END_READ)
2527 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2541 * Closes an interprocess channel
2543 * @param p pipe to close
2544 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2547 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
2549 int ret = GNUNET_OK;
2552 int write_end_close;
2553 int read_end_close_errno;
2554 int write_end_close_errno;
2556 read_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_READ);
2557 read_end_close_errno = errno;
2558 write_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_WRITE);
2559 write_end_close_errno = errno;
2562 if (GNUNET_OK != read_end_close)
2564 errno = read_end_close_errno;
2565 ret = read_end_close;
2567 else if (GNUNET_OK != write_end_close)
2569 errno = write_end_close_errno;
2570 ret = write_end_close;
2578 * Get the handle to a particular pipe end
2581 * @param n end to access
2582 * @return handle for the respective end
2584 const struct GNUNET_DISK_FileHandle *
2585 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2586 enum GNUNET_DISK_PipeEnd n)
2590 case GNUNET_DISK_PIPE_END_READ:
2591 case GNUNET_DISK_PIPE_END_WRITE:
2601 * Retrieve OS file handle
2603 * @param fh GNUnet file descriptor
2604 * @param dst destination buffer
2605 * @param dst_len length of dst
2606 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
2609 GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
2610 void *dst, size_t dst_len)
2613 return GNUNET_SYSERR;
2615 if (dst_len < sizeof (HANDLE))
2616 return GNUNET_SYSERR;
2617 *((HANDLE *) dst) = fh->h;
2619 if (dst_len < sizeof (int))
2620 return GNUNET_SYSERR;
2621 *((int *) dst) = fh->fd;