aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-10-05 12:51:05 +0000
committerChristian Grothoff <christian@grothoff.org>2009-10-05 12:51:05 +0000
commitf811206714d9cef29580a1944a0ef55d7be4972a (patch)
tree1c09388397dd93f2006e8737f2b1f369bfe7923a /src/util
parenta8594b108fb91bf69b56cc69fe918e12ef43357b (diff)
downloadgnunet-f811206714d9cef29580a1944a0ef55d7be4972a.tar.gz
gnunet-f811206714d9cef29580a1944a0ef55d7be4972a.zip
code clean up
Diffstat (limited to 'src/util')
-rw-r--r--src/util/bio.c3
-rw-r--r--src/util/crypto_hash.c3
-rw-r--r--src/util/crypto_rsa.c3
-rw-r--r--src/util/disk.c326
-rw-r--r--src/util/server.c11
-rw-r--r--src/util/test_scheduler.c4
6 files changed, 229 insertions, 121 deletions
diff --git a/src/util/bio.c b/src/util/bio.c
index 8c3daed15..83afa34f3 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -56,7 +56,8 @@ GNUNET_BIO_read_open (const char *fn)
56 struct GNUNET_DISK_FileHandle *fd; 56 struct GNUNET_DISK_FileHandle *fd;
57 struct GNUNET_BIO_ReadHandle *h; 57 struct GNUNET_BIO_ReadHandle *h;
58 58
59 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ); 59 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
60 GNUNET_DISK_PERM_NONE);
60 if (NULL == fd) 61 if (NULL == fd)
61 return NULL; 62 return NULL;
62 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); 63 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index ec2a79602..c3fb9a84c 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -523,7 +523,8 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
523 } 523 }
524 fhc->run_on_shutdown = run_on_shutdown; 524 fhc->run_on_shutdown = run_on_shutdown;
525 fhc->fh = GNUNET_DISK_file_open (filename, 525 fhc->fh = GNUNET_DISK_file_open (filename,
526 GNUNET_DISK_OPEN_READ); 526 GNUNET_DISK_OPEN_READ,
527 GNUNET_DISK_PERM_NONE);
527 if (!fhc->fh) 528 if (!fhc->fh)
528 { 529 {
529 file_hash_finish (fhc, NULL); 530 file_hash_finish (fhc, NULL);
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index ef2e328e9..7bffa71ee 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -615,7 +615,8 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
615 return ret; 615 return ret;
616 } 616 }
617 /* hostkey file exists already, read it! */ 617 /* hostkey file exists already, read it! */
618 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ); 618 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
619 GNUNET_DISK_PERM_NONE);
619 if (NULL == fd) 620 if (NULL == fd)
620 { 621 {
621 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); 622 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
diff --git a/src/util/disk.c b/src/util/disk.c
index ee327e03f..46a71b48f 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -34,6 +34,13 @@
34#include "disk.h" 34#include "disk.h"
35 35
36 36
37/**
38 * Block size for IO for copying files.
39 */
40#define COPY_BLK_SIZE 65536
41
42
43
37#if LINUX || CYGWIN 44#if LINUX || CYGWIN
38#include <sys/vfs.h> 45#include <sys/vfs.h>
39#else 46#else
@@ -68,19 +75,53 @@
68#endif 75#endif
69#endif 76#endif
70#endif 77#endif
78#if LINUX
79#include <sys/statvfs.h>
80#endif
81
71 82
72typedef struct 83/**
84 * Handle used to manage a pipe.
85 */
86struct GNUNET_DISK_PipeHandle
73{ 87{
88 /**
89 * File descriptors for the pipe.
90 */
91 struct GNUNET_DISK_FileHandle * fd[2];
92};
93
94
95/**
96 * Closure for the recursion to determine the file size
97 * of a directory.
98 */
99struct GetFileSizeData
100{
101 /**
102 * Set to the total file size.
103 */
74 uint64_t total; 104 uint64_t total;
75 int include_sym_links;
76} GetFileSizeData;
77 105
106 /**
107 * GNUNET_YES if symbolic links should be included.
108 */
109 int include_sym_links;
110};
78 111
79 112
113/**
114 * Iterate over all files in the given directory and
115 * accumulate their size.
116 *
117 * @param cls closure of type "struct GetFileSizeData"
118 * @param fn current filename we are looking at
119 * @return GNUNET_SYSERR on serious errors, otherwise GNUNET_OK
120 */
80static int 121static int
81getSizeRec (void *ptr, const char *fn) 122getSizeRec (void *cls, const char *fn)
82{ 123{
83 GetFileSizeData *gfsd = ptr; 124 struct GetFileSizeData *gfsd = cls;
84#ifdef HAVE_STAT64 125#ifdef HAVE_STAT64
85 struct stat64 buf; 126 struct stat64 buf;
86#else 127#else
@@ -112,8 +153,10 @@ getSizeRec (void *ptr, const char *fn)
112 return GNUNET_OK; 153 return GNUNET_OK;
113} 154}
114 155
156
115/** 157/**
116 * Checks whether a handle is invalid 158 * Checks whether a handle is invalid
159 *
117 * @param h handle to check 160 * @param h handle to check
118 * @return GNUNET_YES if invalid, GNUNET_NO if valid 161 * @return GNUNET_YES if invalid, GNUNET_NO if valid
119 */ 162 */
@@ -121,15 +164,16 @@ int
121GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) 164GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
122{ 165{
123#ifdef MINGW 166#ifdef MINGW
124 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO; 167 return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO;
125#else 168#else
126 return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO; 169 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
127#endif 170#endif
128} 171}
129 172
130 173
131/** 174/**
132 * Move the read/write pointer in a file 175 * Move the read/write pointer in a file
176 *
133 * @param h handle of an open file 177 * @param h handle of an open file
134 * @param offset position to move to 178 * @param offset position to move to
135 * @param whence specification to which position the offset parameter relates to 179 * @param whence specification to which position the offset parameter relates to
@@ -137,7 +181,7 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
137 */ 181 */
138off_t 182off_t
139GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset, 183GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
140 enum GNUNET_DISK_Seek whence) 184 enum GNUNET_DISK_Seek whence)
141{ 185{
142 if (h == NULL) 186 if (h == NULL)
143 { 187 {
@@ -165,10 +209,17 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
165#endif 209#endif
166} 210}
167 211
212
168/** 213/**
169 * Get the size of the file (or directory) 214 * Get the size of the file (or directory) of the given file (in
170 * of the given file (in bytes). 215 * bytes).
171 * 216 *
217 * @param filename name of the file or directory
218 * @param size set to the size of the file (or,
219 * in the case of directories, the sum
220 * of all sizes of files in the directory)
221 * @param includeSymLinks should symbolic links be
222 * included?
172 * @return GNUNET_SYSERR on error, GNUNET_OK on success 223 * @return GNUNET_SYSERR on error, GNUNET_OK on success
173 */ 224 */
174int 225int
@@ -176,7 +227,7 @@ GNUNET_DISK_file_size (const char *filename,
176 uint64_t *size, 227 uint64_t *size,
177 int includeSymLinks) 228 int includeSymLinks)
178{ 229{
179 GetFileSizeData gfsd; 230 struct GetFileSizeData gfsd;
180 int ret; 231 int ret;
181 232
182 GNUNET_assert (size != NULL); 233 GNUNET_assert (size != NULL);
@@ -188,15 +239,15 @@ GNUNET_DISK_file_size (const char *filename,
188} 239}
189 240
190 241
191
192#if LINUX
193#include <sys/statvfs.h>
194#endif
195
196
197
198/** 242/**
199 * FIXME. 243 * Obtain some unique identifiers for the given file
244 * that can be used to identify it in the local system.
245 * This function is used between GNUnet processes to
246 * quickly check if two files with the same absolute path
247 * are actually identical. The two processes represent
248 * the same peer but may communicate over the network
249 * (and the file may be on an NFS volume). This function
250 * may not be supported on all operating systems.
200 * 251 *
201 * @param filename name of the file 252 * @param filename name of the file
202 * @param dev set to the device ID 253 * @param dev set to the device ID
@@ -322,9 +373,14 @@ GNUNET_DISK_get_blocks_available (const char *part)
322#endif 373#endif
323} 374}
324 375
376
325/** 377/**
326 * Test if fil is a directory. 378 * Test if "fil" is a directory.
379 * Will not print an error message if the directory
380 * does not exist. Will log errors if GNUNET_SYSERR is
381 * returned (i.e., a file exists with the same name).
327 * 382 *
383 * @param fil filename to test
328 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it 384 * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
329 * does not exist 385 * does not exist
330 */ 386 */
@@ -357,7 +413,9 @@ GNUNET_DISK_directory_test (const char *fil)
357/** 413/**
358 * Check that fil corresponds to a filename 414 * Check that fil corresponds to a filename
359 * (of a file that exists and that is not a directory). 415 * (of a file that exists and that is not a directory).
360 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something 416 *
417 * @param fil filename to check
418 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
361 * else (will print an error message in that case, too). 419 * else (will print an error message in that case, too).
362 */ 420 */
363int 421int
@@ -398,6 +456,7 @@ GNUNET_DISK_file_test (const char *fil)
398 return GNUNET_YES; 456 return GNUNET_YES;
399} 457}
400 458
459
401/** 460/**
402 * Implementation of "mkdir -p" 461 * Implementation of "mkdir -p"
403 * @param dir the directory to create 462 * @param dir the directory to create
@@ -504,6 +563,7 @@ GNUNET_DISK_directory_create_for_file (const char *dir)
504 return ret; 563 return ret;
505} 564}
506 565
566
507/** 567/**
508 * Read the contents of a binary file into a buffer. 568 * Read the contents of a binary file into a buffer.
509 * @param h handle to an open file 569 * @param h handle to an open file
@@ -545,14 +605,15 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
545 * @return number of bytes read, GNUNET_SYSERR on failure 605 * @return number of bytes read, GNUNET_SYSERR on failure
546 */ 606 */
547ssize_t 607ssize_t
548GNUNET_DISK_fn_read (const char * const fn, 608GNUNET_DISK_fn_read (const char * fn,
549 void *result, 609 void *result,
550 size_t len) 610 size_t len)
551{ 611{
552 struct GNUNET_DISK_FileHandle *fh; 612 struct GNUNET_DISK_FileHandle *fh;
553 ssize_t ret; 613 ssize_t ret;
554 614
555 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ); 615 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
616 GNUNET_DISK_PERM_NONE);
556 if (!fh) 617 if (!fh)
557 return GNUNET_SYSERR; 618 return GNUNET_SYSERR;
558 ret = GNUNET_DISK_file_read (fh, result, len); 619 ret = GNUNET_DISK_file_read (fh, result, len);
@@ -595,16 +656,18 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buff
595 656
596/** 657/**
597 * Write a buffer to a file. If the file is longer than the 658 * Write a buffer to a file. If the file is longer than the
598 * number of bytes that will be written, iit will be truncated. 659 * number of bytes that will be written, it will be truncated.
599 * 660 *
600 * @param fn file name 661 * @param fn file name
601 * @param buffer the data to write 662 * @param buffer the data to write
602 * @param n number of bytes to write 663 * @param n number of bytes to write
664 * @param mode file permissions
603 * @return GNUNET_OK on success, GNUNET_SYSERR on error 665 * @return GNUNET_OK on success, GNUNET_SYSERR on error
604 */ 666 */
605ssize_t 667ssize_t
606GNUNET_DISK_fn_write (const char * const fn, const void *buffer, 668GNUNET_DISK_fn_write (const char * fn, const void *buffer,
607 size_t n, int mode) 669 size_t n,
670 enum GNUNET_DISK_AccessPermissions mode)
608{ 671{
609 struct GNUNET_DISK_FileHandle *fh; 672 struct GNUNET_DISK_FileHandle *fh;
610 int ret; 673 int ret;
@@ -622,18 +685,19 @@ GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
622} 685}
623 686
624/** 687/**
625 * Scan a directory for files. The name of the directory 688 * Scan a directory for files.
626 * must be expanded first (!). 689 *
627 * @param dirName the name of the directory 690 * @param dirName the name of the directory
628 * @param callback the method to call for each file, 691 * @param callback the method to call for each file,
629 * can be NULL, in that case, we only count 692 * can be NULL, in that case, we only count
630 * @param data argument to pass to callback 693 * @param callback_cls closure for callback
631 * @return the number of files found, GNUNET_SYSERR on error or 694 * @return the number of files found, GNUNET_SYSERR on error or
632 * ieration aborted by callback returning GNUNET_SYSERR 695 * ieration aborted by callback returning GNUNET_SYSERR
633 */ 696 */
634int 697int
635GNUNET_DISK_directory_scan (const char *dirName, 698GNUNET_DISK_directory_scan (const char *dirName,
636 GNUNET_FileNameCallback callback, void *data) 699 GNUNET_FileNameCallback callback,
700 void *callback_cls)
637{ 701{
638 DIR *dinfo; 702 DIR *dinfo;
639 struct dirent *finfo; 703 struct dirent *finfo;
@@ -697,7 +761,7 @@ GNUNET_DISK_directory_scan (const char *dirName,
697 dname, 761 dname,
698 (strcmp (dname, DIR_SEPARATOR_STR) == 762 (strcmp (dname, DIR_SEPARATOR_STR) ==
699 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); 763 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
700 if (GNUNET_OK != callback (data, name)) 764 if (GNUNET_OK != callback (callback_cls, name))
701 { 765 {
702 closedir (dinfo); 766 closedir (dinfo);
703 GNUNET_free (name); 767 GNUNET_free (name);
@@ -856,6 +920,14 @@ GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
856} 920}
857 921
858 922
923/**
924 * Function that removes the given directory by calling
925 * "GNUNET_DISK_directory_remove".
926 *
927 * @param unused not used
928 * @param fn directory to remove
929 * @return GNUNET_OK
930 */
859static int 931static int
860remove_helper (void *unused, const char *fn) 932remove_helper (void *unused, const char *fn)
861{ 933{
@@ -863,6 +935,7 @@ remove_helper (void *unused, const char *fn)
863 return GNUNET_OK; 935 return GNUNET_OK;
864} 936}
865 937
938
866/** 939/**
867 * Remove all files in a directory (rm -rf). Call with 940 * Remove all files in a directory (rm -rf). Call with
868 * caution. 941 * caution.
@@ -890,7 +963,9 @@ GNUNET_DISK_directory_remove (const char *fileName)
890 return GNUNET_SYSERR; 963 return GNUNET_SYSERR;
891 } 964 }
892 if (GNUNET_SYSERR == 965 if (GNUNET_SYSERR ==
893 GNUNET_DISK_directory_scan (fileName, remove_helper, NULL)) 966 GNUNET_DISK_directory_scan (fileName,
967 &remove_helper,
968 NULL))
894 return GNUNET_SYSERR; 969 return GNUNET_SYSERR;
895 if (0 != RMDIR (fileName)) 970 if (0 != RMDIR (fileName))
896 { 971 {
@@ -900,10 +975,12 @@ GNUNET_DISK_directory_remove (const char *fileName)
900 return GNUNET_OK; 975 return GNUNET_OK;
901} 976}
902 977
903#define COPY_BLK_SIZE 65536
904 978
905/** 979/**
906 * Copy a file. 980 * Copy a file.
981 *
982 * @param src file to copy
983 * @param dst destination file name
907 * @return GNUNET_OK on success, GNUNET_SYSERR on error 984 * @return GNUNET_OK on success, GNUNET_SYSERR on error
908 */ 985 */
909int 986int
@@ -919,7 +996,8 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
919 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES)) 996 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
920 return GNUNET_SYSERR; 997 return GNUNET_SYSERR;
921 pos = 0; 998 pos = 0;
922 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ); 999 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
1000 GNUNET_DISK_PERM_NONE);
923 if (!in) 1001 if (!in)
924 return GNUNET_SYSERR; 1002 return GNUNET_SYSERR;
925 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE 1003 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
@@ -984,6 +1062,10 @@ GNUNET_DISK_filename_canonicalize (char *fn)
984 1062
985/** 1063/**
986 * @brief Change owner of a file 1064 * @brief Change owner of a file
1065 *
1066 * @param filename name of file to change the owner of
1067 * @param user name of the new owner
1068 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
987 */ 1069 */
988int 1070int
989GNUNET_DISK_file_change_owner (const char *filename, const char *user) 1071GNUNET_DISK_file_change_owner (const char *filename, const char *user)
@@ -1097,16 +1179,21 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
1097 1179
1098 1180
1099/** 1181/**
1100 * Open a file 1182 * Open a file. Note that the access permissions will only be
1183 * used if a new file is created and if the underlying operating
1184 * system supports the given permissions.
1185 *
1101 * @param fn file name to be opened 1186 * @param fn file name to be opened
1102 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags 1187 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
1103 * @param ... permissions for the newly created file 1188 * @param perm permissions for the newly created file, use
1189 * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this
1190 * call (because of flags)
1104 * @return IO handle on success, NULL on error 1191 * @return IO handle on success, NULL on error
1105 */ 1192 */
1106struct GNUNET_DISK_FileHandle * 1193struct GNUNET_DISK_FileHandle *
1107GNUNET_DISK_file_open (const char *fn, 1194GNUNET_DISK_file_open (const char *fn,
1108 enum GNUNET_DISK_OpenFlags flags, 1195 enum GNUNET_DISK_OpenFlags flags,
1109 ...) 1196 enum GNUNET_DISK_AccessPermissions perm)
1110{ 1197{
1111 char *expfn; 1198 char *expfn;
1112 struct GNUNET_DISK_FileHandle *ret; 1199 struct GNUNET_DISK_FileHandle *ret;
@@ -1144,15 +1231,7 @@ GNUNET_DISK_file_open (const char *fn,
1144 oflags |= O_APPEND; 1231 oflags |= O_APPEND;
1145 if (flags & GNUNET_DISK_OPEN_CREATE) 1232 if (flags & GNUNET_DISK_OPEN_CREATE)
1146 { 1233 {
1147 int perm;
1148
1149 oflags |= O_CREAT; 1234 oflags |= O_CREAT;
1150
1151 va_list arg;
1152 va_start (arg, flags);
1153 perm = va_arg (arg, int);
1154 va_end (arg);
1155
1156 if (perm & GNUNET_DISK_PERM_USER_READ) 1235 if (perm & GNUNET_DISK_PERM_USER_READ)
1157 mode |= S_IRUSR; 1236 mode |= S_IRUSR;
1158 if (perm & GNUNET_DISK_PERM_USER_WRITE) 1237 if (perm & GNUNET_DISK_PERM_USER_WRITE)
@@ -1243,6 +1322,7 @@ GNUNET_DISK_file_open (const char *fn,
1243 return ret; 1322 return ret;
1244} 1323}
1245 1324
1325
1246/** 1326/**
1247 * Close an open file 1327 * Close an open file
1248 * @param h file handle 1328 * @param h file handle
@@ -1277,6 +1357,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1277 return GNUNET_OK; 1357 return GNUNET_OK;
1278} 1358}
1279 1359
1360
1280/** 1361/**
1281 * Construct full path to a file inside of the private 1362 * Construct full path to a file inside of the private
1282 * directory used by GNUnet. Also creates the corresponding 1363 * directory used by GNUnet. Also creates the corresponding
@@ -1347,12 +1428,26 @@ GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1347 return ret; 1428 return ret;
1348} 1429}
1349 1430
1431
1432/**
1433 * Handle for a memory-mapping operation.
1434 */
1350struct GNUNET_DISK_MapHandle 1435struct GNUNET_DISK_MapHandle
1351{ 1436{
1352#ifdef MINGW 1437#ifdef MINGW
1438 /**
1439 * Underlying OS handle.
1440 */
1353 HANDLE h; 1441 HANDLE h;
1354#else 1442#else
1443 /**
1444 * Address where the map is in memory.
1445 */
1355 void *addr; 1446 void *addr;
1447
1448 /**
1449 * Number of bytes mapped.
1450 */
1356 size_t len; 1451 size_t len;
1357#endif 1452#endif
1358}; 1453};
@@ -1360,6 +1455,7 @@ struct GNUNET_DISK_MapHandle
1360 1455
1361/** 1456/**
1362 * Map a file into memory 1457 * Map a file into memory
1458 *
1363 * @param h open file handle 1459 * @param h open file handle
1364 * @param m handle to the new mapping 1460 * @param m handle to the new mapping
1365 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx 1461 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
@@ -1505,11 +1601,14 @@ struct GNUNET_DISK_PipeHandle *
1505GNUNET_DISK_pipe (int blocking) 1601GNUNET_DISK_pipe (int blocking)
1506{ 1602{
1507 struct GNUNET_DISK_PipeHandle *p; 1603 struct GNUNET_DISK_PipeHandle *p;
1604 struct GNUNET_DISK_FileHandle *fds;
1508 int err; 1605 int err;
1509 1606
1510 err = GNUNET_NO; 1607 err = GNUNET_NO;
1511 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle)); 1608 p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + 2 * sizeof (struct GNUNET_DISK_FileHandle));
1512 1609 fds = (struct GNUNET_DISK_FileHandle *) &p[1];
1610 p->fd[0] = &fds[0];
1611 p->fd[1] = &fds[1];
1513#ifndef MINGW 1612#ifndef MINGW
1514 int fd[2]; 1613 int fd[2];
1515 int ret; 1614 int ret;
@@ -1517,67 +1616,60 @@ GNUNET_DISK_pipe (int blocking)
1517 int eno; 1616 int eno;
1518 1617
1519 ret = pipe (fd); 1618 ret = pipe (fd);
1520 if (ret != -1) 1619 if (ret == -1)
1521 { 1620 {
1522 p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); 1621 err = errno;
1523 p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); 1622 GNUNET_free (p);
1524 p->fd[0]->fd = fd[0]; 1623 errno = err;
1525 p->fd[1]->fd = fd[1]; 1624 return NULL;
1526 1625 }
1527 if (!blocking) 1626 p->fd[0]->fd = fd[0];
1528 { 1627 p->fd[1]->fd = fd[1];
1529 flags = fcntl (fd[0], F_GETFL); 1628 if (!blocking)
1530 flags |= O_NONBLOCK; 1629 {
1531 ret = fcntl (fd[0], F_SETFL, flags); 1630 flags = fcntl (fd[0], F_GETFL);
1532 if (ret != -1) 1631 flags |= O_NONBLOCK;
1533 { 1632 ret = fcntl (fd[0], F_SETFL, flags);
1534 flags = fcntl (fd[1], F_GETFL); 1633 if (ret != -1)
1535 flags |= O_NONBLOCK; 1634 {
1536 ret = fcntl (fd[1], F_SETFL, flags); 1635 flags = fcntl (fd[1], F_GETFL);
1537 } 1636 flags |= O_NONBLOCK;
1538 if (ret == -1) 1637 ret = fcntl (fd[1], F_SETFL, flags);
1539 { 1638 }
1540 eno = errno; 1639 if (ret == -1)
1541 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl"); 1640 {
1542 GNUNET_DISK_file_close (p->fd[0]); 1641 eno = errno;
1543 GNUNET_DISK_file_close (p->fd[1]); 1642 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl");
1544 p->fd[0] = NULL; 1643 close (p->fd[0]->fd);
1545 p->fd[1] = NULL; 1644 close (p->fd[1]->fd);
1546 err = GNUNET_YES; 1645 err = GNUNET_YES;
1547 errno = eno; 1646 GNUNET_free (p);
1548 } 1647 errno = eno;
1549 } 1648 return NULL;
1649 }
1550 } 1650 }
1551 else
1552 err = GNUNET_YES;
1553#else 1651#else
1554 BOOL ret; 1652 BOOL ret;
1555 1653
1556 ret = CreatePipe (&p->fd[0].h, &p->fd[1].h, NULL, 0); 1654 ret = CreatePipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0);
1557 if (ret) 1655 if (! ret)
1558 { 1656 {
1559 if (!blocking) 1657 GNUNET_free (p);
1560 { 1658 SetErrnoFromWinError (GetLastError ());
1561 DWORD mode; 1659 return NULL;
1562
1563 mode = PIPE_NOWAIT;
1564 p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1565 p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1566 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
1567 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
1568 /* this always fails on Windows 95, so we don't care about error handling */
1569 }
1570 } 1660 }
1571 else 1661 if (!blocking)
1572 err = GNUNET_YES;
1573#endif
1574
1575 if (GNUNET_YES == err)
1576 { 1662 {
1577 GNUNET_free (p); 1663 DWORD mode;
1578 p = NULL; 1664
1665 mode = PIPE_NOWAIT;
1666 p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1667 p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1668 SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
1669 SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
1670 /* this always fails on Windows 95, so we don't care about error handling */
1579 } 1671 }
1580 1672#endif
1581 return p; 1673 return p;
1582} 1674}
1583 1675
@@ -1592,37 +1684,35 @@ int
1592GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) 1684GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1593{ 1685{
1594 int ret = GNUNET_OK; 1686 int ret = GNUNET_OK;
1687 int save;
1688
1595#ifdef MINGW 1689#ifdef MINGW
1596 if (!CloseHandle (p->fd[0]->h)) 1690 if (!CloseHandle (p->fd[0]->h))
1597 { 1691 {
1598 SetErrnoFromWinError (GetLastError ()); 1692 SetErrnoFromWinError (GetLastError ());
1599 ret = GNUNET_SYSERR; 1693 ret = GNUNET_SYSERR;
1600 } 1694 }
1601
1602 if (!CloseHandle (p->fd[1]->h)) 1695 if (!CloseHandle (p->fd[1]->h))
1603 { 1696 {
1604 SetErrnoFromWinError (GetLastError ()); 1697 SetErrnoFromWinError (GetLastError ());
1605 ret = GNUNET_SYSERR; 1698 ret = GNUNET_SYSERR;
1606 } 1699 }
1700 save = errno;
1607#else 1701#else
1608 int save; 1702 save = 0;
1609
1610 if (0 != close (p->fd[0]->fd)) 1703 if (0 != close (p->fd[0]->fd))
1611 { 1704 {
1612 ret = GNUNET_SYSERR; 1705 ret = GNUNET_SYSERR;
1613 save = errno; 1706 save = errno;
1614 } 1707 }
1615 else
1616 save = 0;
1617
1618 if (0 != close (p->fd[1]->fd)) 1708 if (0 != close (p->fd[1]->fd))
1619 ret = GNUNET_SYSERR; 1709 {
1620 else 1710 ret = GNUNET_SYSERR;
1621 errno = save; 1711 save = errno;
1712 }
1622#endif 1713#endif
1623 GNUNET_free (p->fd[0]);
1624 GNUNET_free (p->fd[1]);
1625 GNUNET_free (p); 1714 GNUNET_free (p);
1715 errno = save;
1626 return ret; 1716 return ret;
1627} 1717}
1628 1718
@@ -1630,14 +1720,24 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1630/** 1720/**
1631 * Get the handle to a particular pipe end 1721 * Get the handle to a particular pipe end
1632 * @param p pipe 1722 * @param p pipe
1633 * @param n number of the end 1723 * @param n end to access
1634 */ 1724 */
1635const struct GNUNET_DISK_FileHandle * 1725const struct GNUNET_DISK_FileHandle *
1636GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, int n) 1726GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
1727 enum GNUNET_DISK_PipeEnd n)
1637{ 1728{
1638 return p->fd[n]; 1729 switch (n)
1730 {
1731 case GNUNET_DISK_PIPE_END_READ:
1732 case GNUNET_DISK_PIPE_END_WRITE:
1733 return p->fd[n];
1734 default:
1735 GNUNET_break (0);
1736 return NULL;
1737 }
1639} 1738}
1640 1739
1740
1641/** 1741/**
1642 * Retrieve OS file handle 1742 * Retrieve OS file handle
1643 * @internal 1743 * @internal
diff --git a/src/util/server.c b/src/util/server.c
index a2d5551a4..7214b1939 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -324,7 +324,8 @@ process_listen_socket (void *cls,
324 destroy_server (server); 324 destroy_server (server);
325 return; 325 return;
326 } 326 }
327 shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe, 0); 327 shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe,
328 GNUNET_DISK_PIPE_END_READ);
328 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket)); 329 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket));
329 GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe)); 330 GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe));
330 sock = GNUNET_CONNECTION_create_from_accept (tc->sched, 331 sock = GNUNET_CONNECTION_create_from_accept (tc->sched,
@@ -476,7 +477,8 @@ GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
476 { 477 {
477 r = GNUNET_NETWORK_fdset_create (); 478 r = GNUNET_NETWORK_fdset_create ();
478 GNUNET_NETWORK_fdset_set (r, ret->listen_socket); 479 GNUNET_NETWORK_fdset_set (r, ret->listen_socket);
479 GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe, 0)); 480 GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe,
481 GNUNET_DISK_PIPE_END_READ));
480 GNUNET_SCHEDULER_add_select (sched, 482 GNUNET_SCHEDULER_add_select (sched,
481 GNUNET_YES, 483 GNUNET_YES,
482 GNUNET_SCHEDULER_PRIORITY_HIGH, 484 GNUNET_SCHEDULER_PRIORITY_HIGH,
@@ -503,7 +505,10 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
503 if (s->listen_socket == NULL) 505 if (s->listen_socket == NULL)
504 destroy_server (s); 506 destroy_server (s);
505 else 507 else
506 GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe, 1), &c, 1)); 508 GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe,
509 GNUNET_DISK_PIPE_END_WRITE),
510 &c,
511 sizeof(c)));
507} 512}
508 513
509 514
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c
index 811389953..b19ccff2d 100644
--- a/src/util/test_scheduler.c
+++ b/src/util/test_scheduler.c
@@ -116,8 +116,8 @@ task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 (*ok) = 6; 116 (*ok) = 6;
117 p = GNUNET_DISK_pipe (GNUNET_NO); 117 p = GNUNET_DISK_pipe (GNUNET_NO);
118 GNUNET_assert (NULL != p); 118 GNUNET_assert (NULL != p);
119 fds[0] = GNUNET_DISK_pipe_handle (p, 0); 119 fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
120 fds[1] = GNUNET_DISK_pipe_handle (p, 1); 120 fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
121 GNUNET_SCHEDULER_add_read_file (tc->sched, 121 GNUNET_SCHEDULER_add_read_file (tc->sched,
122 GNUNET_NO, 122 GNUNET_NO,
123 GNUNET_SCHEDULER_PRIORITY_DEFAULT, 123 GNUNET_SCHEDULER_PRIORITY_DEFAULT,