aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/gnunet_disk_lib.h57
-rw-r--r--src/statistics/gnunet-service-statistics.c21
-rw-r--r--src/util/container_bloomfilter.c10
-rw-r--r--src/util/crypto_hash.c4
-rw-r--r--src/util/disk.c136
-rw-r--r--src/util/pseudonym.c10
6 files changed, 127 insertions, 111 deletions
diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h
index 2c7488cc4..a8a51ab78 100644
--- a/src/include/gnunet_disk_lib.h
+++ b/src/include/gnunet_disk_lib.h
@@ -74,7 +74,7 @@ extern "C"
74 74
75enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END}; 75enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END};
76 76
77struct GNUNET_IO_Handle; 77struct GNUNET_DISK_FileHandle;
78 78
79/** 79/**
80 * Get the number of blocks that are left on the partition that 80 * Get the number of blocks that are left on the partition that
@@ -91,7 +91,7 @@ long GNUNET_DISK_get_blocks_available (const char *part);
91 * @param h handle to check 91 * @param h handle to check
92 * @return GNUNET_YES if invalid, GNUNET_NO if valid 92 * @return GNUNET_YES if invalid, GNUNET_NO if valid
93 */ 93 */
94int GNUNET_DISK_handle_invalid (const struct GNUNET_IO_Handle *h); 94int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
95 95
96 96
97/** 97/**
@@ -112,7 +112,7 @@ int GNUNET_DISK_file_test (const char *fil);
112 * @return the new position on success, GNUNET_SYSERR otherwise 112 * @return the new position on success, GNUNET_SYSERR otherwise
113 */ 113 */
114off_t 114off_t
115GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset, 115GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
116 enum GNUNET_DISK_Seek whence); 116 enum GNUNET_DISK_Seek whence);
117 117
118 118
@@ -136,15 +136,16 @@ int GNUNET_DISK_file_size (const char *filename,
136 * @param perm permissions for the newly created file 136 * @param perm permissions for the newly created file
137 * @return IO handle on success, NULL on error 137 * @return IO handle on success, NULL on error
138 */ 138 */
139struct GNUNET_IO_Handle *GNUNET_DISK_file_open (const char *fn, int flags, ...); 139struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...);
140 140
141 141
142/** 142/**
143 * Close an open file 143 * Close an open file.
144 *
144 * @param h file handle 145 * @param h file handle
145 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 146 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
146 */ 147 */
147int GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h); 148int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
148 149
149 150
150/** 151/**
@@ -154,7 +155,8 @@ int GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h);
154 * @param len the maximum number of bytes to read 155 * @param len the maximum number of bytes to read
155 * @return the number of bytes read on success, GNUNET_SYSERR on failure 156 * @return the number of bytes read on success, GNUNET_SYSERR on failure
156 */ 157 */
157int GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len); 158ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
159 size_t len);
158 160
159 161
160/** 162/**
@@ -164,29 +166,36 @@ int GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int l
164 * @param len the maximum number of bytes to read 166 * @param len the maximum number of bytes to read
165 * @return number of bytes read, GNUNET_SYSERR on failure 167 * @return number of bytes read, GNUNET_SYSERR on failure
166 */ 168 */
167int GNUNET_DISK_fn_read (const char * const fn, void *result, int len); 169ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result,
170 size_t len);
168 171
169 172
170/** 173/**
171 * Write a buffer to a file. 174 * Write a buffer to a file.
175 *
172 * @param h handle to open file 176 * @param h handle to open file
173 * @param buffer the data to write 177 * @param buffer the data to write
174 * @param n number of bytes to write 178 * @param n number of bytes to write
175 * @return GNUNET_OK on success, GNUNET_SYSERR on error 179 * @return GNUNET_OK on success, GNUNET_SYSERR on error
176 */ 180 */
177int GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer, 181ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
178 unsigned int n); 182 const void *buffer,
183 size_t n);
179 184
180 185
181/** 186/**
182 * Write a buffer to a file. 187 * Write a buffer to a file. If the file is longer than
188 * the given buffer size, it will be truncated.
189 *
183 * @param fn file name 190 * @param fn file name
184 * @param buffer the data to write 191 * @param buffer the data to write
185 * @param n number of bytes to write 192 * @param n number of bytes to write
186 * @return number of bytes written on success, GNUNET_SYSERR on error 193 * @return number of bytes written on success, GNUNET_SYSERR on error
187 */ 194 */
188int GNUNET_DISK_fn_write (const char * const fn, const void *buffer, 195ssize_t GNUNET_DISK_fn_write (const char * fn,
189 unsigned int n, int mode); 196 const void *buffer,
197 size_t n,
198 int mode);
190 199
191 200
192/** 201/**
@@ -317,7 +326,7 @@ int GNUNET_DISK_directory_create (const char *dir);
317 * @return GNUNET_OK on success, GNUNET_SYSERR on error 326 * @return GNUNET_OK on success, GNUNET_SYSERR on error
318 */ 327 */
319int 328int
320GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart, 329GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
321 off_t lockEnd); 330 off_t lockEnd);
322 331
323 332
@@ -353,32 +362,38 @@ int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
353char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg, 362char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
354 const char *serviceName, ...); 363 const char *serviceName, ...);
355 364
365
366/**
367 * Opaque handle for a memory-mapping operation.
368 */
369struct GNUNET_DISK_MapHandle;
370
356/** 371/**
357 * Map a file into memory 372 * Map a file into memory
358 * @param h open file handle 373 * @param h open file handle
359 * @param m handle to the new mapping 374 * @param m handle to the new mapping (will be set)
360 * @param access access specification, GNUNET_DISK_MAP_xxx 375 * @param access access specification, GNUNET_DISK_MAP_xxx
361 * @param len size of the mapping 376 * @param len size of the mapping
362 * @return pointer to the mapped memory region, NULL on failure 377 * @return pointer to the mapped memory region, NULL on failure
363 */ 378 */
364void *GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m, 379void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
365 int access, size_t len); 380 struct GNUNET_DISK_MapHandle **m,
381 int access, size_t len);
366 382
367/** 383/**
368 * Unmap a file 384 * Unmap a file
385 *
369 * @param h mapping handle 386 * @param h mapping handle
370 * @param addr pointer to the mapped memory region
371 * @param len size of the mapping
372 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 387 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
373 */ 388 */
374int GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len); 389int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
375 390
376/** 391/**
377 * Write file changes to disk 392 * Write file changes to disk
378 * @param h handle to an open file 393 * @param h handle to an open file
379 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 394 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
380 */ 395 */
381int GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h); 396int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
382 397
383#if 0 /* keep Emacsens' auto-indent happy */ 398#if 0 /* keep Emacsens' auto-indent happy */
384{ 399{
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c
index c7388ebd9..1c0981b80 100644
--- a/src/statistics/gnunet-service-statistics.c
+++ b/src/statistics/gnunet-service-statistics.c
@@ -99,7 +99,8 @@ load (struct GNUNET_SERVER_Handle *server,
99 struct GNUNET_CONFIGURATION_Handle *cfg) 99 struct GNUNET_CONFIGURATION_Handle *cfg)
100{ 100{
101 char *fn; 101 char *fn;
102 struct GNUNET_IO_Handle *fh, *mh; 102 struct GNUNET_DISK_FileHandle *fh;
103 struct GNUNET_DISK_MapHandle *mh;
103 struct stat sb; 104 struct stat sb;
104 char *buf; 105 char *buf;
105 size_t off; 106 size_t off;
@@ -124,7 +125,7 @@ load (struct GNUNET_SERVER_Handle *server,
124 if (NULL == buf) 125 if (NULL == buf)
125 { 126 {
126 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn); 127 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh)); 128 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
128 GNUNET_free (fn); 129 GNUNET_free (fn);
129 return; 130 return;
130 } 131 }
@@ -143,8 +144,8 @@ load (struct GNUNET_SERVER_Handle *server,
143 } 144 }
144 off += ntohs (msg->size); 145 off += ntohs (msg->size);
145 } 146 }
146 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (&mh, buf, sb.st_size)); 147 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
147 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh)); 148 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
148 GNUNET_free (fn); 149 GNUNET_free (fn);
149} 150}
150 151
@@ -160,10 +161,11 @@ save (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
160{ 161{
161 struct StatsEntry *pos; 162 struct StatsEntry *pos;
162 char *fn; 163 char *fn;
163 struct GNUNET_IO_Handle *fh; 164 struct GNUNET_DISK_FileHandle *fh;
164 uint16_t size; 165 uint16_t size;
165 unsigned long long total; 166 unsigned long long total;
166 167
168 fh = NULL;
167 fn = GNUNET_DISK_get_home_filename (cfg, 169 fn = GNUNET_DISK_get_home_filename (cfg,
168 "statistics", "statistics.data", NULL); 170 "statistics", "statistics.data", NULL);
169 if (fn != NULL) 171 if (fn != NULL)
@@ -174,23 +176,24 @@ save (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
174 while (NULL != (pos = start)) 176 while (NULL != (pos = start))
175 { 177 {
176 start = pos->next; 178 start = pos->next;
177 if ((pos->persistent) && fh) 179 if ((pos->persistent) && (NULL != fh))
178 { 180 {
179 size = htons (pos->msg->header.size); 181 size = htons (pos->msg->header.size);
180 if (size != GNUNET_DISK_file_write (fh, pos->msg, size)) 182 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
181 { 183 {
182 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 184 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
183 "write", fn); 185 "write", fn);
184 GNUNET_DISK_file_close (&fh); 186 GNUNET_DISK_file_close (fh);
187 fh = NULL;
185 } 188 }
186 else 189 else
187 total += size; 190 total += size;
188 } 191 }
189 GNUNET_free (pos); 192 GNUNET_free (pos);
190 } 193 }
191 if (fh) 194 if (NULL != fh)
192 { 195 {
193 GNUNET_DISK_file_close (&fh); 196 GNUNET_DISK_file_close (fh);
194 if (total == 0) 197 if (total == 0)
195 GNUNET_break (0 == UNLINK (fn)); 198 GNUNET_break (0 == UNLINK (fn));
196 else 199 else
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index cec686795..cf99ac7b4 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -60,7 +60,7 @@ struct GNUNET_CONTAINER_BloomFilter
60 /** 60 /**
61 * The bit counter file on disk 61 * The bit counter file on disk
62 */ 62 */
63 struct GNUNET_IO_Handle *fh; 63 struct GNUNET_DISK_FileHandle *fh;
64 64
65 /** 65 /**
66 * How many bits we set for each stored element 66 * How many bits we set for each stored element
@@ -142,7 +142,7 @@ testBit (char *bitArray, unsigned int bitIdx)
142 * @param fh A file to keep the 4 bit address usage counters in 142 * @param fh A file to keep the 4 bit address usage counters in
143 */ 143 */
144static void 144static void
145incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_IO_Handle *fh) 145incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_FileHandle *fh)
146{ 146{
147 unsigned int fileSlot; 147 unsigned int fileSlot;
148 unsigned char value; 148 unsigned char value;
@@ -187,7 +187,7 @@ incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_IO_Handle
187 * @param fh A file to keep the 4bit address usage counters in 187 * @param fh A file to keep the 4bit address usage counters in
188 */ 188 */
189static void 189static void
190decrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_IO_Handle *fh) 190decrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_FileHandle *fh)
191{ 191{
192 unsigned int fileSlot; 192 unsigned int fileSlot;
193 unsigned char value; 193 unsigned char value;
@@ -240,7 +240,7 @@ decrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_IO_Handle
240 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise 240 * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
241 */ 241 */
242static int 242static int
243makeEmptyFile (const struct GNUNET_IO_Handle *fh, unsigned int size) 243makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, unsigned int size)
244{ 244{
245 char *buffer; 245 char *buffer;
246 unsigned int bytesleft = size; 246 unsigned int bytesleft = size;
@@ -541,7 +541,7 @@ GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
541 return; 541 return;
542 if (bf->filename != NULL) 542 if (bf->filename != NULL)
543 { 543 {
544 GNUNET_DISK_file_close (&bf->fh); 544 GNUNET_DISK_file_close (bf->fh);
545 GNUNET_free (bf->filename); 545 GNUNET_free (bf->filename);
546 } 546 }
547 GNUNET_free (bf->bitArray); 547 GNUNET_free (bf->bitArray);
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index 0d8cdeb5c..8df4cc8f4 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -429,7 +429,7 @@ struct FileHashContext
429 /** 429 /**
430 * File descriptor. 430 * File descriptor.
431 */ 431 */
432 struct GNUNET_IO_Handle *fh; 432 struct GNUNET_DISK_FileHandle *fh;
433 433
434}; 434};
435 435
@@ -444,7 +444,7 @@ file_hash_finish (struct FileHashContext *fhc, const GNUNET_HashCode * res)
444 fhc->callback (fhc->callback_cls, res); 444 fhc->callback (fhc->callback_cls, res);
445 GNUNET_free (fhc->filename); 445 GNUNET_free (fhc->filename);
446 if (!GNUNET_DISK_handle_invalid (fhc->fh)) 446 if (!GNUNET_DISK_handle_invalid (fhc->fh))
447 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fhc->fh)); 447 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
448 GNUNET_free (fhc); /* also frees fhc->buffer */ 448 GNUNET_free (fhc); /* also frees fhc->buffer */
449} 449}
450 450
diff --git a/src/util/disk.c b/src/util/disk.c
index 019278683..078d6ea15 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -74,7 +74,7 @@ typedef struct
74 int include_sym_links; 74 int include_sym_links;
75} GetFileSizeData; 75} GetFileSizeData;
76 76
77struct GNUNET_IO_Handle 77struct GNUNET_DISK_FileHandle
78{ 78{
79#if MINGW 79#if MINGW
80 HANDLE h; 80 HANDLE h;
@@ -124,7 +124,7 @@ getSizeRec (void *ptr, const char *fn)
124 * @return GNUNET_YES if invalid, GNUNET_NO if valid 124 * @return GNUNET_YES if invalid, GNUNET_NO if valid
125 */ 125 */
126int 126int
127GNUNET_DISK_handle_invalid (const struct GNUNET_IO_Handle *h) 127GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
128{ 128{
129#ifdef MINGW 129#ifdef MINGW
130 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO; 130 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
@@ -133,19 +133,6 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_IO_Handle *h)
133#endif 133#endif
134} 134}
135 135
136/**
137 * Mark a handle as invalid
138 * @param h file handle
139 */
140static void
141GNUNET_DISK_handle_invalidate (struct GNUNET_IO_Handle *h)
142{
143#ifdef MINGW
144 h->h = INVALID_HANDLE_VALUE;
145#else
146 h->fd = -1;
147#endif
148}
149 136
150/** 137/**
151 * Move the read/write pointer in a file 138 * Move the read/write pointer in a file
@@ -155,7 +142,7 @@ GNUNET_DISK_handle_invalidate (struct GNUNET_IO_Handle *h)
155 * @return the new position on success, GNUNET_SYSERR otherwise 142 * @return the new position on success, GNUNET_SYSERR otherwise
156 */ 143 */
157off_t 144off_t
158GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset, 145GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
159 enum GNUNET_DISK_Seek whence) 146 enum GNUNET_DISK_Seek whence)
160{ 147{
161 if (h == NULL) 148 if (h == NULL)
@@ -441,8 +428,9 @@ GNUNET_DISK_directory_create_for_file (const char *dir)
441 * @param len the maximum number of bytes to read 428 * @param len the maximum number of bytes to read
442 * @return the number of bytes read on success, GNUNET_SYSERR on failure 429 * @return the number of bytes read on success, GNUNET_SYSERR on failure
443 */ 430 */
444int 431ssize_t
445GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len) 432GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
433 size_t len)
446{ 434{
447 if (h == NULL) 435 if (h == NULL)
448 { 436 {
@@ -467,22 +455,25 @@ GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len)
467 455
468/** 456/**
469 * Read the contents of a binary file into a buffer. 457 * Read the contents of a binary file into a buffer.
458 *
470 * @param fn file name 459 * @param fn file name
471 * @param result the buffer to write the result to 460 * @param result the buffer to write the result to
472 * @param len the maximum number of bytes to read 461 * @param len the maximum number of bytes to read
473 * @return number of bytes read, GNUNET_SYSERR on failure 462 * @return number of bytes read, GNUNET_SYSERR on failure
474 */ 463 */
475int 464ssize_t
476GNUNET_DISK_fn_read (const char * const fn, void *result, int len) 465GNUNET_DISK_fn_read (const char * const fn,
466 void *result,
467 size_t len)
477{ 468{
478 struct GNUNET_IO_Handle *fh; 469 struct GNUNET_DISK_FileHandle *fh;
479 int ret; 470 ssize_t ret;
480 471
481 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ); 472 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
482 if (!fh) 473 if (!fh)
483 return GNUNET_SYSERR; 474 return GNUNET_SYSERR;
484 ret = GNUNET_DISK_file_read (fh, result, len); 475 ret = GNUNET_DISK_file_read (fh, result, len);
485 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh)); 476 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
486 477
487 return ret; 478 return ret;
488} 479}
@@ -495,9 +486,9 @@ GNUNET_DISK_fn_read (const char * const fn, void *result, int len)
495 * @param n number of bytes to write 486 * @param n number of bytes to write
496 * @return number of bytes written on success, GNUNET_SYSERR on error 487 * @return number of bytes written on success, GNUNET_SYSERR on error
497 */ 488 */
498int 489ssize_t
499GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer, 490GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buffer,
500 unsigned int n) 491 size_t n)
501{ 492{
502 if (h == NULL) 493 if (h == NULL)
503 { 494 {
@@ -520,17 +511,19 @@ GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer,
520} 511}
521 512
522/** 513/**
523 * Write a buffer to a file. 514 * Write a buffer to a file. If the file is longer than the
515 * number of bytes that will be written, iit will be truncated.
516 *
524 * @param fn file name 517 * @param fn file name
525 * @param buffer the data to write 518 * @param buffer the data to write
526 * @param n number of bytes to write 519 * @param n number of bytes to write
527 * @return GNUNET_OK on success, GNUNET_SYSERR on error 520 * @return GNUNET_OK on success, GNUNET_SYSERR on error
528 */ 521 */
529int 522ssize_t
530GNUNET_DISK_fn_write (const char * const fn, const void *buffer, 523GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
531 unsigned int n, int mode) 524 size_t n, int mode)
532{ 525{
533 struct GNUNET_IO_Handle *fh; 526 struct GNUNET_DISK_FileHandle *fh;
534 int ret; 527 int ret;
535 528
536 fh = GNUNET_DISK_file_open (fn, 529 fh = GNUNET_DISK_file_open (fn,
@@ -540,7 +533,7 @@ GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
540 if (!fh) 533 if (!fh)
541 return GNUNET_SYSERR; 534 return GNUNET_SYSERR;
542 ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR; 535 ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR;
543 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh)); 536 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
544 537
545 return ret; 538 return ret;
546} 539}
@@ -837,7 +830,7 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
837 unsigned long long pos; 830 unsigned long long pos;
838 unsigned long long size; 831 unsigned long long size;
839 unsigned long long len; 832 unsigned long long len;
840 struct GNUNET_IO_Handle *in, *out; 833 struct GNUNET_DISK_FileHandle *in, *out;
841 834
842 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES)) 835 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
843 return GNUNET_SYSERR; 836 return GNUNET_SYSERR;
@@ -851,7 +844,7 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
851 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE); 844 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
852 if (!out) 845 if (!out)
853 { 846 {
854 GNUNET_DISK_file_close (&in); 847 GNUNET_DISK_file_close (in);
855 return GNUNET_SYSERR; 848 return GNUNET_SYSERR;
856 } 849 }
857 buf = GNUNET_malloc (COPY_BLK_SIZE); 850 buf = GNUNET_malloc (COPY_BLK_SIZE);
@@ -867,13 +860,13 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
867 pos += len; 860 pos += len;
868 } 861 }
869 GNUNET_free (buf); 862 GNUNET_free (buf);
870 GNUNET_DISK_file_close (&in); 863 GNUNET_DISK_file_close (in);
871 GNUNET_DISK_file_close (&out); 864 GNUNET_DISK_file_close (out);
872 return GNUNET_OK; 865 return GNUNET_OK;
873FAIL: 866FAIL:
874 GNUNET_free (buf); 867 GNUNET_free (buf);
875 GNUNET_DISK_file_close (&in); 868 GNUNET_DISK_file_close (in);
876 GNUNET_DISK_file_close (&out); 869 GNUNET_DISK_file_close (out);
877 return GNUNET_SYSERR; 870 return GNUNET_SYSERR;
878} 871}
879 872
@@ -937,7 +930,7 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
937 * @return GNUNET_OK on success, GNUNET_SYSERR on error 930 * @return GNUNET_OK on success, GNUNET_SYSERR on error
938 */ 931 */
939int 932int
940GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart, 933GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
941 off_t lockEnd) 934 off_t lockEnd)
942{ 935{
943 if (fh == NULL) 936 if (fh == NULL)
@@ -975,11 +968,11 @@ GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart,
975 * @param perm permissions for the newly created file 968 * @param perm permissions for the newly created file
976 * @return IO handle on success, NULL on error 969 * @return IO handle on success, NULL on error
977 */ 970 */
978struct GNUNET_IO_Handle * 971struct GNUNET_DISK_FileHandle *
979GNUNET_DISK_file_open (const char *fn, int flags, ...) 972GNUNET_DISK_file_open (const char *fn, int flags, ...)
980{ 973{
981 char *expfn; 974 char *expfn;
982 struct GNUNET_IO_Handle *ret; 975 struct GNUNET_DISK_FileHandle *ret;
983#ifdef MINGW 976#ifdef MINGW
984 DWORD access; 977 DWORD access;
985 DWORD disp; 978 DWORD disp;
@@ -1085,7 +1078,7 @@ GNUNET_DISK_file_open (const char *fn, int flags, ...)
1085 } 1078 }
1086#endif 1079#endif
1087 1080
1088 ret = GNUNET_malloc(sizeof(struct GNUNET_IO_Handle)); 1081 ret = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
1089#ifdef MINGW 1082#ifdef MINGW
1090 ret->h = h; 1083 ret->h = h;
1091#else 1084#else
@@ -1101,33 +1094,31 @@ GNUNET_DISK_file_open (const char *fn, int flags, ...)
1101 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1094 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1102 */ 1095 */
1103int 1096int
1104GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h) 1097GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1105{ 1098{
1106 if (*h == NULL) 1099 if (h == NULL)
1107 { 1100 {
1108 errno = EINVAL; 1101 errno = EINVAL;
1109 return GNUNET_SYSERR; 1102 return GNUNET_SYSERR;
1110 } 1103 }
1111 1104
1112#if MINGW 1105#if MINGW
1113 if (!CloseHandle ((*h)->h)) 1106 if (!CloseHandle (h->h))
1114 { 1107 {
1115 SetErrnoFromWinError (GetLastError ()); 1108 SetErrnoFromWinError (GetLastError ());
1116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); 1109 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1110 GNUNET_free (h);
1117 return GNUNET_SYSERR; 1111 return GNUNET_SYSERR;
1118 } 1112 }
1119#else 1113#else
1120 if (close ((*h)->fd) != 0) 1114 if (close (h->fd) != 0)
1121 { 1115 {
1122 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); 1116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1117 GNUNET_free (h);
1123 return GNUNET_SYSERR; 1118 return GNUNET_SYSERR;
1124 } 1119 }
1125#endif 1120#endif
1126 1121 GNUNET_free (h);
1127 GNUNET_DISK_handle_invalidate (*h);
1128 free(*h);
1129 *h = NULL;
1130
1131 return GNUNET_OK; 1122 return GNUNET_OK;
1132} 1123}
1133 1124
@@ -1200,6 +1191,17 @@ GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1200 return ret; 1191 return ret;
1201} 1192}
1202 1193
1194struct GNUNET_DISK_MapHandle
1195{
1196#ifdef MINGW
1197 HANDLE h;
1198#else
1199 void *addr;
1200 size_t len;
1201#endif
1202};
1203
1204
1203/** 1205/**
1204 * Map a file into memory 1206 * Map a file into memory
1205 * @param h open file handle 1207 * @param h open file handle
@@ -1209,7 +1211,7 @@ GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1209 * @return pointer to the mapped memory region, NULL on failure 1211 * @return pointer to the mapped memory region, NULL on failure
1210 */ 1212 */
1211void * 1213void *
1212GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m, 1214GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m,
1213 int access, size_t len) 1215 int access, size_t len)
1214{ 1216{
1215 if (h == NULL) 1217 if (h == NULL)
@@ -1243,7 +1245,7 @@ GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle
1243 return NULL; 1245 return NULL;
1244 } 1246 }
1245 1247
1246 *m = GNUNET_malloc (sizeof (struct GNUNET_IO_Handle)); 1248 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1247 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); 1249 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1248 if ((*m)->h == INVALID_HANDLE_VALUE) 1250 if ((*m)->h == INVALID_HANDLE_VALUE)
1249 { 1251 {
@@ -1269,56 +1271,52 @@ GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle
1269 prot = PROT_READ; 1271 prot = PROT_READ;
1270 if (access & GNUNET_DISK_MAP_WRITE) 1272 if (access & GNUNET_DISK_MAP_WRITE)
1271 prot |= PROT_WRITE; 1273 prot |= PROT_WRITE;
1272 *m = NULL; 1274 *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
1273 return mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); 1275 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1276 (*m)->len = len;
1277 return (*m)->addr;
1274#endif 1278#endif
1275} 1279}
1276 1280
1277/** 1281/**
1278 * Unmap a file 1282 * Unmap a file
1279 * @param h mapping handle 1283 * @param h mapping handle
1280 * @param addr pointer to the mapped memory region
1281 * @param len size of the mapping
1282 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1284 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1283 */ 1285 */
1284int 1286int
1285GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len) 1287GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1286{ 1288{
1287#ifdef MINGW
1288 int ret; 1289 int ret;
1289 1290 if (h == NULL)
1290 if ( (h == NULL) || (*h == NULL) )
1291 { 1291 {
1292 errno = EINVAL; 1292 errno = EINVAL;
1293 return GNUNET_SYSERR; 1293 return GNUNET_SYSERR;
1294 } 1294 }
1295 1295
1296#ifdef MINGW
1296 ret = UnmapViewOfFile (addr) ? GNUNET_OK : GNUNET_SYSERR; 1297 ret = UnmapViewOfFile (addr) ? GNUNET_OK : GNUNET_SYSERR;
1297 if (ret != GNUNET_OK) 1298 if (ret != GNUNET_OK)
1298 SetErrnoFromWinError (GetLastError ()); 1299 SetErrnoFromWinError (GetLastError ());
1299 if (!CloseHandle ((*h)->h) && ret == GNUNET_OK) 1300 if (!CloseHandle (h->h) && (ret == GNUNET_OK))
1300 { 1301 {
1301 ret = GNUNET_SYSERR; 1302 ret = GNUNET_SYSERR;
1302 SetErrnoFromWinError (GetLastError ()); 1303 SetErrnoFromWinError (GetLastError ());
1303 } 1304 }
1304
1305 GNUNET_DISK_handle_invalidate (*h);
1306 GNUNET_free (*h);
1307 h = NULL;
1308
1309 return ret;
1310#else 1305#else
1311 return munmap (addr, len) != -1 ? GNUNET_OK : GNUNET_SYSERR; 1306 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1312#endif 1307#endif
1308 GNUNET_free (h);
1309 return ret;
1313} 1310}
1314 1311
1312
1315/** 1313/**
1316 * Write file changes to disk 1314 * Write file changes to disk
1317 * @param h handle to an open file 1315 * @param h handle to an open file
1318 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1316 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1319 */ 1317 */
1320int 1318int
1321GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h) 1319GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1322{ 1320{
1323 if (h == NULL) 1321 if (h == NULL)
1324 { 1322 {
diff --git a/src/util/pseudonym.c b/src/util/pseudonym.c
index f529d11f8..52351c2c8 100644
--- a/src/util/pseudonym.c
+++ b/src/util/pseudonym.c
@@ -281,7 +281,7 @@ GNUNET_PSEUDONYM_id_to_name (struct GNUNET_CONFIGURATION_Handle *cfg,
281 GNUNET_HashCode nh; 281 GNUNET_HashCode nh;
282 char *fn; 282 char *fn;
283 unsigned long long len; 283 unsigned long long len;
284 struct GNUNET_IO_Handle *fh; 284 struct GNUNET_DISK_FileHandle *fh;
285 unsigned int i; 285 unsigned int i;
286 unsigned int idx; 286 unsigned int idx;
287 char *ret; 287 char *ret;
@@ -342,7 +342,7 @@ GNUNET_PSEUDONYM_id_to_name (struct GNUNET_CONFIGURATION_Handle *cfg,
342 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode))) 342 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
343 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); 343 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
344 } 344 }
345 GNUNET_DISK_file_close (&fh); 345 GNUNET_DISK_file_close (fh);
346 ret = GNUNET_malloc (strlen (name) + 32); 346 ret = GNUNET_malloc (strlen (name) + 32);
347 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx); 347 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
348 GNUNET_free (name); 348 GNUNET_free (name);
@@ -365,7 +365,7 @@ GNUNET_PSEUDONYM_name_to_id (struct GNUNET_CONFIGURATION_Handle *cfg,
365 char *name; 365 char *name;
366 GNUNET_HashCode nh; 366 GNUNET_HashCode nh;
367 char *fn; 367 char *fn;
368 struct GNUNET_IO_Handle *fh; 368 struct GNUNET_DISK_FileHandle *fh;
369 369
370 idx = -1; 370 idx = -1;
371 slen = strlen (ns_uname); 371 slen = strlen (ns_uname);
@@ -394,10 +394,10 @@ GNUNET_PSEUDONYM_name_to_id (struct GNUNET_CONFIGURATION_Handle *cfg,
394 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_SEEK_SET); 394 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_SEEK_SET);
395 if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode))) 395 if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
396 { 396 {
397 GNUNET_DISK_file_close (&fh); 397 GNUNET_DISK_file_close (fh);
398 return GNUNET_SYSERR; 398 return GNUNET_SYSERR;
399 } 399 }
400 GNUNET_DISK_file_close (&fh); 400 GNUNET_DISK_file_close (fh);
401 return GNUNET_OK; 401 return GNUNET_OK;
402} 402}
403 403