2 This file is part of GNUnet.
3 (C) 2001--2012 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"
34 #include "gnunet_crypto_lib.h"
37 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
39 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
41 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
44 * Block size for IO for copying files.
46 #define COPY_BLK_SIZE 65536
50 #if defined(LINUX) || defined(CYGWIN) || defined(GNU)
53 #if defined(SOMEBSD) || defined(DARWIN)
54 #include <sys/param.h>
55 #include <sys/mount.h>
58 #include <sys/types.h>
59 #include <sys/statvfs.h>
64 ULONG PipeSerialNumber;
66 #define _IFMT 0170000 /* type of file */
67 #define _IFLNK 0120000 /* symbolic link */
68 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
70 #error PORT-ME: need to port statfs (how much space is left on the drive?)
76 #if !defined(SOMEBSD) && !defined(DARWIN) && !defined(WINDOWS)
80 #include <sys/statvfs.h>
85 * Handle used to manage a pipe.
87 struct GNUNET_DISK_PipeHandle
90 * File descriptors for the pipe.
91 * One or both of them could be NULL.
93 struct GNUNET_DISK_FileHandle *fd[2];
98 * Closure for the recursion to determine the file size
101 struct GetFileSizeData
104 * Set to the total file size.
109 * GNUNET_YES if symbolic links should be included.
111 int include_sym_links;
114 * GNUNET_YES if mode is file-only (return total == -1 for directories).
116 int single_file_mode;
122 * Translate GNUnet-internal permission bitmap to UNIX file
123 * access permission bitmap.
125 * @param perm file permissions, GNUnet style
126 * @return file permissions, UNIX style
129 translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
134 if (perm & GNUNET_DISK_PERM_USER_READ)
136 if (perm & GNUNET_DISK_PERM_USER_WRITE)
138 if (perm & GNUNET_DISK_PERM_USER_EXEC)
140 if (perm & GNUNET_DISK_PERM_GROUP_READ)
142 if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
144 if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
146 if (perm & GNUNET_DISK_PERM_OTHER_READ)
148 if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
150 if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
159 * Iterate over all files in the given directory and
160 * accumulate their size.
162 * @param cls closure of type "struct GetFileSizeData"
163 * @param fn current filename we are looking at
164 * @return GNUNET_SYSERR on serious errors, otherwise GNUNET_OK
167 getSizeRec (void *cls, const char *fn)
169 struct GetFileSizeData *gfsd = cls;
178 if (0 != STAT64 (fn, &buf))
180 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat64", fn);
181 return GNUNET_SYSERR;
184 if (0 != STAT (fn, &buf))
186 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn);
187 return GNUNET_SYSERR;
190 if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
193 return GNUNET_SYSERR;
195 if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
196 gfsd->total += buf.st_size;
197 if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) &&
198 ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
200 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
201 return GNUNET_SYSERR;
208 * Checks whether a handle is invalid
210 * @param h handle to check
211 * @return GNUNET_YES if invalid, GNUNET_NO if valid
214 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
217 return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO;
219 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
224 * Get the size of an open file.
226 * @param fh open file handle
227 * @param size where to write size of the file
228 * @return GNUNET_OK on success, GNUNET_SYSERR on error
231 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
237 b = GetFileSizeEx (fh->h, &li);
240 SetErrnoFromWinError (GetLastError ());
241 return GNUNET_SYSERR;
243 *size = (OFF_T) li.QuadPart;
247 if (0 != FSTAT (fh->fd, &sbuf))
248 return GNUNET_SYSERR;
249 *size = sbuf.st_size;
256 * Move the read/write pointer in a file
258 * @param h handle of an open file
259 * @param offset position to move to
260 * @param whence specification to which position the offset parameter relates to
261 * @return the new position on success, GNUNET_SYSERR otherwise
264 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, OFF_T offset,
265 enum GNUNET_DISK_Seek whence)
270 return GNUNET_SYSERR;
275 LARGE_INTEGER new_pos;
278 static DWORD t[] = { FILE_BEGIN, FILE_CURRENT, FILE_END };
279 li.QuadPart = offset;
281 b = SetFilePointerEx (h->h, li, &new_pos, t[whence]);
284 SetErrnoFromWinError (GetLastError ());
285 return GNUNET_SYSERR;
287 return (OFF_T) new_pos.QuadPart;
289 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
291 return lseek (h->fd, offset, t[whence]);
297 * Get the size of the file (or directory) of the given file (in
300 * @param filename name of the file or directory
301 * @param size set to the size of the file (or,
302 * in the case of directories, the sum
303 * of all sizes of files in the directory)
304 * @param includeSymLinks should symbolic links be
306 * @param singleFileMode GNUNET_YES to only get size of one file
307 * and return GNUNET_SYSERR for directories.
308 * @return GNUNET_SYSERR on error, GNUNET_OK on success
311 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
312 int includeSymLinks, int singleFileMode)
314 struct GetFileSizeData gfsd;
317 GNUNET_assert (size != NULL);
319 gfsd.include_sym_links = includeSymLinks;
320 gfsd.single_file_mode = singleFileMode;
321 ret = getSizeRec (&gfsd, filename);
328 * Obtain some unique identifiers for the given file
329 * that can be used to identify it in the local system.
330 * This function is used between GNUnet processes to
331 * quickly check if two files with the same absolute path
332 * are actually identical. The two processes represent
333 * the same peer but may communicate over the network
334 * (and the file may be on an NFS volume). This function
335 * may not be supported on all operating systems.
337 * @param filename name of the file
338 * @param dev set to the device ID
339 * @param ino set to the inode ID
340 * @return GNUNET_OK on success
343 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
350 if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf)))
352 *dev = (uint64_t) fbuf.f_fsid;
353 *ino = (uint64_t) sbuf.st_ino;
360 if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf)))
362 *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 ||
363 ((uint64_t) fbuf.f_fsid.val[1]);
364 *ino = (uint64_t) sbuf.st_ino;
368 // FIXME NILS: test this
369 struct GNUNET_DISK_FileHandle *fh;
370 BY_HANDLE_FILE_INFORMATION info;
373 fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 0);
375 return GNUNET_SYSERR;
376 succ = GetFileInformationByHandle (fh->h, &info);
377 GNUNET_DISK_file_close (fh);
380 *dev = info.dwVolumeSerialNumber;
381 *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow);
385 return GNUNET_SYSERR;
388 return GNUNET_SYSERR;
393 * Create the name for a temporary file or directory from a template.
395 * @param t template (without XXXXX or "/tmp/")
396 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error
399 mktemp_name (const char *t)
405 if ((t[0] != '/') && (t[0] != '\\')
407 && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':'))
411 /* FIXME: This uses system codepage on W32, not UTF-8 */
412 tmpdir = getenv ("TMPDIR");
414 tmpdir = getenv ("TMP");
416 tmpdir = getenv ("TEMP");
419 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
423 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
426 fn = (char *) GNUNET_malloc (MAX_PATH + 1);
427 if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
450 tfn = GNUNET_strdup (fn);
451 random_fn = _mktemp (tfn);
452 if (NULL == random_fn)
457 /* FIXME: assume fn to be UTF-8-encoded and do the right thing */
458 if (0 == CreateDirectoryA (tfn, NULL))
460 DWORD error = GetLastError ();
462 if (ERROR_ALREADY_EXISTS == error)
474 * Create an (empty) temporary directory on disk. If the given name is not
475 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
476 * 6 random characters will be appended to the name to create a unique
479 * @param t component to use for the name;
480 * does NOT contain "XXXXXX" or "/tmp/".
481 * @return NULL on error, otherwise name of fresh
482 * file on disk in directory for temporary files
485 GNUNET_DISK_mkdtemp (const char *t)
489 fn = mktemp_name (t);
490 if (fn != mkdtemp (fn))
492 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
501 * Move a file out of the way (create a backup) by
502 * renaming it to "orig.NUM~" where NUM is the smallest
503 * number that is not used yet.
505 * @param fil name of the file to back up
508 GNUNET_DISK_file_backup (const char *fil)
514 slen = strlen (fil) + 20;
515 target = GNUNET_malloc (slen);
519 GNUNET_snprintf (target, slen,
523 } while (0 == access (target, F_OK));
524 if (0 != rename (fil, target))
525 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
528 GNUNET_free (target);
533 * Create an (empty) temporary file on disk. If the given name is not
534 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
535 * 6 random characters will be appended to the name to create a unique
538 * @param t component to use for the name;
539 * does NOT contain "XXXXXX" or "/tmp/".
540 * @return NULL on error, otherwise name of fresh
541 * file on disk in directory for temporary files
544 GNUNET_DISK_mktemp (const char *t)
549 fn = mktemp_name (t);
550 if (-1 == (fd = mkstemp (fn)))
552 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
557 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
563 * Get the number of blocks that are left on the partition that
564 * contains the given file (for normal users).
566 * @param part a file on the partition to check
567 * @return -1 on errors, otherwise the number of free blocks
570 GNUNET_DISK_get_blocks_available (const char *part)
575 if (0 != statvfs (part, &buf))
577 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
585 wchar_t wpath[MAX_PATH + 1];
588 path = GNUNET_STRINGS_filename_expand (part);
591 /* "part" was in UTF-8, and so is "path" */
592 if (ERROR_SUCCESS != plibc_conv_to_win_pathwconv(path, wpath))
598 wcsncpy (szDrive, wpath, 3);
600 if (!GetDiskFreeSpaceW (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
602 LOG (GNUNET_ERROR_TYPE_WARNING, _("`%s' failed for drive `%S': %u\n"),
603 "GetDiskFreeSpace", szDrive, GetLastError ());
611 if (0 != statfs (part, &s))
613 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
622 * Test if "fil" is a directory and listable. Optionally, also check if the
623 * directory is readable. Will not print an error message if the directory does
624 * not exist. Will log errors if GNUNET_SYSERR is returned (i.e., a file exists
625 * with the same name).
627 * @param fil filename to test
628 * @param is_readable GNUNET_YES to additionally check if "fil" is readable;
629 * GNUNET_NO to disable this check
630 * @return GNUNET_YES if yes, GNUNET_NO if not; GNUNET_SYSERR if it
631 * does not exist or stat'ed
634 GNUNET_DISK_directory_test (const char *fil, int is_readable)
636 struct stat filestat;
639 ret = STAT (fil, &filestat);
643 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
644 return GNUNET_SYSERR;
646 if (!S_ISDIR (filestat.st_mode))
648 LOG (GNUNET_ERROR_TYPE_WARNING,
649 "A file already exits with the same name %s\n", fil);
652 if (GNUNET_YES == is_readable)
653 ret = ACCESS (fil, R_OK | X_OK);
655 ret = ACCESS (fil, X_OK);
658 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
666 * Check that fil corresponds to a filename
667 * (of a file that exists and that is not a directory).
669 * @param fil filename to check
670 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
671 * else (will print an error message in that case, too).
674 GNUNET_DISK_file_test (const char *fil)
676 struct stat filestat;
680 rdir = GNUNET_STRINGS_filename_expand (fil);
682 return GNUNET_SYSERR;
684 ret = STAT (rdir, &filestat);
689 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
691 return GNUNET_SYSERR;
696 if (!S_ISREG (filestat.st_mode))
701 if (ACCESS (rdir, F_OK) < 0)
703 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
705 return GNUNET_SYSERR;
713 * Implementation of "mkdir -p"
714 * @param dir the directory to create
715 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure
718 GNUNET_DISK_directory_create (const char *dir)
726 rdir = GNUNET_STRINGS_filename_expand (dir);
728 return GNUNET_SYSERR;
732 pos = 1; /* skip heading '/' */
734 /* Local or Network path? */
735 if (strncmp (rdir, "\\\\", 2) == 0)
740 if (rdir[pos] == '\\')
750 pos = 3; /* strlen("C:\\") */
753 /* Check which low level directories already exist */
755 rdir[len] = DIR_SEPARATOR;
758 if (DIR_SEPARATOR == rdir[pos2])
761 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
762 if (GNUNET_NO == ret)
765 return GNUNET_SYSERR;
767 rdir[pos2] = DIR_SEPARATOR;
768 if (GNUNET_YES == ret)
779 /* Start creating directories */
782 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
785 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
786 if (GNUNET_NO == ret)
789 return GNUNET_SYSERR;
791 if (GNUNET_SYSERR == ret)
794 ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */
796 wchar_t wrdir[MAX_PATH + 1];
797 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(rdir, wrdir))
798 ret = !CreateDirectoryW (wrdir, NULL);
802 if ((ret != 0) && (errno != EEXIST))
804 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
806 return GNUNET_SYSERR;
809 rdir[pos] = DIR_SEPARATOR;
819 * Create the directory structure for storing
822 * @param filename name of a file in the directory
823 * @returns GNUNET_OK on success,
824 * GNUNET_SYSERR on failure,
825 * GNUNET_NO if the directory
826 * exists but is not writeable for us
829 GNUNET_DISK_directory_create_for_file (const char *filename)
835 rdir = GNUNET_STRINGS_filename_expand (filename);
837 return GNUNET_SYSERR;
839 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
842 ret = GNUNET_DISK_directory_create (rdir);
843 if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
851 * Read the contents of a binary file into a buffer.
852 * @param h handle to an open file
853 * @param result the buffer to write the result to
854 * @param len the maximum number of bytes to read
855 * @return the number of bytes read on success, GNUNET_SYSERR on failure
858 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result,
864 return GNUNET_SYSERR;
870 if (h->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
872 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
874 SetErrnoFromWinError (GetLastError ());
875 return GNUNET_SYSERR;
880 if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead))
882 if (GetLastError () != ERROR_IO_PENDING)
884 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ());
885 SetErrnoFromWinError (GetLastError ());
886 return GNUNET_SYSERR;
888 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
889 GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE);
891 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytesRead);
895 return read (h->fd, result, len);
901 * Read the contents of a binary file into a buffer.
902 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
903 * when no data can be read).
905 * @param h handle to an open file
906 * @param result the buffer to write the result to
907 * @param len the maximum number of bytes to read
908 * @return the number of bytes read on success, GNUNET_SYSERR on failure
911 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
918 return GNUNET_SYSERR;
924 if (h->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
926 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
928 SetErrnoFromWinError (GetLastError ());
929 return GNUNET_SYSERR;
934 if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead))
936 if (GetLastError () != ERROR_IO_PENDING)
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ());
939 SetErrnoFromWinError (GetLastError ());
940 return GNUNET_SYSERR;
944 LOG (GNUNET_ERROR_TYPE_DEBUG,
945 "ReadFile() queued a read, cancelling\n");
948 return GNUNET_SYSERR;
951 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead);
958 /* set to non-blocking, read, then set back */
959 flags = fcntl (h->fd, F_GETFL);
960 if (0 == (flags & O_NONBLOCK))
961 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
962 ret = read (h->fd, result, len);
963 if (0 == (flags & O_NONBLOCK))
966 (void) fcntl (h->fd, F_SETFL, flags);
975 * Read the contents of a binary file into a buffer.
977 * @param fn file name
978 * @param result the buffer to write the result to
979 * @param len the maximum number of bytes to read
980 * @return number of bytes read, GNUNET_SYSERR on failure
983 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
985 struct GNUNET_DISK_FileHandle *fh;
988 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
990 return GNUNET_SYSERR;
991 ret = GNUNET_DISK_file_read (fh, result, len);
992 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
999 * Write a buffer to a file.
1000 * @param h handle to open file
1001 * @param buffer the data to write
1002 * @param n number of bytes to write
1003 * @return number of bytes written on success, GNUNET_SYSERR on error
1006 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h,
1007 const void *buffer, size_t n)
1012 return GNUNET_SYSERR;
1018 if (h->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
1020 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
1022 SetErrnoFromWinError (GetLastError ());
1023 return GNUNET_SYSERR;
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n);
1029 if (!WriteFile (h->h, buffer, n, &bytesWritten, h->oOverlapWrite))
1031 if (GetLastError () != ERROR_IO_PENDING)
1033 SetErrnoFromWinError (GetLastError ());
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1036 return GNUNET_SYSERR;
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n");
1039 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE))
1041 SetErrnoFromWinError (GetLastError ());
1042 LOG (GNUNET_ERROR_TYPE_DEBUG,
1043 "Error getting overlapped result while writing to pipe: %u\n",
1045 return GNUNET_SYSERR;
1051 if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE))
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "Error getting control overlapped result while writing to pipe: %u\n",
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "Wrote %u bytes (ovr says %u), picking the greatest\n",
1064 if (bytesWritten == 0)
1068 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytesWritten);
1070 return GNUNET_SYSERR;
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten);
1075 return bytesWritten;
1077 return write (h->fd, buffer, n);
1083 * Write a buffer to a file, blocking, if necessary.
1084 * @param h handle to open file
1085 * @param buffer the data to write
1086 * @param n number of bytes to write
1087 * @return number of bytes written on success, GNUNET_SYSERR on error
1090 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
1091 const void *buffer, size_t n)
1096 return GNUNET_SYSERR;
1101 /* We do a non-overlapped write, which is as blocking as it gets */
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n);
1103 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
1105 SetErrnoFromWinError (GetLastError ());
1106 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1108 return GNUNET_SYSERR;
1110 if (bytesWritten == 0 && n > 0)
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n");
1113 WaitForSingleObject (h->h, INFINITE);
1114 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
1116 SetErrnoFromWinError (GetLastError ());
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
1119 return GNUNET_SYSERR;
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten);
1123 return bytesWritten;
1128 /* set to blocking, write, then set back */
1129 flags = fcntl (h->fd, F_GETFL);
1130 if (0 != (flags & O_NONBLOCK))
1131 (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
1132 ret = write (h->fd, buffer, n);
1133 if (0 == (flags & O_NONBLOCK))
1134 (void) fcntl (h->fd, F_SETFL, flags);
1141 * Write a buffer to a file. If the file is longer than the
1142 * number of bytes that will be written, it will be truncated.
1144 * @param fn file name
1145 * @param buffer the data to write
1146 * @param n number of bytes to write
1147 * @param mode file permissions
1148 * @return number of bytes written on success, GNUNET_SYSERR on error
1151 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
1152 enum GNUNET_DISK_AccessPermissions mode)
1154 struct GNUNET_DISK_FileHandle *fh;
1157 fh = GNUNET_DISK_file_open (fn,
1158 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
1159 | GNUNET_DISK_OPEN_CREATE, mode);
1161 return GNUNET_SYSERR;
1162 ret = GNUNET_DISK_file_write (fh, buffer, n);
1163 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1169 * Scan a directory for files.
1171 * @param dirName the name of the directory
1172 * @param callback the method to call for each file,
1173 * can be NULL, in that case, we only count
1174 * @param callback_cls closure for callback
1175 * @return the number of files found, GNUNET_SYSERR on error or
1176 * ieration aborted by callback returning GNUNET_SYSERR
1179 GNUNET_DISK_directory_scan (const char *dirName,
1180 GNUNET_FileNameCallback callback,
1184 struct dirent *finfo;
1189 unsigned int name_len;
1190 unsigned int n_size;
1192 GNUNET_assert (dirName != NULL);
1193 dname = GNUNET_STRINGS_filename_expand (dirName);
1195 return GNUNET_SYSERR;
1196 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
1197 dname[strlen (dname) - 1] = '\0';
1198 if (0 != STAT (dname, &istat))
1200 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
1201 GNUNET_free (dname);
1202 return GNUNET_SYSERR;
1204 if (!S_ISDIR (istat.st_mode))
1206 LOG (GNUNET_ERROR_TYPE_WARNING, _("Expected `%s' to be a directory!\n"),
1208 GNUNET_free (dname);
1209 return GNUNET_SYSERR;
1212 dinfo = OPENDIR (dname);
1213 if ((errno == EACCES) || (dinfo == NULL))
1215 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
1218 GNUNET_free (dname);
1219 return GNUNET_SYSERR;
1222 n_size = strlen (dname) + name_len + 2;
1223 name = GNUNET_malloc (n_size);
1224 while ((finfo = READDIR (dinfo)) != NULL)
1226 if ((0 == strcmp (finfo->d_name, ".")) ||
1227 (0 == strcmp (finfo->d_name, "..")))
1229 if (callback != NULL)
1231 if (name_len < strlen (finfo->d_name))
1234 name_len = strlen (finfo->d_name);
1235 n_size = strlen (dname) + name_len + 2;
1236 name = GNUNET_malloc (n_size);
1238 /* dname can end in "/" only if dname == "/";
1239 * if dname does not end in "/", we need to add
1240 * a "/" (otherwise, we must not!) */
1241 GNUNET_snprintf (name, n_size, "%s%s%s", dname,
1242 (strcmp (dname, DIR_SEPARATOR_STR) ==
1243 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
1244 if (GNUNET_OK != callback (callback_cls, name))
1248 GNUNET_free (dname);
1249 return GNUNET_SYSERR;
1256 GNUNET_free (dname);
1262 * Opaque handle used for iterating over a directory.
1264 struct GNUNET_DISK_DirectoryIterator
1268 * Function to call on directory entries.
1270 GNUNET_DISK_DirectoryIteratorCallback callback;
1273 * Closure for callback.
1278 * Reference to directory.
1288 * Next filename to process.
1295 enum GNUNET_SCHEDULER_Priority priority;
1301 * Task used by the directory iterator.
1304 directory_iterator_task (void *cls,
1305 const struct GNUNET_SCHEDULER_TaskContext *tc)
1307 struct GNUNET_DISK_DirectoryIterator *iter = cls;
1310 name = iter->next_name;
1311 GNUNET_assert (name != NULL);
1312 iter->next_name = NULL;
1313 iter->callback (iter->callback_cls, iter, name, iter->dirname);
1319 * This function must be called during the DiskIteratorCallback
1320 * (exactly once) to schedule the task to process the next
1321 * filename in the directory (if there is one).
1323 * @param iter opaque handle for the iterator
1324 * @param can set to GNUNET_YES to terminate the iteration early
1325 * @return GNUNET_YES if iteration will continue,
1326 * GNUNET_NO if this was the last entry (and iteration is complete),
1327 * GNUNET_SYSERR if abort was YES
1330 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
1333 struct dirent *finfo;
1335 GNUNET_assert (iter->next_name == NULL);
1336 if (can == GNUNET_YES)
1338 CLOSEDIR (iter->directory);
1339 GNUNET_free (iter->dirname);
1341 return GNUNET_SYSERR;
1343 while (NULL != (finfo = READDIR (iter->directory)))
1345 if ((0 == strcmp (finfo->d_name, ".")) ||
1346 (0 == strcmp (finfo->d_name, "..")))
1348 GNUNET_asprintf (&iter->next_name, "%s%s%s", iter->dirname,
1349 DIR_SEPARATOR_STR, finfo->d_name);
1354 GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
1357 GNUNET_SCHEDULER_add_with_priority (iter->priority, &directory_iterator_task,
1364 * Scan a directory for files using the scheduler to run a task for
1365 * each entry. The name of the directory must be expanded first (!).
1366 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
1367 * may provide a simpler API.
1369 * @param prio priority to use
1370 * @param dirName the name of the directory
1371 * @param callback the method to call for each file
1372 * @param callback_cls closure for callback
1373 * @return GNUNET_YES if directory is not empty and 'callback'
1374 * will be called later, GNUNET_NO otherwise, GNUNET_SYSERR on error.
1377 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
1378 const char *dirName,
1379 GNUNET_DISK_DirectoryIteratorCallback
1380 callback, void *callback_cls)
1382 struct GNUNET_DISK_DirectoryIterator *di;
1384 di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
1385 di->callback = callback;
1386 di->callback_cls = callback_cls;
1387 di->directory = OPENDIR (dirName);
1388 if (di->directory == NULL)
1391 callback (callback_cls, NULL, NULL, NULL);
1392 return GNUNET_SYSERR;
1394 di->dirname = GNUNET_strdup (dirName);
1395 di->priority = prio;
1396 return GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
1401 * Function that removes the given directory by calling
1402 * "GNUNET_DISK_directory_remove".
1404 * @param unused not used
1405 * @param fn directory to remove
1409 remove_helper (void *unused, const char *fn)
1411 (void) GNUNET_DISK_directory_remove (fn);
1417 * Remove all files in a directory (rm -rf). Call with
1421 * @param filename the file to remove
1422 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1425 GNUNET_DISK_directory_remove (const char *filename)
1429 if (0 != LSTAT (filename, &istat))
1430 return GNUNET_NO; /* file may not exist... */
1431 (void) CHMOD (filename, S_IWUSR | S_IRUSR | S_IXUSR);
1432 if (UNLINK (filename) == 0)
1434 if ((errno != EISDIR) &&
1435 /* EISDIR is not sufficient in all cases, e.g.
1436 * sticky /tmp directory may result in EPERM on BSD.
1437 * So we also explicitly check "isDirectory" */
1438 (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES)))
1440 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1441 return GNUNET_SYSERR;
1443 if (GNUNET_SYSERR ==
1444 GNUNET_DISK_directory_scan (filename, &remove_helper, NULL))
1445 return GNUNET_SYSERR;
1446 if (0 != RMDIR (filename))
1448 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1449 return GNUNET_SYSERR;
1458 * @param src file to copy
1459 * @param dst destination file name
1460 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1463 GNUNET_DISK_file_copy (const char *src, const char *dst)
1469 struct GNUNET_DISK_FileHandle *in;
1470 struct GNUNET_DISK_FileHandle *out;
1472 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1473 return GNUNET_SYSERR;
1475 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
1476 GNUNET_DISK_PERM_NONE);
1478 return GNUNET_SYSERR;
1480 GNUNET_DISK_file_open (dst,
1481 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
1482 GNUNET_DISK_OPEN_FAILIFEXISTS,
1483 GNUNET_DISK_PERM_USER_READ |
1484 GNUNET_DISK_PERM_USER_WRITE |
1485 GNUNET_DISK_PERM_GROUP_READ |
1486 GNUNET_DISK_PERM_GROUP_WRITE);
1489 GNUNET_DISK_file_close (in);
1490 return GNUNET_SYSERR;
1492 buf = GNUNET_malloc (COPY_BLK_SIZE);
1495 len = COPY_BLK_SIZE;
1496 if (len > size - pos)
1498 if (len != GNUNET_DISK_file_read (in, buf, len))
1500 if (len != GNUNET_DISK_file_write (out, buf, len))
1505 GNUNET_DISK_file_close (in);
1506 GNUNET_DISK_file_close (out);
1510 GNUNET_DISK_file_close (in);
1511 GNUNET_DISK_file_close (out);
1512 return GNUNET_SYSERR;
1517 * @brief Removes special characters as ':' from a filename.
1518 * @param fn the filename to canonicalize
1521 GNUNET_DISK_filename_canonicalize (char *fn)
1531 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' ||
1532 c == '<' || c == '>' || c == '|')
1544 * @brief Change owner of a file
1546 * @param filename name of file to change the owner of
1547 * @param user name of the new owner
1548 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1551 GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1556 pws = getpwnam (user);
1559 LOG (GNUNET_ERROR_TYPE_ERROR,
1560 _("Cannot obtain information about user `%s': %s\n"), user,
1562 return GNUNET_SYSERR;
1564 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1565 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1572 * Lock a part of a file
1573 * @param fh file handle
1574 * @param lockStart absolute position from where to lock
1575 * @param lockEnd absolute position until where to lock
1576 * @param excl GNUNET_YES for an exclusive lock
1577 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1580 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
1581 OFF_T lockEnd, int excl)
1586 return GNUNET_SYSERR;
1592 memset (&fl, 0, sizeof (struct flock));
1593 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1594 fl.l_whence = SEEK_SET;
1595 fl.l_start = lockStart;
1598 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1601 OFF_T diff = lockEnd - lockStart;
1602 DWORD diff_low, diff_high;
1603 diff_low = (DWORD) (diff & 0xFFFFFFFF);
1604 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1606 memset (&o, 0, sizeof (OVERLAPPED));
1607 o.Offset = (DWORD) (lockStart & 0xFFFFFFFF);;
1608 o.OffsetHigh = (DWORD) (((lockStart & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1611 (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY,
1612 0, diff_low, diff_high, &o))
1614 SetErrnoFromWinError (GetLastError ());
1615 return GNUNET_SYSERR;
1624 * Unlock a part of a file
1625 * @param fh file handle
1626 * @param unlockStart absolute position from where to unlock
1627 * @param unlockEnd absolute position until where to unlock
1628 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1631 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
1637 return GNUNET_SYSERR;
1643 memset (&fl, 0, sizeof (struct flock));
1644 fl.l_type = F_UNLCK;
1645 fl.l_whence = SEEK_SET;
1646 fl.l_start = unlockStart;
1647 fl.l_len = unlockEnd;
1649 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1652 OFF_T diff = unlockEnd - unlockStart;
1653 DWORD diff_low, diff_high;
1654 diff_low = (DWORD) (diff & 0xFFFFFFFF);
1655 diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1657 memset (&o, 0, sizeof (OVERLAPPED));
1658 o.Offset = (DWORD) (unlockStart & 0xFFFFFFFF);;
1659 o.OffsetHigh = (DWORD) (((unlockStart & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
1661 if (!UnlockFileEx (fh->h, 0, diff_low, diff_high, &o))
1663 SetErrnoFromWinError (GetLastError ());
1664 return GNUNET_SYSERR;
1673 * Open a file. Note that the access permissions will only be
1674 * used if a new file is created and if the underlying operating
1675 * system supports the given permissions.
1677 * @param fn file name to be opened
1678 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1679 * @param perm permissions for the newly created file, use
1680 * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this
1681 * call (because of flags)
1682 * @return IO handle on success, NULL on error
1684 struct GNUNET_DISK_FileHandle *
1685 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
1686 enum GNUNET_DISK_AccessPermissions perm)
1689 struct GNUNET_DISK_FileHandle *ret;
1695 wchar_t wexpfn[MAX_PATH + 1];
1702 expfn = GNUNET_STRINGS_filename_expand (fn);
1707 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1708 oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1709 else if (flags & GNUNET_DISK_OPEN_READ)
1711 else if (flags & GNUNET_DISK_OPEN_WRITE)
1716 GNUNET_free (expfn);
1719 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1720 oflags |= (O_CREAT | O_EXCL);
1721 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1723 if (flags & GNUNET_DISK_OPEN_APPEND)
1725 if (flags & GNUNET_DISK_OPEN_CREATE)
1727 (void) GNUNET_DISK_directory_create_for_file (expfn);
1729 mode = translate_unix_perms (perm);
1732 fd = open (expfn, oflags | O_LARGEFILE, mode);
1735 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1736 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1738 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1739 GNUNET_free (expfn);
1746 if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE))
1747 access = FILE_READ_DATA | FILE_WRITE_DATA;
1748 else if (flags & GNUNET_DISK_OPEN_READ)
1749 access = FILE_READ_DATA;
1750 else if (flags & GNUNET_DISK_OPEN_WRITE)
1751 access = FILE_WRITE_DATA;
1753 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1757 else if (flags & GNUNET_DISK_OPEN_CREATE)
1759 (void) GNUNET_DISK_directory_create_for_file (expfn);
1760 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1761 disp = CREATE_ALWAYS;
1765 else if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1767 disp = TRUNCATE_EXISTING;
1771 disp = OPEN_EXISTING;
1774 if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(expfn, wexpfn))
1775 h = CreateFileW (wexpfn, access,
1776 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1777 disp, FILE_ATTRIBUTE_NORMAL, NULL);
1779 h = INVALID_HANDLE_VALUE;
1780 if (h == INVALID_HANDLE_VALUE)
1783 SetErrnoFromWinError (GetLastError ());
1785 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_INFO, "open", expfn);
1786 GNUNET_free (expfn);
1791 if (flags & GNUNET_DISK_OPEN_APPEND)
1792 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1794 SetErrnoFromWinError (GetLastError ());
1795 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn);
1797 GNUNET_free (expfn);
1802 ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
1805 ret->type = GNUNET_DISK_HANLDE_TYPE_FILE;
1809 GNUNET_free (expfn);
1815 * Close an open file
1816 * @param h file handle
1817 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1820 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1826 return GNUNET_SYSERR;
1832 if (!CloseHandle (h->h))
1834 SetErrnoFromWinError (GetLastError ());
1835 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1836 ret = GNUNET_SYSERR;
1838 if (h->oOverlapRead)
1840 if (!CloseHandle (h->oOverlapRead->hEvent))
1842 SetErrnoFromWinError (GetLastError ());
1843 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1844 ret = GNUNET_SYSERR;
1846 GNUNET_free (h->oOverlapRead);
1848 if (h->oOverlapWrite)
1850 if (!CloseHandle (h->oOverlapWrite->hEvent))
1852 SetErrnoFromWinError (GetLastError ());
1853 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1854 ret = GNUNET_SYSERR;
1856 GNUNET_free (h->oOverlapWrite);
1859 if (close (h->fd) != 0)
1861 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1862 ret = GNUNET_SYSERR;
1871 * Get a GNUnet file handle from a W32 handle.
1873 * @param handle native handle
1874 * @return GNUnet file handle corresponding to the W32 handle
1876 struct GNUNET_DISK_FileHandle *
1877 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh)
1879 struct GNUNET_DISK_FileHandle *fh;
1882 enum GNUNET_FILE_Type ftype;
1884 dwret = GetFileType (osfh);
1887 case FILE_TYPE_DISK:
1888 ftype = GNUNET_DISK_HANLDE_TYPE_FILE;
1890 case FILE_TYPE_PIPE:
1891 ftype = GNUNET_DISK_HANLDE_TYPE_PIPE;
1897 fh = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
1901 if (ftype == GNUNET_DISK_HANLDE_TYPE_PIPE)
1904 * Note that we can't make it overlapped if it isn't already.
1905 * (ReOpenFile() is only available in 2003/Vista).
1906 * The process that opened this file in the first place (usually a parent
1907 * process, if this is stdin/stdout/stderr) must make it overlapped,
1908 * otherwise we're screwed, as selecting on non-overlapped handle
1911 fh->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
1912 fh->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
1913 fh->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
1914 fh->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
1922 * Get a handle from a native integer FD.
1924 * @param fd native integer file descriptor
1925 * @return file handle corresponding to the descriptor
1927 struct GNUNET_DISK_FileHandle *
1928 GNUNET_DISK_get_handle_from_int_fd (int fno)
1930 struct GNUNET_DISK_FileHandle *fh;
1933 fh = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
1939 osfh = _get_osfhandle (fno);
1940 if (INVALID_HANDLE_VALUE == (HANDLE) osfh)
1943 fh = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) osfh);
1951 * Get a handle from a native streaming FD.
1953 * @param fd native streaming file descriptor
1954 * @return file handle corresponding to the descriptor
1956 struct GNUNET_DISK_FileHandle *
1957 GNUNET_DISK_get_handle_from_native (FILE *fd)
1965 return GNUNET_DISK_get_handle_from_int_fd (fno);
1970 * Construct full path to a file inside of the private
1971 * directory used by GNUnet. Also creates the corresponding
1972 * directory. If the resulting name is supposed to be
1973 * a directory, end the last argument in '/' (or pass
1974 * DIR_SEPARATOR_STR as the last argument before NULL).
1976 * @param cfg configuration to use (determines HOME)
1977 * @param serviceName name of the service
1978 * @param ... is NULL-terminated list of
1979 * path components to append to the
1980 * private directory name.
1981 * @return the constructed filename
1984 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1985 const char *serviceName, ...)
1991 unsigned int needed;
1994 GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", &pfx))
1998 LOG (GNUNET_ERROR_TYPE_WARNING,
1999 _("No `%s' specified for service `%s' in configuration.\n"), "HOME",
2003 needed = strlen (pfx) + 2;
2004 if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\'))
2006 va_start (ap, serviceName);
2009 c = va_arg (ap, const char *);
2013 needed += strlen (c);
2014 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
2018 ret = GNUNET_malloc (needed);
2021 va_start (ap, serviceName);
2024 c = va_arg (ap, const char *);
2028 if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\'))
2029 strcat (ret, DIR_SEPARATOR_STR);
2033 if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\'))
2034 (void) GNUNET_DISK_directory_create_for_file (ret);
2036 (void) GNUNET_DISK_directory_create (ret);
2042 * Handle for a memory-mapping operation.
2044 struct GNUNET_DISK_MapHandle
2047 * Address where the map is in memory.
2053 * Underlying OS handle.
2058 * Number of bytes mapped.
2066 #define MAP_FAILED ((void *) -1)
2070 * Map a file into memory
2072 * @param h open file handle
2073 * @param m handle to the new mapping
2074 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
2075 * @param len size of the mapping
2076 * @return pointer to the mapped memory region, NULL on failure
2079 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
2080 struct GNUNET_DISK_MapHandle **m,
2081 enum GNUNET_DISK_MapType access, size_t len)
2090 DWORD mapAccess, protect;
2092 if ((access & GNUNET_DISK_MAP_TYPE_READ) &&
2093 (access & GNUNET_DISK_MAP_TYPE_WRITE))
2095 protect = PAGE_READWRITE;
2096 mapAccess = FILE_MAP_ALL_ACCESS;
2098 else if (access & GNUNET_DISK_MAP_TYPE_READ)
2100 protect = PAGE_READONLY;
2101 mapAccess = FILE_MAP_READ;
2103 else if (access & GNUNET_DISK_MAP_TYPE_WRITE)
2105 protect = PAGE_READWRITE;
2106 mapAccess = FILE_MAP_WRITE;
2114 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
2115 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
2116 if ((*m)->h == INVALID_HANDLE_VALUE)
2118 SetErrnoFromWinError (GetLastError ());
2123 (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
2126 SetErrnoFromWinError (GetLastError ());
2127 CloseHandle ((*m)->h);
2136 if (access & GNUNET_DISK_MAP_TYPE_READ)
2138 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
2140 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
2141 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
2142 GNUNET_assert (NULL != (*m)->addr);
2143 if (MAP_FAILED == (*m)->addr)
2155 * @param h mapping handle
2156 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2159 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
2166 return GNUNET_SYSERR;
2170 ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR;
2171 if (ret != GNUNET_OK)
2172 SetErrnoFromWinError (GetLastError ());
2173 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
2175 ret = GNUNET_SYSERR;
2176 SetErrnoFromWinError (GetLastError ());
2179 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
2187 * Write file changes to disk
2188 * @param h handle to an open file
2189 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2192 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
2197 return GNUNET_SYSERR;
2203 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
2204 if (ret != GNUNET_OK)
2205 SetErrnoFromWinError (GetLastError ());
2207 #elif defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN)
2208 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
2210 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
2216 /* Copyright Bob Byrnes <byrnes <at> curl.com>
2217 http://permalink.gmane.org/gmane.os.cygwin.patches/2121
2219 /* Create a pipe, and return handles to the read and write ends,
2220 just like CreatePipe, but ensure that the write end permits
2221 FILE_READ_ATTRIBUTES access, on later versions of win32 where
2222 this is supported. This access is needed by NtQueryInformationFile,
2223 which is used to implement select and nonblocking writes.
2224 Note that the return value is either NO_ERROR or GetLastError,
2225 unlike CreatePipe, which returns a bool for success or failure. */
2227 create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
2228 LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize,
2229 DWORD dwReadMode, DWORD dwWriteMode)
2231 /* Default to error. */
2232 *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;
2237 /* Ensure that there is enough pipe buffer space for atomic writes. */
2238 if (psize < PIPE_BUF)
2241 char pipename[MAX_PATH];
2243 /* Retry CreateNamedPipe as long as the pipe name is in use.
2244 * Retrying will probably never be necessary, but we want
2245 * to be as robust as possible. */
2248 static volatile LONG pipe_unique_id;
2250 snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld",
2251 getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id));
2252 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n",
2254 /* Use CreateNamedPipe instead of CreatePipe, because the latter
2255 * returns a write handle that does not permit FILE_READ_ATTRIBUTES
2256 * access, on versions of win32 earlier than WinXP SP2.
2257 * CreatePipe also stupidly creates a full duplex pipe, which is
2258 * a waste, since only a single direction is actually used.
2259 * It's important to only allow a single instance, to ensure that
2260 * the pipe was not created earlier by some other process, even if
2261 * the pid has been reused. */
2262 read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */
2263 psize, /* output buffer size */
2264 psize, /* input buffer size */
2265 NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
2267 if (read_pipe != INVALID_HANDLE_VALUE)
2269 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe);
2273 DWORD err = GetLastError ();
2277 case ERROR_PIPE_BUSY:
2278 /* The pipe is already open with compatible parameters.
2279 * Pick a new name and retry. */
2280 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n");
2282 case ERROR_ACCESS_DENIED:
2283 /* The pipe is already open with incompatible parameters.
2284 * Pick a new name and retry. */
2285 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n");
2287 case ERROR_CALL_NOT_IMPLEMENTED:
2288 /* We are on an older Win9x platform without named pipes.
2289 * Return an anonymous pipe as the best approximation. */
2290 LOG (GNUNET_ERROR_TYPE_DEBUG,
2291 "CreateNamedPipe not implemented, resorting to "
2292 "CreatePipe: size = %lu\n", psize);
2293 if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p, write handle = %p\n",
2300 err = GetLastError ();
2301 LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err);
2304 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err);
2309 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename);
2311 /* Open the named pipe for writing.
2312 * Be sure to permit FILE_READ_ATTRIBUTES access. */
2313 write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */
2314 sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */
2315 0); /* handle to template file */
2317 if (write_pipe == INVALID_HANDLE_VALUE)
2320 DWORD err = GetLastError ();
2322 LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err);
2323 CloseHandle (read_pipe);
2326 LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe);
2328 *read_pipe_ptr = read_pipe;
2329 *write_pipe_ptr = write_pipe;
2336 * Creates an interprocess channel
2338 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
2339 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
2340 * @param inherit_read inherit the parent processes stdin (only for windows)
2341 * @param inherit_write inherit the parent processes stdout (only for windows)
2342 * @return handle to the new pipe, NULL on error
2344 struct GNUNET_DISK_PipeHandle *
2345 GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write)
2356 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
2360 return GNUNET_DISK_pipe_from_fd (blocking_read,
2364 struct GNUNET_DISK_PipeHandle *p;
2369 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle));
2370 p->fd[0] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
2371 p->fd[1] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
2373 /* All pipes are overlapped. If you want them to block - just
2374 * call WriteFile() and ReadFile() with NULL overlapped pointer.
2375 * NOTE: calling with NULL overlapped pointer works only
2376 * for pipes, and doesn't seem to be a documented feature.
2377 * It will NOT work for files, because overlapped files need
2378 * to read offsets from the overlapped structure, regardless.
2379 * Pipes are not seekable, and need no offsets, which is
2380 * probably why it works for them.
2383 create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0,
2384 FILE_FLAG_OVERLAPPED,
2385 FILE_FLAG_OVERLAPPED);
2388 SetErrnoFromWinError (GetLastError ());
2390 GNUNET_free (p->fd[0]);
2391 GNUNET_free (p->fd[1]);
2396 if (!DuplicateHandle
2397 (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0,
2398 inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
2400 SetErrnoFromWinError (GetLastError ());
2402 CloseHandle (p->fd[0]->h);
2403 CloseHandle (p->fd[1]->h);
2404 GNUNET_free (p->fd[0]);
2405 GNUNET_free (p->fd[1]);
2410 CloseHandle (p->fd[0]->h);
2411 p->fd[0]->h = tmp_handle;
2413 if (!DuplicateHandle
2414 (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0,
2415 inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS))
2417 SetErrnoFromWinError (GetLastError ());
2419 CloseHandle (p->fd[0]->h);
2420 CloseHandle (p->fd[1]->h);
2421 GNUNET_free (p->fd[0]);
2422 GNUNET_free (p->fd[1]);
2427 CloseHandle (p->fd[1]->h);
2428 p->fd[1]->h = tmp_handle;
2430 p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2431 p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2433 p->fd[0]->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
2434 p->fd[0]->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
2435 p->fd[1]->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
2436 p->fd[1]->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
2438 p->fd[0]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2439 p->fd[0]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2441 p->fd[1]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2442 p->fd[1]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2450 * Creates a pipe object from a couple of file descriptors.
2451 * Useful for wrapping existing pipe FDs.
2453 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
2454 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
2455 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
2457 * @return handle to the new pipe, NULL on error
2459 struct GNUNET_DISK_PipeHandle *
2460 GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
2462 struct GNUNET_DISK_PipeHandle *p;
2464 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle));
2469 int eno = 0; /* make gcc happy */
2474 p->fd[0] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
2475 p->fd[0]->fd = fd[0];
2478 flags = fcntl (fd[0], F_GETFL);
2479 flags |= O_NONBLOCK;
2480 if (0 > fcntl (fd[0], F_SETFL, flags))
2486 flags = fcntl (fd[0], F_GETFD);
2487 flags |= FD_CLOEXEC;
2488 if (0 > fcntl (fd[0], F_SETFD, flags))
2497 p->fd[1] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
2498 p->fd[1]->fd = fd[1];
2499 if (!blocking_write)
2501 flags = fcntl (fd[1], F_GETFL);
2502 flags |= O_NONBLOCK;
2503 if (0 > fcntl (fd[1], F_SETFL, flags))
2509 flags = fcntl (fd[1], F_GETFD);
2510 flags |= FD_CLOEXEC;
2511 if (0 > fcntl (fd[1], F_SETFD, flags))
2520 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
2521 if (p->fd[0]->fd >= 0)
2522 GNUNET_break (0 == close (p->fd[0]->fd));
2523 if (p->fd[1]->fd >= 0)
2524 GNUNET_break (0 == close (p->fd[1]->fd));
2525 GNUNET_free_non_null (p->fd[0]);
2526 GNUNET_free_non_null (p->fd[1]);
2534 p->fd[0] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
2535 p->fd[0]->h = (HANDLE) _get_osfhandle (fd[0]);
2536 if (p->fd[0]->h != INVALID_HANDLE_VALUE)
2538 p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2539 p->fd[0]->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
2540 p->fd[0]->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
2541 p->fd[0]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2542 p->fd[0]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2546 GNUNET_free (p->fd[0]);
2552 p->fd[1] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
2553 p->fd[1]->h = (HANDLE) _get_osfhandle (fd[1]);
2554 if (p->fd[1]->h != INVALID_HANDLE_VALUE)
2556 p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
2557 p->fd[1]->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
2558 p->fd[1]->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
2559 p->fd[1]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2560 p->fd[1]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
2564 GNUNET_free (p->fd[1]);
2575 * Closes an interprocess channel
2577 * @param p pipe to close
2578 * @param end which end of the pipe to close
2579 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2582 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
2583 enum GNUNET_DISK_PipeEnd end)
2585 int ret = GNUNET_OK;
2587 if (end == GNUNET_DISK_PIPE_END_READ)
2591 ret = GNUNET_DISK_file_close (p->fd[0]);
2595 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2599 ret = GNUNET_DISK_file_close (p->fd[1]);
2608 * Detaches one of the ends from the pipe.
2609 * Detached end is a fully-functional FileHandle, it will
2610 * not be affected by anything you do with the pipe afterwards.
2611 * Each end of a pipe can only be detched from it once (i.e.
2612 * it is not duplicated).
2614 * @param p pipe to detach an end from
2615 * @param end which end of the pipe to detach
2616 * @return Detached end on success, NULL on failure
2617 * (or if that end is not present or is closed).
2619 struct GNUNET_DISK_FileHandle *
2620 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
2621 enum GNUNET_DISK_PipeEnd end)
2623 struct GNUNET_DISK_FileHandle *ret = NULL;
2625 if (end == GNUNET_DISK_PIPE_END_READ)
2633 else if (end == GNUNET_DISK_PIPE_END_WRITE)
2647 * Closes an interprocess channel
2649 * @param p pipe to close
2650 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2653 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
2655 int ret = GNUNET_OK;
2658 int write_end_close;
2659 int read_end_close_errno;
2660 int write_end_close_errno;
2662 read_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_READ);
2663 read_end_close_errno = errno;
2664 write_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_WRITE);
2665 write_end_close_errno = errno;
2668 if (GNUNET_OK != read_end_close)
2670 errno = read_end_close_errno;
2671 ret = read_end_close;
2673 else if (GNUNET_OK != write_end_close)
2675 errno = write_end_close_errno;
2676 ret = write_end_close;
2684 * Get the handle to a particular pipe end
2687 * @param n end to access
2688 * @return handle for the respective end
2690 const struct GNUNET_DISK_FileHandle *
2691 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
2692 enum GNUNET_DISK_PipeEnd n)
2696 case GNUNET_DISK_PIPE_END_READ:
2697 case GNUNET_DISK_PIPE_END_WRITE:
2707 * Retrieve OS file handle
2709 * @param fh GNUnet file descriptor
2710 * @param dst destination buffer
2711 * @param dst_len length of dst
2712 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
2715 GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
2716 void *dst, size_t dst_len)
2719 if (dst_len < sizeof (HANDLE))
2720 return GNUNET_SYSERR;
2721 *((HANDLE *) dst) = fh->h;
2723 if (dst_len < sizeof (int))
2724 return GNUNET_SYSERR;
2725 *((int *) dst) = fh->fd;