aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNils Durner <durner@gnunet.org>2009-06-15 20:11:51 +0000
committerNils Durner <durner@gnunet.org>2009-06-15 20:11:51 +0000
commit1088905b5a89a24706d2c1877d289589e3f3c222 (patch)
tree38ec7cdc1b80502aa950c68ebefe1e3f42efa14b /src
parent7e3df72d6b8a32b15b02110b54c77f77e28928a2 (diff)
downloadgnunet-1088905b5a89a24706d2c1877d289589e3f3c222.tar.gz
gnunet-1088905b5a89a24706d2c1877d289589e3f3c222.zip
more complete DISK API
Diffstat (limited to 'src')
-rw-r--r--src/include/Makefile.am1
-rw-r--r--src/include/gnunet_disk_lib.h137
-rw-r--r--src/include/gnunet_io_lib.h46
-rw-r--r--src/include/gnunet_util_lib.h1
-rw-r--r--src/peerinfo/gnunet-service-peerinfo.c21
-rw-r--r--src/statistics/gnunet-service-statistics.c36
-rw-r--r--src/topology/gnunet-daemon-topology.c80
-rw-r--r--src/util/Makefile.am2
-rw-r--r--src/util/container_bloomfilter.c86
-rw-r--r--src/util/crypto_hash.c15
-rw-r--r--src/util/disk.c566
-rw-r--r--src/util/io_handle.c58
-rw-r--r--src/util/io_handle.h38
-rw-r--r--src/util/pseudonym.c31
-rw-r--r--src/util/service.c2
-rw-r--r--src/util/test_disk.c35
-rw-r--r--src/util/test_os_load.c20
17 files changed, 885 insertions, 290 deletions
diff --git a/src/include/Makefile.am b/src/include/Makefile.am
index bc832c444..0fb7aa0fd 100644
--- a/src/include/Makefile.am
+++ b/src/include/Makefile.am
@@ -24,6 +24,7 @@ gnunetinclude_HEADERS = \
24 gnunet_fragmentation_lib.h \ 24 gnunet_fragmentation_lib.h \
25 gnunet_getopt_lib.h \ 25 gnunet_getopt_lib.h \
26 gnunet_hello_lib.h \ 26 gnunet_hello_lib.h \
27 gnunet_io_lib.h \
27 gnunet_network_lib.h \ 28 gnunet_network_lib.h \
28 gnunet_peerinfo_service.h \ 29 gnunet_peerinfo_service.h \
29 gnunet_program_lib.h \ 30 gnunet_program_lib.h \
diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h
index 3886be7c9..23a4789fa 100644
--- a/src/include/gnunet_disk_lib.h
+++ b/src/include/gnunet_disk_lib.h
@@ -28,6 +28,7 @@
28 28
29#include "gnunet_configuration_lib.h" 29#include "gnunet_configuration_lib.h"
30#include "gnunet_scheduler_lib.h" 30#include "gnunet_scheduler_lib.h"
31#include "gnunet_io_lib.h"
31 32
32/* we need size_t, and since it can be both unsigned int 33/* we need size_t, and since it can be both unsigned int
33 or unsigned long long, this IS platform dependent; 34 or unsigned long long, this IS platform dependent;
@@ -43,6 +44,37 @@ extern "C"
43#endif 44#endif
44#endif 45#endif
45 46
47/* Open the file for reading */
48#define GNUNET_DISK_OPEN_READ 1
49/* Open the file for writing */
50#define GNUNET_DISK_OPEN_WRITE 2
51/* Open the file for both reading and writing */
52#define GNUNET_DISK_OPEN_READWRITE 3
53/* Fail if file already exists */
54#define GNUNET_DISK_OPEN_FAILIFEXISTS 4
55/* Truncate file if it exists */
56#define GNUNET_DISK_OPEN_TRUNCATE 8
57/* Create file if it doesn't exist */
58#define GNUNET_DISK_OPEN_CREATE 16
59/* Append to the file */
60#define GNUNET_DISK_OPEN_APPEND 32
61
62#define GNUNET_DISK_MAP_READ 1
63#define GNUNET_DISK_MAP_WRITE 2
64#define GNUNET_DISK_MAP_READWRITE 3
65
66#define GNUNET_DISK_PERM_USER_READ 1
67#define GNUNET_DISK_PERM_USER_WRITE 2
68#define GNUNET_DISK_PERM_USER_EXEC 4
69#define GNUNET_DISK_PERM_GROUP_READ 8
70#define GNUNET_DISK_PERM_GROUP_WRITE 16
71#define GNUNET_DISK_PERM_GROUP_EXEC 32
72#define GNUNET_DISK_PERM_OTHER_READ 64
73#define GNUNET_DISK_PERM_OTHER_WRITE 128
74#define GNUNET_DISK_PERM_OTHER_EXEC 256
75
76enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END};
77
46/** 78/**
47 * Get the number of blocks that are left on the partition that 79 * Get the number of blocks that are left on the partition that
48 * contains the given file (for normal users). 80 * contains the given file (for normal users).
@@ -64,6 +96,18 @@ int GNUNET_DISK_file_test (const char *fil);
64 96
65 97
66/** 98/**
99 * Move the read/write pointer in a file
100 * @param h handle of an open file
101 * @param offset position to move to
102 * @param whence specification to which position the offset parameter relates to
103 * @return the new position on success, GNUNET_SYSERR otherwise
104 */
105off_t
106GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset,
107 enum GNUNET_DISK_Seek whence);
108
109
110/**
67 * Get the size of the file (or directory) 111 * Get the size of the file (or directory)
68 * of the given file (in bytes). 112 * of the given file (in bytes).
69 * 113 *
@@ -77,41 +121,63 @@ int GNUNET_DISK_file_size (const char *filename,
77 121
78 122
79/** 123/**
80 * Wrapper around "open()". Opens a file. 124 * Open a file
81 * 125 * @param fn file name to be opened
82 * @return file handle, -1 on error 126 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
127 * @param perm permissions for the newly created file
128 * @return IO handle on success, NULL on error
83 */ 129 */
84int GNUNET_DISK_file_open (const char *filename, int oflag, ...); 130struct GNUNET_IO_Handle *GNUNET_DISK_file_open (const char *fn, int flags, ...);
85 131
86 132
87/** 133/**
88 * Wrapper around "close()". Closes a file. 134 * Close an open file
135 * @param h file handle
136 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
89 */ 137 */
90void GNUNET_DISK_file_close (const char *filename, int fd); 138int GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h);
91 139
92 140
93/** 141/**
94 * Read the contents of a binary file into a buffer. 142 * Read the contents of a binary file into a buffer.
95 * @param fileName the name of the file, not freed, 143 * @param h handle to an open file
96 * must already be expanded! 144 * @param result the buffer to write the result to
97 * @param len the maximum number of bytes to read 145 * @param len the maximum number of bytes to read
146 * @return the number of bytes read on success, GNUNET_SYSERR on failure
147 */
148int GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len);
149
150
151/**
152 * Read the contents of a binary file into a buffer.
153 * @param fn file name
98 * @param result the buffer to write the result to 154 * @param result the buffer to write the result to
99 * @return the number of bytes read on success, -1 on failure 155 * @param len the maximum number of bytes to read
156 * @return the number of bytes read on success, GNUNET_SYSERR on failure
100 */ 157 */
101int GNUNET_DISK_file_read (const char *fileName, int len, void *result); 158int GNUNET_DISK_fn_read (const char * const fn, void *result, int len);
102 159
103 160
104/** 161/**
105 * Write a buffer to a file. 162 * Write a buffer to a file.
106 * @param fileName the name of the file, NOT freed! 163 * @param h handle to open file
107 * @param buffer the data to write 164 * @param buffer the data to write
108 * @param n number of bytes to write 165 * @param n number of bytes to write
109 * @param mode the mode for file permissions 166 * @return number of bytes written on success, GNUNET_SYSERR on error
110 * @return GNUNET_OK on success, GNUNET_SYSERR on error
111 */ 167 */
112int GNUNET_DISK_file_write (const char *fileName, 168int GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer,
113 const void *buffer, unsigned int n, 169 unsigned int n);
114 const char *mode); 170
171
172/**
173 * Write a buffer to a file.
174 * @param fn file name
175 * @param buffer the data to write
176 * @param n number of bytes to write
177 * @return number of bytes written on success, GNUNET_SYSERR on error
178 */
179int GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
180 unsigned int n, int mode);
115 181
116 182
117/** 183/**
@@ -235,6 +301,18 @@ int GNUNET_DISK_directory_create (const char *dir);
235 301
236 302
237/** 303/**
304 * Lock a part of a file
305 * @param fh file handle
306 * @lockStart absolute position from where to lock
307 * @lockEnd absolute position until where to lock
308 * @return GNUNET_OK on success, GNUNET_SYSERR on error
309 */
310int
311GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart,
312 off_t lockEnd);
313
314
315/**
238 * @brief Removes special characters as ':' from a filename. 316 * @brief Removes special characters as ':' from a filename.
239 * @param fn the filename to canonicalize 317 * @param fn the filename to canonicalize
240 */ 318 */
@@ -266,6 +344,33 @@ int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
266char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg, 344char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
267 const char *serviceName, ...); 345 const char *serviceName, ...);
268 346
347/**
348 * Map a file into memory
349 * @param h open file handle
350 * @param m handle to the new mapping
351 * @param access access specification, GNUNET_DISK_MAP_xxx
352 * @param len size of the mapping
353 * @return pointer to the mapped memory region, NULL on failure
354 */
355void *GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m,
356 int access, size_t len);
357
358/**
359 * Unmap a file
360 * @param h mapping handle
361 * @param addr pointer to the mapped memory region
362 * @param len size of the mapping
363 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
364 */
365int GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len);
366
367/**
368 * Write file changes to disk
369 * @param h handle to an open file
370 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
371 */
372int GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h);
373
269#if 0 /* keep Emacsens' auto-indent happy */ 374#if 0 /* keep Emacsens' auto-indent happy */
270{ 375{
271#endif 376#endif
diff --git a/src/include/gnunet_io_lib.h b/src/include/gnunet_io_lib.h
new file mode 100644
index 000000000..3d04a2317
--- /dev/null
+++ b/src/include/gnunet_io_lib.h
@@ -0,0 +1,46 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file include/gnunet_io_lib.h
23 * @brief Helper functions for abstract IO handles
24 * @author Nils Durner
25 */
26
27#ifndef IO_HANDLE_H_
28#define IO_HANDLE_H_
29
30struct GNUNET_IO_Handle;
31
32/**
33 * Checks whether a handle is invalid
34 * @param h handle to check
35 * @return GNUNET_YES if invalid, GNUNET_NO if valid
36 */
37int GNUNET_IO_handle_invalid (const struct GNUNET_IO_Handle *h);
38
39/**
40 * Mark a handle as invalid
41 * @param h file handle
42 */
43void GNUNET_IO_handle_invalidate (struct GNUNET_IO_Handle *h);
44
45
46#endif /* IO_HANDLE_H_ */
diff --git a/src/include/gnunet_util_lib.h b/src/include/gnunet_util_lib.h
index 6354e28fa..58621caf5 100644
--- a/src/include/gnunet_util_lib.h
+++ b/src/include/gnunet_util_lib.h
@@ -43,6 +43,7 @@ extern "C"
43#include "gnunet_crypto_lib.h" 43#include "gnunet_crypto_lib.h"
44#include "gnunet_disk_lib.h" 44#include "gnunet_disk_lib.h"
45#include "gnunet_getopt_lib.h" 45#include "gnunet_getopt_lib.h"
46#include "gnunet_io_lib.h"
46#include "gnunet_network_lib.h" 47#include "gnunet_network_lib.h"
47#include "gnunet_plugin_lib.h" 48#include "gnunet_plugin_lib.h"
48#include "gnunet_program_lib.h" 49#include "gnunet_program_lib.h"
diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c
index 0d383c0f1..eb2dc4264 100644
--- a/src/peerinfo/gnunet-service-peerinfo.c
+++ b/src/peerinfo/gnunet-service-peerinfo.c
@@ -197,14 +197,14 @@ add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
197 entry->identity = *identity; 197 entry->identity = *identity;
198 fn = get_trust_filename (identity); 198 fn = get_trust_filename (identity);
199 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) && 199 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
200 (sizeof (trust) == GNUNET_DISK_file_read (fn, sizeof (trust), &trust))) 200 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
201 entry->disk_trust = entry->trust = ntohl (trust); 201 entry->disk_trust = entry->trust = ntohl (trust);
202 GNUNET_free (fn); 202 GNUNET_free (fn);
203 203
204 fn = get_host_filename (identity); 204 fn = get_host_filename (identity);
205 if (GNUNET_DISK_file_test (fn) == GNUNET_YES) 205 if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
206 { 206 {
207 size = GNUNET_DISK_file_read (fn, sizeof (buffer), buffer); 207 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
208 hello = (const struct GNUNET_HELLO_Message *) buffer; 208 hello = (const struct GNUNET_HELLO_Message *) buffer;
209 now = GNUNET_TIME_absolute_get (); 209 now = GNUNET_TIME_absolute_get ();
210 hello_clean = GNUNET_HELLO_iterate_addresses (hello, 210 hello_clean = GNUNET_HELLO_iterate_addresses (hello,
@@ -371,7 +371,9 @@ bind_address (const struct GNUNET_PeerIdentity *peer,
371 host->hello = mrg; 371 host->hello = mrg;
372 } 372 }
373 fn = get_host_filename (peer); 373 fn = get_host_filename (peer);
374 GNUNET_DISK_file_write (fn, host->hello, GNUNET_HELLO_size (hello), "644"); 374 GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (hello),
375 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
376 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
375 GNUNET_free (fn); 377 GNUNET_free (fn);
376} 378}
377 379
@@ -455,8 +457,9 @@ flush_trust (struct HostEntry *host)
455 else 457 else
456 { 458 {
457 trust = htonl (host->trust); 459 trust = htonl (host->trust);
458 if (GNUNET_OK == 460 if (GNUNET_OK == GNUNET_DISK_fn_write (fn, &trust, sizeof(uint32_t),
459 GNUNET_DISK_file_write (fn, &trust, sizeof (uint32_t), "644")) 461 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
462 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
460 host->disk_trust = host->trust; 463 host->disk_trust = host->trust;
461 } 464 }
462 GNUNET_free (fn); 465 GNUNET_free (fn);
@@ -497,7 +500,7 @@ discard_hosts_helper (void *cls, const char *fn)
497 struct GNUNET_HELLO_Message *new_hello; 500 struct GNUNET_HELLO_Message *new_hello;
498 int size; 501 int size;
499 502
500 size = GNUNET_DISK_file_read (fn, sizeof (buffer), buffer); 503 size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
501 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn))) 504 if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
502 { 505 {
503 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | 506 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
@@ -513,9 +516,9 @@ discard_hosts_helper (void *cls, const char *fn)
513 GNUNET_ERROR_TYPE_BULK, "unlink", fn); 516 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
514 if (new_hello != NULL) 517 if (new_hello != NULL)
515 { 518 {
516 GNUNET_DISK_file_write (fn, 519 GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
517 new_hello, 520 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
518 GNUNET_HELLO_size (new_hello), "644"); 521 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
519 GNUNET_free (new_hello); 522 GNUNET_free (new_hello);
520 } 523 }
521 return GNUNET_OK; 524 return GNUNET_OK;
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c
index 42ff8d927..c7388ebd9 100644
--- a/src/statistics/gnunet-service-statistics.c
+++ b/src/statistics/gnunet-service-statistics.c
@@ -99,7 +99,7 @@ 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 int fd; 102 struct GNUNET_IO_Handle *fh, *mh;
103 struct stat sb; 103 struct stat sb;
104 char *buf; 104 char *buf;
105 size_t off; 105 size_t off;
@@ -114,17 +114,17 @@ load (struct GNUNET_SERVER_Handle *server,
114 GNUNET_free (fn); 114 GNUNET_free (fn);
115 return; 115 return;
116 } 116 }
117 fd = GNUNET_DISK_file_open (fn, O_RDONLY); 117 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
118 if (fd == -1) 118 if (!fh)
119 { 119 {
120 GNUNET_free (fn); 120 GNUNET_free (fn);
121 return; 121 return;
122 } 122 }
123 buf = MMAP (NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); 123 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_READ, sb.st_size);
124 if (MAP_FAILED == buf) 124 if (NULL == buf)
125 { 125 {
126 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn); 126 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
127 GNUNET_break (0 == CLOSE (fd)); 127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh));
128 GNUNET_free (fn); 128 GNUNET_free (fn);
129 return; 129 return;
130 } 130 }
@@ -143,8 +143,8 @@ load (struct GNUNET_SERVER_Handle *server,
143 } 143 }
144 off += ntohs (msg->size); 144 off += ntohs (msg->size);
145 } 145 }
146 GNUNET_break (0 == MUNMAP (buf, sb.st_size)); 146 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (&mh, buf, sb.st_size));
147 GNUNET_break (0 == CLOSE (fd)); 147 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh));
148 GNUNET_free (fn); 148 GNUNET_free (fn);
149} 149}
150 150
@@ -160,39 +160,37 @@ save (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
160{ 160{
161 struct StatsEntry *pos; 161 struct StatsEntry *pos;
162 char *fn; 162 char *fn;
163 int fd; 163 struct GNUNET_IO_Handle *fh;
164 uint16_t size; 164 uint16_t size;
165 unsigned long long total; 165 unsigned long long total;
166 166
167 fd = -1;
168 fn = GNUNET_DISK_get_home_filename (cfg, 167 fn = GNUNET_DISK_get_home_filename (cfg,
169 "statistics", "statistics.data", NULL); 168 "statistics", "statistics.data", NULL);
170 if (fn != NULL) 169 if (fn != NULL)
171 fd = 170 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
172 GNUNET_DISK_file_open (fn, O_WRONLY | O_CREAT | O_TRUNC, 171 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
173 S_IRUSR | S_IWUSR); 172 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
174 total = 0; 173 total = 0;
175 while (NULL != (pos = start)) 174 while (NULL != (pos = start))
176 { 175 {
177 start = pos->next; 176 start = pos->next;
178 if ((pos->persistent) && (fd != -1)) 177 if ((pos->persistent) && fh)
179 { 178 {
180 size = htons (pos->msg->header.size); 179 size = htons (pos->msg->header.size);
181 if (size != WRITE (fd, pos->msg, size)) 180 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
182 { 181 {
183 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 182 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
184 "write", fn); 183 "write", fn);
185 GNUNET_DISK_file_close (fn, fd); 184 GNUNET_DISK_file_close (&fh);
186 fd = -1;
187 } 185 }
188 else 186 else
189 total += size; 187 total += size;
190 } 188 }
191 GNUNET_free (pos); 189 GNUNET_free (pos);
192 } 190 }
193 if (fd != -1) 191 if (fh)
194 { 192 {
195 GNUNET_DISK_file_close (fn, fd); 193 GNUNET_DISK_file_close (&fh);
196 if (total == 0) 194 if (total == 0)
197 GNUNET_break (0 == UNLINK (fn)); 195 GNUNET_break (0 == UNLINK (fn));
198 else 196 else
diff --git a/src/topology/gnunet-daemon-topology.c b/src/topology/gnunet-daemon-topology.c
index 0ef896714..f4884e00d 100644
--- a/src/topology/gnunet-daemon-topology.c
+++ b/src/topology/gnunet-daemon-topology.c
@@ -38,26 +38,26 @@
38/** 38/**
39 * For how long do we blacklist a peer after a failed 39 * For how long do we blacklist a peer after a failed
40 * connection attempt? 40 * connection attempt?
41 */ 41 */
42#define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS 42#define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS
43 43
44/** 44/**
45 * For how long do we blacklist a friend after a failed 45 * For how long do we blacklist a friend after a failed
46 * connection attempt? 46 * connection attempt?
47 */ 47 */
48#define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) 48#define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
49 49
50/** 50/**
51 * How frequently are we allowed to ask PEERINFO for more 51 * How frequently are we allowed to ask PEERINFO for more
52 * HELLO's to advertise (at most)? 52 * HELLO's to advertise (at most)?
53 */ 53 */
54#define MIN_HELLO_GATHER_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 27) 54#define MIN_HELLO_GATHER_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 27)
55 55
56/** 56/**
57 * How often do we at most advertise the same HELLO to the same peer? 57 * How often do we at most advertise the same HELLO to the same peer?
58 * Also used to remove HELLOs of peers that PEERINFO no longer lists 58 * Also used to remove HELLOs of peers that PEERINFO no longer lists
59 * from our cache. 59 * from our cache.
60 */ 60 */
61#define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12) 61#define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
62 62
63 63
@@ -97,7 +97,7 @@ struct PeerList
97 * ID of the peer. 97 * ID of the peer.
98 */ 98 */
99 struct GNUNET_PeerIdentity id; 99 struct GNUNET_PeerIdentity id;
100 100
101}; 101};
102 102
103 103
@@ -122,12 +122,12 @@ struct HelloList
122 * this HELLO. 122 * this HELLO.
123 */ 123 */
124 struct GNUNET_CONTAINER_BloomFilter *filter; 124 struct GNUNET_CONTAINER_BloomFilter *filter;
125 125
126 /** 126 /**
127 * What peer is this HELLO for? 127 * What peer is this HELLO for?
128 */ 128 */
129 struct GNUNET_PeerIdentity id; 129 struct GNUNET_PeerIdentity id;
130 130
131 /** 131 /**
132 * When should we remove this entry from the linked list (either 132 * When should we remove this entry from the linked list (either
133 * resetting the filter or possibly eliminating it for good because 133 * resetting the filter or possibly eliminating it for good because
@@ -152,12 +152,12 @@ static struct GNUNET_SCHEDULER_Handle * sched;
152 * Our configuration. 152 * Our configuration.
153 */ 153 */
154static struct GNUNET_CONFIGURATION_Handle * cfg; 154static struct GNUNET_CONFIGURATION_Handle * cfg;
155 155
156/** 156/**
157 * Handle to the core API. 157 * Handle to the core API.
158 */ 158 */
159static struct GNUNET_CORE_Handle *handle; 159static struct GNUNET_CORE_Handle *handle;
160 160
161/** 161/**
162 * Handle to the transport API. 162 * Handle to the transport API.
163 */ 163 */
@@ -167,7 +167,7 @@ static struct GNUNET_TRANSPORT_Handle *transport;
167 * Identity of this peer. 167 * Identity of this peer.
168 */ 168 */
169static struct GNUNET_PeerIdentity my_identity; 169static struct GNUNET_PeerIdentity my_identity;
170 170
171/** 171/**
172 * Linked list of all of our friends and all of our current 172 * Linked list of all of our friends and all of our current
173 * neighbours. 173 * neighbours.
@@ -194,17 +194,17 @@ static unsigned int minimum_friend_count;
194 * Number of peers (friends and others) that we are currently connected to. 194 * Number of peers (friends and others) that we are currently connected to.
195 */ 195 */
196static unsigned int connection_count; 196static unsigned int connection_count;
197 197
198/** 198/**
199 * Target number of connections. 199 * Target number of connections.
200 */ 200 */
201static unsigned int target_connection_count; 201static unsigned int target_connection_count;
202 202
203/** 203/**
204 * Number of friends that we are currently connected to. 204 * Number of friends that we are currently connected to.
205 */ 205 */
206static unsigned int friend_count; 206static unsigned int friend_count;
207 207
208/** 208/**
209 * Should the topology daemon try to establish connections? 209 * Should the topology daemon try to establish connections?
210 */ 210 */
@@ -239,7 +239,7 @@ force_disconnect (const struct GNUNET_PeerIdentity *peer)
239 * Function called by core when our attempt to connect 239 * Function called by core when our attempt to connect
240 * succeeded. Does nothing. 240 * succeeded. Does nothing.
241 */ 241 */
242static size_t 242static size_t
243ready_callback (void *cls, 243ready_callback (void *cls,
244 size_t size, void *buf) 244 size_t size, void *buf)
245{ 245{
@@ -346,7 +346,7 @@ static void connect_notify (void *cls,
346 GNUNET_assert (GNUNET_NO == pos->is_connected); 346 GNUNET_assert (GNUNET_NO == pos->is_connected);
347 pos->is_connected = GNUNET_YES; 347 pos->is_connected = GNUNET_YES;
348 pos->blacklisted_until.value = 0; /* remove blacklisting */ 348 pos->blacklisted_until.value = 0; /* remove blacklisting */
349 friend_count++; 349 friend_count++;
350 return; 350 return;
351 } 351 }
352 pos = pos->next; 352 pos = pos->next;
@@ -364,8 +364,8 @@ static void connect_notify (void *cls,
364/** 364/**
365 * Disconnect from all non-friends (we're below quota). 365 * Disconnect from all non-friends (we're below quota).
366 */ 366 */
367static void 367static void
368drop_non_friends () 368drop_non_friends ()
369{ 369{
370 struct PeerList *pos; 370 struct PeerList *pos;
371 371
@@ -427,7 +427,7 @@ static void disconnect_notify (void *cls,
427 } 427 }
428 prev = pos; 428 prev = pos;
429 pos = pos->next; 429 pos = pos->next;
430 } 430 }
431 GNUNET_break (0); 431 GNUNET_break (0);
432} 432}
433 433
@@ -444,12 +444,12 @@ find_more_peers (void *cls,
444/** 444/**
445 * Determine when we should try again to find more peers and 445 * Determine when we should try again to find more peers and
446 * schedule the task. 446 * schedule the task.
447 */ 447 */
448static void 448static void
449schedule_peer_search () 449schedule_peer_search ()
450{ 450{
451 struct GNUNET_TIME_Relative delay; 451 struct GNUNET_TIME_Relative delay;
452 452
453 /* Typically, we try again every 15 minutes; the minimum period is 453 /* Typically, we try again every 15 minutes; the minimum period is
454 15s; if we are above the connection target, we reduce re-trying 454 15s; if we are above the connection target, we reduce re-trying
455 by the square of how much we are above; so for example, with 200% 455 by the square of how much we are above; so for example, with 200%
@@ -473,8 +473,8 @@ schedule_peer_search ()
473 473
474 474
475/** 475/**
476 * Iterator called on each address. 476 * Iterator called on each address.
477 * 477 *
478 * @param cls flag that we will set if we see any addresses. 478 * @param cls flag that we will set if we see any addresses.
479 */ 479 */
480static int 480static int
@@ -526,18 +526,18 @@ consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
526 memcpy (&pos->msg, hello, size); 526 memcpy (&pos->msg, hello, size);
527 pos->id = pid; 527 pos->id = pid;
528 pos->expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY); 528 pos->expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
529 /* 2^{-5} chance of not sending a HELLO to a peer is 529 /* 2^{-5} chance of not sending a HELLO to a peer is
530 acceptably small (if the filter is 50% full); 530 acceptably small (if the filter is 50% full);
531 64 bytes of memory are small compared to the rest 531 64 bytes of memory are small compared to the rest
532 of the data structure and would only really become 532 of the data structure and would only really become
533 "useless" once a HELLO has been passed on to ~100 533 "useless" once a HELLO has been passed on to ~100
534 other peers, which is likely more than enough in 534 other peers, which is likely more than enough in
535 any case; hence 64, 5 as bloomfilter parameters. */ 535 any case; hence 64, 5 as bloomfilter parameters. */
536 pos->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5); 536 pos->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
537 /* never send a peer its own HELLO */ 537 /* never send a peer its own HELLO */
538 GNUNET_CONTAINER_bloomfilter_add (pos->filter, &pos->id.hashPubKey); 538 GNUNET_CONTAINER_bloomfilter_add (pos->filter, &pos->id.hashPubKey);
539 pos->next = hellos; 539 pos->next = hellos;
540 hellos = pos; 540 hellos = pos;
541} 541}
542 542
543 543
@@ -585,7 +585,7 @@ process_peer (void *cls,
585 } 585 }
586 } 586 }
587 pos = pos->next; 587 pos = pos->next;
588 } 588 }
589 if (GNUNET_YES == friends_only) 589 if (GNUNET_YES == friends_only)
590 return; 590 return;
591 if (friend_count < minimum_friend_count) 591 if (friend_count < minimum_friend_count)
@@ -700,7 +700,7 @@ core_init (void *cls,
700 { 700 {
701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
702 _("Failed to connect to core service, can not manage topology!\n")); 702 _("Failed to connect to core service, can not manage topology!\n"));
703 return; 703 return;
704 } 704 }
705 handle = server; 705 handle = server;
706 my_identity = *my_id; 706 my_identity = *my_id;
@@ -744,7 +744,7 @@ read_friends_file (struct GNUNET_CONFIGURATION_Handle *cfg)
744 "FRIENDS", 744 "FRIENDS",
745 &fn); 745 &fn);
746 if (GNUNET_OK != GNUNET_DISK_file_test (fn)) 746 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
747 GNUNET_DISK_file_write (fn, NULL, 0, "600"); 747 GNUNET_DISK_fn_write (fn, NULL, 0, "600");
748 if (0 != STAT (fn, &frstat)) 748 if (0 != STAT (fn, &frstat))
749 { 749 {
750 if ((friends_only) || (minimum_friend_count > 0)) 750 if ((friends_only) || (minimum_friend_count > 0))
@@ -761,11 +761,11 @@ read_friends_file (struct GNUNET_CONFIGURATION_Handle *cfg)
761 _("Friends file `%s' is empty.\n"), 761 _("Friends file `%s' is empty.\n"),
762 fn); 762 fn);
763 GNUNET_free (fn); 763 GNUNET_free (fn);
764 return; 764 return;
765 } 765 }
766 data = GNUNET_malloc_large (frstat.st_size); 766 data = GNUNET_malloc_large (frstat.st_size);
767 if (frstat.st_size != 767 if (frstat.st_size !=
768 GNUNET_DISK_file_read (fn, frstat.st_size, data)) 768 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
769 { 769 {
770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
771 _("Failed to read friends list from `%s'\n"), fn); 771 _("Failed to read friends list from `%s'\n"), fn);
@@ -914,10 +914,10 @@ hello_advertising (void *cls,
914 while (NULL != (pos = next)) 914 while (NULL != (pos = next))
915 { 915 {
916 next = pos->next; 916 next = pos->next;
917 if (GNUNET_NO == 917 if (GNUNET_NO ==
918 GNUNET_CONTAINER_bloomfilter_test (pos->filter, 918 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
919 &receiver->hashPubKey)) 919 &receiver->hashPubKey))
920 break; 920 break;
921 if (0 == GNUNET_TIME_absolute_get_remaining (pos->expiration).value) 921 if (0 == GNUNET_TIME_absolute_get_remaining (pos->expiration).value)
922 { 922 {
923 /* time to discard... */ 923 /* time to discard... */
@@ -947,11 +947,11 @@ hello_advertising (void *cls,
947 size = 0; 947 size = 0;
948 } 948 }
949 return size; 949 return size;
950 } 950 }
951 if ( (GNUNET_NO == hello_gathering_active) && 951 if ( (GNUNET_NO == hello_gathering_active) &&
952 (GNUNET_TIME_absolute_get_duration (last_hello_gather_time).value > 952 (GNUNET_TIME_absolute_get_duration (last_hello_gather_time).value >
953 MIN_HELLO_GATHER_DELAY.value) ) 953 MIN_HELLO_GATHER_DELAY.value) )
954 { 954 {
955 hello_gathering_active = GNUNET_YES; 955 hello_gathering_active = GNUNET_YES;
956 last_hello_gather_time = GNUNET_TIME_absolute_get(); 956 last_hello_gather_time = GNUNET_TIME_absolute_get();
957 GNUNET_PEERINFO_for_all (cfg, 957 GNUNET_PEERINFO_for_all (cfg,
@@ -994,14 +994,14 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
994 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 994 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
995 * @param c configuration 995 * @param c configuration
996 */ 996 */
997static void 997static void
998run (void *cls, 998run (void *cls,
999 struct GNUNET_SCHEDULER_Handle * s, 999 struct GNUNET_SCHEDULER_Handle * s,
1000 char *const *args, 1000 char *const *args,
1001 const char *cfgfile, 1001 const char *cfgfile,
1002 struct GNUNET_CONFIGURATION_Handle * c) 1002 struct GNUNET_CONFIGURATION_Handle * c)
1003{ 1003{
1004 struct GNUNET_CORE_MessageHandler handlers[] = 1004 struct GNUNET_CORE_MessageHandler handlers[] =
1005 { 1005 {
1006 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0}, 1006 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1007 { NULL, 0, 0 } 1007 { NULL, 0, 0 }
@@ -1012,7 +1012,7 @@ run (void *cls,
1012 cfg = c; 1012 cfg = c;
1013 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1013 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1014 "TOPOLOGY", 1014 "TOPOLOGY",
1015 "AUTOCONNECT"); 1015 "AUTOCONNECT");
1016 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1016 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1017 "TOPOLOGY", 1017 "TOPOLOGY",
1018 "FRIENDS-ONLY"); 1018 "FRIENDS-ONLY");
@@ -1050,7 +1050,7 @@ run (void *cls,
1050 NULL, GNUNET_NO, 1050 NULL, GNUNET_NO,
1051 NULL, GNUNET_NO, 1051 NULL, GNUNET_NO,
1052 handlers); 1052 handlers);
1053 1053
1054 GNUNET_SCHEDULER_add_delayed (sched, 1054 GNUNET_SCHEDULER_add_delayed (sched,
1055 GNUNET_YES, 1055 GNUNET_YES,
1056 GNUNET_SCHEDULER_PRIORITY_IDLE, 1056 GNUNET_SCHEDULER_PRIORITY_IDLE,
@@ -1075,7 +1075,7 @@ main (int argc, char *const *argv)
1075 ret = (GNUNET_OK == 1075 ret = (GNUNET_OK ==
1076 GNUNET_PROGRAM_run (argc, 1076 GNUNET_PROGRAM_run (argc,
1077 argv, 1077 argv,
1078 "topology", 1078 "topology",
1079 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"), 1079 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1080 options, 1080 options,
1081 &run, NULL)) ? 0 : 1; 1081 &run, NULL)) ? 0 : 1;
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 64178e538..014521f90 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -33,6 +33,8 @@ libgnunetutil_la_SOURCES = \
33 disk.c \ 33 disk.c \
34 getopt.c \ 34 getopt.c \
35 getopt_helpers.c \ 35 getopt_helpers.c \
36 io_handle.c \
37 io_handle.h \
36 network.c \ 38 network.c \
37 os_installation.c \ 39 os_installation.c \
38 os_load.c \ 40 os_load.c \
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index 8b76ef8dc..37780cb2a 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 int fd; 63 struct GNUNET_IO_Handle *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
@@ -139,10 +139,10 @@ testBit (char *bitArray, unsigned int bitIdx)
139 * 139 *
140 * @param bitArray memory area to set the bit in 140 * @param bitArray memory area to set the bit in
141 * @param bitIdx which bit to test 141 * @param bitIdx which bit to test
142 * @param fd 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, int fd) 145incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_IO_Handle *fh)
146{ 146{
147 unsigned int fileSlot; 147 unsigned int fileSlot;
148 unsigned char value; 148 unsigned char value;
@@ -151,14 +151,14 @@ incrementBit (char *bitArray, unsigned int bitIdx, int fd)
151 unsigned int targetLoc; 151 unsigned int targetLoc;
152 152
153 setBit (bitArray, bitIdx); 153 setBit (bitArray, bitIdx);
154 if (fd == -1) 154 if (GNUNET_IO_handle_invalid (fh))
155 return; 155 return;
156 /* Update the counter file on disk */ 156 /* Update the counter file on disk */
157 fileSlot = bitIdx / 2; 157 fileSlot = bitIdx / 2;
158 targetLoc = bitIdx % 2; 158 targetLoc = bitIdx % 2;
159 159
160 GNUNET_assert (fileSlot == (unsigned int) LSEEK (fd, fileSlot, SEEK_SET)); 160 GNUNET_assert (fileSlot == (unsigned int) GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_SEEK_SET));
161 if (1 != READ (fd, &value, 1)) 161 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
162 value = 0; 162 value = 0;
163 low = value & 0xF; 163 low = value & 0xF;
164 high = (value & (~0xF)) >> 4; 164 high = (value & (~0xF)) >> 4;
@@ -174,8 +174,8 @@ incrementBit (char *bitArray, unsigned int bitIdx, int fd)
174 high++; 174 high++;
175 } 175 }
176 value = ((high << 4) | low); 176 value = ((high << 4) | low);
177 GNUNET_assert (fileSlot == (unsigned int) LSEEK (fd, fileSlot, SEEK_SET)); 177 GNUNET_assert (fileSlot == (unsigned int) GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_SEEK_SET));
178 GNUNET_assert (1 == WRITE (fd, &value, 1)); 178 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
179} 179}
180 180
181/** 181/**
@@ -184,10 +184,10 @@ incrementBit (char *bitArray, unsigned int bitIdx, int fd)
184 * 184 *
185 * @param bitArray memory area to set the bit in 185 * @param bitArray memory area to set the bit in
186 * @param bitIdx which bit to test 186 * @param bitIdx which bit to test
187 * @param fd 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, int fd) 190decrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_IO_Handle *fh)
191{ 191{
192 unsigned int fileSlot; 192 unsigned int fileSlot;
193 unsigned char value; 193 unsigned char value;
@@ -195,13 +195,13 @@ decrementBit (char *bitArray, unsigned int bitIdx, int fd)
195 unsigned int low; 195 unsigned int low;
196 unsigned int targetLoc; 196 unsigned int targetLoc;
197 197
198 if (fd == -1) 198 if (GNUNET_IO_handle_invalid (fh))
199 return; /* cannot decrement! */ 199 return; /* cannot decrement! */
200 /* Each char slot in the counter file holds two 4 bit counters */ 200 /* Each char slot in the counter file holds two 4 bit counters */
201 fileSlot = bitIdx / 2; 201 fileSlot = bitIdx / 2;
202 targetLoc = bitIdx % 2; 202 targetLoc = bitIdx % 2;
203 LSEEK (fd, fileSlot, SEEK_SET); 203 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_SEEK_SET);
204 if (1 != READ (fd, &value, 1)) 204 if (1 != GNUNET_DISK_file_read (fh, &value, 1))
205 value = 0; 205 value = 0;
206 low = value & 0xF; 206 low = value & 0xF;
207 high = (value & 0xF0) >> 4; 207 high = (value & 0xF0) >> 4;
@@ -226,8 +226,8 @@ decrementBit (char *bitArray, unsigned int bitIdx, int fd)
226 } 226 }
227 } 227 }
228 value = ((high << 4) | low); 228 value = ((high << 4) | low);
229 LSEEK (fd, fileSlot, SEEK_SET); 229 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_SEEK_SET);
230 GNUNET_assert (1 == WRITE (fd, &value, 1)); 230 GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
231} 231}
232 232
233#define BUFFSIZE 65536 233#define BUFFSIZE 65536
@@ -235,36 +235,36 @@ decrementBit (char *bitArray, unsigned int bitIdx, int fd)
235/** 235/**
236 * Creates a file filled with zeroes 236 * Creates a file filled with zeroes
237 * 237 *
238 * @param fd the file handle 238 * @param fh the file handle
239 * @param size the size of the file 239 * @param size the size of the file
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 (int fd, unsigned int size) 243makeEmptyFile (const struct GNUNET_IO_Handle *fh, unsigned int size)
244{ 244{
245 char *buffer; 245 char *buffer;
246 unsigned int bytesleft = size; 246 unsigned int bytesleft = size;
247 int res = 0; 247 int res = 0;
248 248
249 if (fd == -1) 249 if (GNUNET_IO_handle_invalid (fh))
250 return GNUNET_SYSERR; 250 return GNUNET_SYSERR;
251 buffer = GNUNET_malloc (BUFFSIZE); 251 buffer = GNUNET_malloc (BUFFSIZE);
252 memset (buffer, 0, BUFFSIZE); 252 memset (buffer, 0, BUFFSIZE);
253 LSEEK (fd, 0, SEEK_SET); 253 GNUNET_DISK_file_seek (fh, 0, GNUNET_SEEK_SET);
254 254
255 while (bytesleft > 0) 255 while (bytesleft > 0)
256 { 256 {
257 if (bytesleft > BUFFSIZE) 257 if (bytesleft > BUFFSIZE)
258 { 258 {
259 res = WRITE (fd, buffer, BUFFSIZE); 259 res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
260 bytesleft -= BUFFSIZE; 260 bytesleft -= BUFFSIZE;
261 } 261 }
262 else 262 else
263 { 263 {
264 res = WRITE (fd, buffer, bytesleft); 264 res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
265 bytesleft = 0; 265 bytesleft = 0;
266 } 266 }
267 GNUNET_assert (res != -1); 267 GNUNET_assert (res != GNUNET_SYSERR);
268 } 268 }
269 GNUNET_free (buffer); 269 GNUNET_free (buffer);
270 return GNUNET_OK; 270 return GNUNET_OK;
@@ -338,7 +338,7 @@ static void
338incrementBitCallback (struct GNUNET_CONTAINER_BloomFilter *bf, 338incrementBitCallback (struct GNUNET_CONTAINER_BloomFilter *bf,
339 unsigned int bit, void *arg) 339 unsigned int bit, void *arg)
340{ 340{
341 incrementBit (bf->bitArray, bit, bf->fd); 341 incrementBit (bf->bitArray, bit, bf->fh);
342} 342}
343 343
344/** 344/**
@@ -352,7 +352,7 @@ static void
352decrementBitCallback (struct GNUNET_CONTAINER_BloomFilter *bf, 352decrementBitCallback (struct GNUNET_CONTAINER_BloomFilter *bf,
353 unsigned int bit, void *arg) 353 unsigned int bit, void *arg)
354{ 354{
355 decrementBit (bf->bitArray, bit, bf->fd); 355 decrementBit (bf->bitArray, bit, bf->fh);
356} 356}
357 357
358/** 358/**
@@ -406,14 +406,10 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
406 /* Try to open a bloomfilter file */ 406 /* Try to open a bloomfilter file */
407 if (filename != NULL) 407 if (filename != NULL)
408 { 408 {
409#ifndef _MSC_VER 409 bf->fh = GNUNET_DISK_file_open (filename,
410 bf->fd = GNUNET_DISK_file_open (filename, O_RDWR | O_CREAT, 410 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
411 S_IRUSR | S_IWUSR); 411 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
412#else 412 if (!bf->fh)
413 bf->fd = GNUNET_DISK_file_open (filename,
414 O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
415#endif
416 if (-1 == bf->fd)
417 { 413 {
418 GNUNET_free (bf); 414 GNUNET_free (bf);
419 return NULL; 415 return NULL;
@@ -422,8 +418,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
422 } 418 }
423 else 419 else
424 { 420 {
425 bf->fd = -1;
426 bf->filename = NULL; 421 bf->filename = NULL;
422 bf->fh = NULL;
427 } 423 }
428 /* Alloc block */ 424 /* Alloc block */
429 bf->bitArray = GNUNET_malloc_large (size); 425 bf->bitArray = GNUNET_malloc_large (size);
@@ -431,7 +427,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
431 bf->addressesPerElement = k; 427 bf->addressesPerElement = k;
432 memset (bf->bitArray, 0, bf->bitArraySize); 428 memset (bf->bitArray, 0, bf->bitArraySize);
433 429
434 if (bf->fd != -1) 430 if (bf->filename != NULL)
435 { 431 {
436 /* Read from the file what bits we can */ 432 /* Read from the file what bits we can */
437 rbuff = GNUNET_malloc (BUFFSIZE); 433 rbuff = GNUNET_malloc (BUFFSIZE);
@@ -440,7 +436,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
440 { 436 {
441 int res; 437 int res;
442 438
443 res = READ (bf->fd, rbuff, BUFFSIZE); 439 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
444 if (res == 0) 440 if (res == 0)
445 break; /* is ok! we just did not use that many bits yet */ 441 break; /* is ok! we just did not use that many bits yet */
446 for (i = 0; i < res; i++) 442 for (i = 0; i < res; i++)
@@ -491,8 +487,8 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, unsigned int size,
491 return NULL; 487 return NULL;
492 } 488 }
493 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); 489 bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
494 bf->fd = -1;
495 bf->filename = NULL; 490 bf->filename = NULL;
491 bf->fh = NULL;
496 bf->bitArray = GNUNET_malloc_large (size); 492 bf->bitArray = GNUNET_malloc_large (size);
497 bf->bitArraySize = size; 493 bf->bitArraySize = size;
498 bf->addressesPerElement = k; 494 bf->addressesPerElement = k;
@@ -537,9 +533,11 @@ GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
537{ 533{
538 if (NULL == bf) 534 if (NULL == bf)
539 return; 535 return;
540 if (bf->fd != -1) 536 if (bf->filename != NULL)
541 GNUNET_DISK_file_close (bf->filename, bf->fd); 537 {
542 GNUNET_free_non_null (bf->filename); 538 GNUNET_DISK_file_close (&bf->fh);
539 GNUNET_free (bf->filename);
540 }
543 GNUNET_free (bf->bitArray); 541 GNUNET_free (bf->bitArray);
544 GNUNET_free (bf); 542 GNUNET_free (bf);
545} 543}
@@ -556,8 +554,8 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
556 return; 554 return;
557 555
558 memset (bf->bitArray, 0, bf->bitArraySize); 556 memset (bf->bitArray, 0, bf->bitArraySize);
559 if (bf->fd != -1) 557 if (bf->filename != NULL)
560 makeEmptyFile (bf->fd, bf->bitArraySize * 4); 558 makeEmptyFile (bf->fh, bf->bitArraySize * 4);
561} 559}
562 560
563 561
@@ -634,7 +632,7 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
634{ 632{
635 if (NULL == bf) 633 if (NULL == bf)
636 return; 634 return;
637 if (bf->fd == -1) 635 if (bf->filename == NULL)
638 return; 636 return;
639 iterateBits (bf, &decrementBitCallback, NULL, e); 637 iterateBits (bf, &decrementBitCallback, NULL, e);
640} 638}
@@ -668,8 +666,8 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
668 bf->bitArraySize = size; 666 bf->bitArraySize = size;
669 bf->bitArray = GNUNET_malloc (size); 667 bf->bitArray = GNUNET_malloc (size);
670 memset (bf->bitArray, 0, bf->bitArraySize); 668 memset (bf->bitArray, 0, bf->bitArraySize);
671 if (bf->fd != -1) 669 if (bf->filename != NULL)
672 makeEmptyFile (bf->fd, bf->bitArraySize * 4); 670 makeEmptyFile (bf->fh, bf->bitArraySize * 4);
673 while (GNUNET_YES == iterator (&hc, iterator_arg)) 671 while (GNUNET_YES == iterator (&hc, iterator_arg))
674 GNUNET_CONTAINER_bloomfilter_add (bf, &hc); 672 GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
675} 673}
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index 139496eac..5a7cb33d7 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 int fd; 432 struct GNUNET_IO_Handle *fh;
433 433
434}; 434};
435 435
@@ -443,8 +443,8 @@ file_hash_finish (struct FileHashContext *fhc, const GNUNET_HashCode * res)
443{ 443{
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 (fhc->fd != -1) 446 if (!GNUNET_IO_handle_invalid (fhc->fh))
447 GNUNET_break (0 == CLOSE (fhc->fd)); 447 GNUNET_break (0 == 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
@@ -466,7 +466,7 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
466 delta = fhc->bsize; 466 delta = fhc->bsize;
467 if (fhc->fsize - fhc->offset < delta) 467 if (fhc->fsize - fhc->offset < delta)
468 delta = fhc->fsize - fhc->offset; 468 delta = fhc->fsize - fhc->offset;
469 if (delta != READ (fhc->fd, fhc->buffer, delta)) 469 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
470 { 470 {
471 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 471 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
472 "read", fhc->filename); 472 "read", fhc->filename);
@@ -517,7 +517,7 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
517 fhc->callback_cls = callback_cls; 517 fhc->callback_cls = callback_cls;
518 fhc->buffer = (unsigned char *) &fhc[1]; 518 fhc->buffer = (unsigned char *) &fhc[1];
519 fhc->filename = GNUNET_strdup (filename); 519 fhc->filename = GNUNET_strdup (filename);
520 fhc->fd = -1; 520 fhc->fh = NULL;
521 sha512_init (&fhc->hctx); 521 sha512_init (&fhc->hctx);
522 fhc->bsize = blocksize; 522 fhc->bsize = blocksize;
523 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO)) 523 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO))
@@ -526,8 +526,9 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
526 return; 526 return;
527 } 527 }
528 fhc->run_on_shutdown = run_on_shutdown; 528 fhc->run_on_shutdown = run_on_shutdown;
529 fhc->fd = GNUNET_DISK_file_open (filename, O_RDONLY | O_LARGEFILE); 529 fhc->fh = GNUNET_DISK_file_open (filename,
530 if (fhc->fd == -1) 530 GNUNET_DISK_OPEN_READ);
531 if (!fhc->fh)
531 { 532 {
532 file_hash_finish (fhc, NULL); 533 file_hash_finish (fhc, NULL);
533 return; 534 return;
diff --git a/src/util/disk.c b/src/util/disk.c
index 5042f0867..b0d3b18a4 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2001, 2002, 2005, 2006 Christian Grothoff (and other contributing authors) 3 (C) 2001, 2002, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -22,11 +22,14 @@
22 * @file util/disk.c 22 * @file util/disk.c
23 * @brief disk IO convenience methods 23 * @brief disk IO convenience methods
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 * @author Nils Durner
25 */ 26 */
26 27
27#include "platform.h" 28#include "platform.h"
29#include "io_handle.h"
28#include "gnunet_common.h" 30#include "gnunet_common.h"
29#include "gnunet_directories.h" 31#include "gnunet_directories.h"
32#include "gnunet_io_lib.h"
30#include "gnunet_disk_lib.h" 33#include "gnunet_disk_lib.h"
31#include "gnunet_scheduler_lib.h" 34#include "gnunet_scheduler_lib.h"
32#include "gnunet_strings_lib.h" 35#include "gnunet_strings_lib.h"
@@ -108,6 +111,44 @@ getSizeRec (void *ptr, const char *fn)
108 return GNUNET_OK; 111 return GNUNET_OK;
109} 112}
110 113
114
115/**
116 * Move the read/write pointer in a file
117 * @param h handle of an open file
118 * @param offset position to move to
119 * @param whence specification to which position the offset parameter relates to
120 * @return the new position on success, GNUNET_SYSERR otherwise
121 */
122off_t
123GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset,
124 enum GNUNET_DISK_Seek whence)
125{
126 if (h == NULL)
127 {
128 errno = EINVAL;
129 return GNUNET_SYSERR;
130 }
131
132#ifdef MINGW
133 DWORD ret;
134 static DWORD t[] = { [GNUNET_SEEK_SET] = FILE_BEGIN,
135 [GNUNET_SEEK_CUR] = FILE_CURRENT, [GNUNET_SEEK_END] = FILE_END };
136
137 ret = SetFilePointer (h->h, offset, NULL, t[whence]);
138 if (ret == INVALID_SET_FILE_POINTER)
139 {
140 SetErrnoFromWinError (GetLastError ());
141 return GNUNET_SYSERR;
142 }
143 return ret;
144#else
145 static int t[] = { [GNUNET_SEEK_SET] = SEEK_SET,
146 [GNUNET_SEEK_CUR] = SEEK_CUR, [GNUNET_SEEK_END] = SEEK_END };
147
148 return lseek (h->fd, offset, t[whence]);
149#endif
150}
151
111/** 152/**
112 * Get the size of the file (or directory) 153 * Get the size of the file (or directory)
113 * of the given file (in bytes). 154 * of the given file (in bytes).
@@ -129,6 +170,7 @@ GNUNET_DISK_file_size (const char *filename,
129 return ret; 170 return ret;
130} 171}
131 172
173
132/** 174/**
133 * Get the number of blocks that are left on the partition that 175 * Get the number of blocks that are left on the partition that
134 * contains the given file (for normal users). 176 * contains the given file (for normal users).
@@ -359,30 +401,55 @@ GNUNET_DISK_directory_create_for_file (const char *dir)
359 401
360/** 402/**
361 * Read the contents of a binary file into a buffer. 403 * Read the contents of a binary file into a buffer.
362 * @param fileName the name of the file, not freed, 404 * @param h handle to an open file
363 * must already be expanded! 405 * @param result the buffer to write the result to
364 * @param len the maximum number of bytes to read 406 * @param len the maximum number of bytes to read
407 * @return the number of bytes read on success, GNUNET_SYSERR on failure
408 */
409int
410GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len)
411{
412 if (h == NULL)
413 {
414 errno = EINVAL;
415 return GNUNET_SYSERR;
416 }
417
418#ifdef MINGW
419 DWORD bytesRead;
420
421 if (!ReadFile (h->h, result, len, &bytesRead, NULL))
422 {
423 SetErrnoFromWinError (GetLastError ());
424 return GNUNET_SYSERR;
425 }
426 return bytesRead;
427#else
428 return read (h->fd, result, len);
429#endif
430}
431
432
433/**
434 * Read the contents of a binary file into a buffer.
435 * @param fn file name
365 * @param result the buffer to write the result to 436 * @param result the buffer to write the result to
366 * @return the number of bytes read on success, -1 on failure 437 * @param len the maximum number of bytes to read
438 * @return the number of bytes read on success, GNUNET_SYSERR on failure
367 */ 439 */
368int 440int
369GNUNET_DISK_file_read (const char *fileName, int len, void *result) 441GNUNET_DISK_fn_read (const char * const fn, void *result, int len)
370{ 442{
371 /* open file, must exist, open read only */ 443 struct GNUNET_IO_Handle *fh;
372 int handle; 444 int ret;
373 int size; 445
374 446 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
375 GNUNET_assert (fileName != NULL); 447 if (!fh)
376 GNUNET_assert (len > 0); 448 return GNUNET_SYSERR;
377 if (len == 0) 449 ret = GNUNET_DISK_file_read (fh, result, len);
378 return 0; 450 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
379 GNUNET_assert (result != NULL); 451
380 handle = GNUNET_DISK_file_open (fileName, O_RDONLY, S_IRUSR); 452 return ret;
381 if (handle < 0)
382 return -1;
383 size = READ (handle, result, len);
384 GNUNET_DISK_file_close (fileName, handle);
385 return size;
386} 453}
387 454
388 455
@@ -404,45 +471,57 @@ atoo (const char *s)
404 471
405/** 472/**
406 * Write a buffer to a file. 473 * Write a buffer to a file.
407 * @param fileName the name of the file, NOT freed! 474 * @param h handle to open file
408 * @param buffer the data to write 475 * @param buffer the data to write
409 * @param n number of bytes to write 476 * @param n number of bytes to write
410 * @param mode permissions to set on the file 477 * @return number of bytes written on success, GNUNET_SYSERR on error
411 * @return GNUNET_OK on success, GNUNET_SYSERR on error
412 */ 478 */
413int 479int
414GNUNET_DISK_file_write (const char *fileName, 480GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer,
415 const void *buffer, unsigned int n, const char *mode) 481 unsigned int n)
416{ 482{
417 int handle; 483 if (h == NULL)
418 char *fn;
419
420 /* open file, open with 600, create if not
421 present, otherwise overwrite */
422 GNUNET_assert (fileName != NULL);
423 fn = GNUNET_STRINGS_filename_expand (fileName);
424 handle = GNUNET_DISK_file_open (fn, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
425 if (handle == -1)
426 { 484 {
427 GNUNET_free (fn); 485 errno = EINVAL;
428 return GNUNET_SYSERR; 486 return GNUNET_SYSERR;
429 } 487 }
430 GNUNET_assert ((n == 0) || (buffer != NULL)); 488
431 /* write the buffer take length from the beginning */ 489#ifdef MINGW
432 if (n != WRITE (handle, buffer, n)) 490 DWORD bytesWritten;
491
492 if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
433 { 493 {
434 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); 494 SetErrnoFromWinError (GetLastError ());
435 GNUNET_DISK_file_close (fn, handle);
436 GNUNET_free (fn);
437 return GNUNET_SYSERR; 495 return GNUNET_SYSERR;
438 } 496 }
439 GNUNET_DISK_file_close (fn, handle); 497 return bytesWritten;
440 if (0 != CHMOD (fn, atoo (mode))) 498#else
441 { 499 return write (h->fd, buffer, n);
442 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn); 500#endif
443 } 501}
444 GNUNET_free (fn); 502
445 return GNUNET_OK; 503/**
504 * Write a buffer to a file.
505 * @param fn file name
506 * @param buffer the data to write
507 * @param n number of bytes to write
508 * @return number of bytes written on success, GNUNET_SYSERR on error
509 */
510int
511GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
512 unsigned int n, int mode)
513{
514 struct GNUNET_IO_Handle *fh;
515 int ret;
516
517 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
518 | GNUNET_DISK_OPEN_CREATE, mode);
519 if (!fh)
520 return GNUNET_SYSERR;
521 ret = GNUNET_DISK_file_write (fh, buffer, n);
522 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
523
524 return ret;
446} 525}
447 526
448/** 527/**
@@ -724,57 +803,6 @@ GNUNET_DISK_directory_remove (const char *fileName)
724 return GNUNET_OK; 803 return GNUNET_OK;
725} 804}
726 805
727void
728GNUNET_DISK_file_close (const char *filename, int fd)
729{
730 if (0 != CLOSE (fd))
731 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", filename);
732}
733
734int
735GNUNET_DISK_file_open (const char *filename, int oflag, ...)
736{
737 char *fn;
738 int mode;
739 int ret;
740#ifdef MINGW
741 char szFile[_MAX_PATH + 1];
742 long lRet;
743
744 if ((lRet = plibc_conv_to_win_path (filename, szFile)) != ERROR_SUCCESS)
745 {
746 errno = ENOENT;
747 SetLastError (lRet);
748 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
749 "plibc_conv_to_win_path", filename);
750 return -1;
751 }
752 fn = GNUNET_strdup (szFile);
753#else
754 fn = GNUNET_STRINGS_filename_expand (filename);
755#endif
756 if (oflag & O_CREAT)
757 {
758 va_list arg;
759 va_start (arg, oflag);
760 mode = va_arg (arg, int);
761 va_end (arg);
762 }
763 else
764 {
765 mode = 0;
766 }
767#ifdef MINGW
768 /* set binary mode */
769 oflag |= O_BINARY;
770#endif
771 ret = OPEN (fn, oflag, mode);
772 if (ret == -1)
773 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
774 GNUNET_free (fn);
775 return ret;
776}
777
778#define COPY_BLK_SIZE 65536 806#define COPY_BLK_SIZE 65536
779 807
780/** 808/**
@@ -788,21 +816,21 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
788 unsigned long long pos; 816 unsigned long long pos;
789 unsigned long long size; 817 unsigned long long size;
790 unsigned long long len; 818 unsigned long long len;
791 int in; 819 struct GNUNET_IO_Handle *in, *out;
792 int out;
793 820
794 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES)) 821 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
795 return GNUNET_SYSERR; 822 return GNUNET_SYSERR;
796 pos = 0; 823 pos = 0;
797 in = GNUNET_DISK_file_open (src, O_RDONLY | O_LARGEFILE); 824 in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ);
798 if (in == -1) 825 if (!in)
799 return GNUNET_SYSERR; 826 return GNUNET_SYSERR;
800 out = GNUNET_DISK_file_open (dst, 827 out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
801 O_LARGEFILE | O_WRONLY | O_CREAT | O_EXCL, 828 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS,
802 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 829 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
803 if (out == -1) 830 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
831 if (!out)
804 { 832 {
805 GNUNET_DISK_file_close (src, in); 833 GNUNET_DISK_file_close (&in);
806 return GNUNET_SYSERR; 834 return GNUNET_SYSERR;
807 } 835 }
808 buf = GNUNET_malloc (COPY_BLK_SIZE); 836 buf = GNUNET_malloc (COPY_BLK_SIZE);
@@ -811,20 +839,20 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
811 len = COPY_BLK_SIZE; 839 len = COPY_BLK_SIZE;
812 if (len > size - pos) 840 if (len > size - pos)
813 len = size - pos; 841 len = size - pos;
814 if (len != READ (in, buf, len)) 842 if (len != GNUNET_DISK_file_read (in, buf, len))
815 goto FAIL; 843 goto FAIL;
816 if (len != WRITE (out, buf, len)) 844 if (len != GNUNET_DISK_file_write (out, buf, len))
817 goto FAIL; 845 goto FAIL;
818 pos += len; 846 pos += len;
819 } 847 }
820 GNUNET_free (buf); 848 GNUNET_free (buf);
821 GNUNET_DISK_file_close (src, in); 849 GNUNET_DISK_file_close (&in);
822 GNUNET_DISK_file_close (dst, out); 850 GNUNET_DISK_file_close (&out);
823 return GNUNET_OK; 851 return GNUNET_OK;
824FAIL: 852FAIL:
825 GNUNET_free (buf); 853 GNUNET_free (buf);
826 GNUNET_DISK_file_close (src, in); 854 GNUNET_DISK_file_close (&in);
827 GNUNET_DISK_file_close (dst, out); 855 GNUNET_DISK_file_close (&out);
828 return GNUNET_SYSERR; 856 return GNUNET_SYSERR;
829} 857}
830 858
@@ -881,6 +909,177 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
881 909
882 910
883/** 911/**
912 * Lock a part of a file
913 * @param fh file handle
914 * @lockStart absolute position from where to lock
915 * @lockEnd absolute position until where to lock
916 * @return GNUNET_OK on success, GNUNET_SYSERR on error
917 */
918int
919GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart,
920 off_t lockEnd)
921{
922 if (fh == NULL)
923 {
924 errno = EINVAL;
925 return GNUNET_SYSERR;
926 }
927
928#ifndef MINGW
929 struct flock fl;
930
931 memset(&fl, 0, sizeof(struct flock));
932 fl.l_type = F_WRLCK;
933 fl.l_whence = SEEK_SET;
934 fl.l_start = lockStart;
935 fl.l_len = lockEnd;
936
937 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
938#else
939 if (!LockFile(fh->h, 0, lockStart, 0, lockEnd))
940 {
941 SetErrnoFromWinError(GetLastError());
942 return GNUNET_SYSERR;
943 }
944
945 return GNUNET_OK;
946#endif
947}
948
949
950/**
951 * Open a file
952 * @param fn file name to be opened
953 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
954 * @param perm permissions for the newly created file
955 * @return IO handle on success, NULL on error
956 */
957struct GNUNET_IO_Handle *
958GNUNET_DISK_file_open (const char *fn, int flags, ...)
959{
960 char *expfn;
961 struct GNUNET_IO_Handle *ret;
962#ifdef MINGW
963 DWORD access, disp;
964 HANDLE h;
965#else
966 int oflags, mode;
967 int fd;
968#endif
969
970 expfn = GNUNET_STRINGS_filename_expand (fn);
971
972#ifndef MINGW
973 oflags = 0;
974 if (flags & GNUNET_DISK_OPEN_READ)
975 oflags = O_RDONLY;
976 if (flags & GNUNET_DISK_OPEN_WRITE)
977 oflags |= O_WRONLY;
978 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
979 oflags |= (O_CREAT & O_EXCL);
980 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
981 oflags |= O_TRUNC;
982 if (flags & GNUNET_DISK_OPEN_CREATE)
983 {
984 oflags |= O_CREAT;
985
986 va_list arg;
987 va_start (arg, oflag);
988 mode = va_arg (arg, int);
989 va_end (arg);
990 }
991 if (flags & GNUNET_DISK_OPEN_APPEND)
992 oflags = O_APPEND;
993
994 fd = open (expfn, oflag | O_LARGEFILE, perm, mode);
995 if (fd == -1)
996 {
997 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
998 return NULL;
999 }
1000#else
1001 access = 0;
1002 disp = OPEN_ALWAYS;
1003
1004 if (flags & GNUNET_DISK_OPEN_READ)
1005 access = FILE_READ_DATA;
1006 if (flags & GNUNET_DISK_OPEN_WRITE)
1007 access = FILE_WRITE_DATA;
1008
1009 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1010 disp = CREATE_NEW;
1011 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1012 disp = TRUNCATE_EXISTING;
1013 if (flags & GNUNET_DISK_OPEN_CREATE)
1014 disp |= OPEN_ALWAYS;
1015
1016 /* TODO: access priviledges? */
1017 h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
1018 | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
1019 if (h == INVALID_HANDLE_VALUE)
1020 {
1021 SetErrnoFromWinError (GetLastError ());
1022 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
1023 return NULL;
1024 }
1025
1026 if (flags & GNUNET_DISK_OPEN_APPEND)
1027 if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
1028 {
1029 SetErrnoFromWinError (GetLastError ());
1030 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", fn);
1031 CloseHandle (h);
1032 return NULL;
1033 }
1034#endif
1035
1036 ret = (struct GNUNET_IO_Handle *) GNUNET_malloc(sizeof(struct GNUNET_IO_Handle));
1037#ifdef MINGW
1038 ret->h = h;
1039#else
1040 ret->fd = fd;
1041#endif
1042
1043 return ret;
1044}
1045
1046/**
1047 * Close an open file
1048 * @param h file handle
1049 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1050 */
1051int
1052GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h)
1053{
1054 if (*h == NULL)
1055 {
1056 errno = EINVAL;
1057 return GNUNET_SYSERR;
1058 }
1059
1060#if MINGW
1061 if (!CloseHandle ((*h)->h))
1062 {
1063 SetErrnoFromWinError (GetLastError ());
1064 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1065 return GNUNET_SYSERR;
1066 }
1067#else
1068 if (close ((*h)->fd) != 0)
1069 {
1070 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
1071 return GNUNET_SYSERR;
1072 }
1073#endif
1074
1075 GNUNET_IO_handle_invalidate (*h);
1076 free(*h);
1077 *h = NULL;
1078
1079 return GNUNET_OK;
1080}
1081
1082/**
884 * Construct full path to a file inside of the private 1083 * Construct full path to a file inside of the private
885 * directory used by GNUnet. Also creates the corresponding 1084 * directory used by GNUnet. Also creates the corresponding
886 * directory. If the resulting name is supposed to be 1085 * directory. If the resulting name is supposed to be
@@ -949,6 +1148,145 @@ GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
949 return ret; 1148 return ret;
950} 1149}
951 1150
1151/**
1152 * Map a file into memory
1153 * @param h open file handle
1154 * @param m handle to the new mapping
1155 * @param access access specification, GNUNET_DISK_MAP_xxx
1156 * @param len size of the mapping
1157 * @return pointer to the mapped memory region, NULL on failure
1158 */
1159void *
1160GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m,
1161 int access, size_t len)
1162{
1163 if (h == NULL)
1164 {
1165 errno = EINVAL;
1166 return NULL;
1167 }
1168
1169#ifdef MINGW
1170 DWORD mapAccess, protect;
1171 void *ret;
1172
1173 if (access & GNUNET_DISK_MAP_READ && access & GNUNET_DISK_MAP_WRITE)
1174 {
1175 protect = PAGE_READWRITE;
1176 mapAccess = FILE_MAP_ALL_ACCESS;
1177 }
1178 else if (access & GNUNET_DISK_MAP_READ)
1179 {
1180 protect = PAGE_READONLY;
1181 mapAccess = FILE_MAP_READ;
1182 }
1183 else if (access & GNUNET_DISK_MAP_WRITE)
1184 {
1185 protect = PAGE_READWRITE;
1186 mapAccess = FILE_MAP_WRITE;
1187 }
1188 else
1189 {
1190 GNUNET_break (0);
1191 return NULL;
1192 }
1193
1194 *m = (struct GNUNET_IO_Handle *) GNUNET_malloc (sizeof (struct GNUNET_IO_Handle));
1195 (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
1196 if ((*m)->h == INVALID_HANDLE_VALUE)
1197 {
1198 SetErrnoFromWinError (GetLastError ());
1199 GNUNET_free (*m);
1200 return NULL;
1201 }
1202
1203 ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
1204 if (!ret)
1205 {
1206 SetErrnoFromWinError (GetLastError ());
1207 CloseHandle ((*m)->h);
1208 GNUNET_free (*m);
1209 }
1210
1211 return ret;
1212#else
1213 int prot;
1214
1215 prot = flags = 0;
1216 if (access & GNUNET_DISK_MAP_READ)
1217 prot = PROT_READ;
1218 if (access & GNUNET_DISK_MAP_WRITE)
1219 prot |= PROT_WRITE;
1220
1221 return mmap (NULL, len, prot, MAP_SHARED, h->h, 0);
1222#endif
1223}
1224
1225/**
1226 * Unmap a file
1227 * @param h mapping handle
1228 * @param addr pointer to the mapped memory region
1229 * @param len size of the mapping
1230 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1231 */
1232int
1233GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len)
1234{
1235#ifdef MINGW
1236 int ret;
1237
1238 if (h == NULL || *h == NULL)
1239 {
1240 errno = EINVAL;
1241 return GNUNET_SYSERR;
1242 }
1243
1244 ret = UnmapViewOfFile (addr) ? GNUNET_OK : GNUNET_SYSERR;
1245 if (ret != GNUNET_OK)
1246 SetErrnoFromWinError (GetLastError ());
1247 if (!CloseHandle ((*h)->h) && ret == GNUNET_OK)
1248 {
1249 ret = GNUNET_SYSERR;
1250 SetErrnoFromWinError (GetLastError ());
1251 }
1252
1253 GNUNET_IO_handle_invalidate (*h);
1254 GNUNET_free (*h);
1255 h = NULL;
1256
1257 return ret;
1258#else
1259 int ret;
1260 ret = munmap (addr, len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1261 GNUNET_IO_handle_invalidate (h);
1262 return ret;
1263#endif
1264}
1265
1266/**
1267 * Write file changes to disk
1268 * @param h handle to an open file
1269 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1270 */
1271int
1272GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h)
1273{
1274 if (h == NULL)
1275 {
1276 errno = EINVAL;
1277 return GNUNET_SYSERR;
1278 }
952 1279
1280#ifdef MINGW
1281 int ret;
1282
1283 ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
1284 if (ret != GNUNET_OK)
1285 SetErrnoFromWinError (GetLastError ());
1286 return ret;
1287#else
1288 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1289#endif
1290}
953 1291
954/* end of disk.c */ 1292/* end of disk.c */
diff --git a/src/util/io_handle.c b/src/util/io_handle.c
new file mode 100644
index 000000000..438974797
--- /dev/null
+++ b/src/util/io_handle.c
@@ -0,0 +1,58 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20/**
21 * @file util/io_handle.c
22 * @brief common functions for IO handles
23 * @author Nils Durner
24 */
25#include "platform.h"
26#include "io_handle.h"
27#include "gnunet_common.h"
28
29/**
30 * Checks whether a handle is invalid
31 * @param h handle to check
32 * @return GNUNET_YES if invalid, GNUNET_NO if valid
33 */
34int
35GNUNET_IO_handle_invalid (const struct GNUNET_IO_Handle *h)
36{
37#ifdef MINGW
38 return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
39#else
40 return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO;
41#endif
42}
43
44/**
45 * Mark a handle as invalid
46 * @param h file handle
47 */
48void
49GNUNET_IO_handle_invalidate (struct GNUNET_IO_Handle *h)
50{
51#ifdef MINGW
52 h->h = INVALID_HANDLE_VALUE;
53#else
54 h->fd = -1;
55#endif
56}
57
58/* end of io_handle.c */
diff --git a/src/util/io_handle.h b/src/util/io_handle.h
new file mode 100644
index 000000000..cafbdf1ee
--- /dev/null
+++ b/src/util/io_handle.h
@@ -0,0 +1,38 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file util/io_handle.c
23 * @brief wrapper structure for file handles, sockets, ...
24 */
25
26#ifndef IO_HANDLE_H_
27#define IO_HANDLE_H_
28
29struct GNUNET_IO_Handle
30{
31#if MINGW
32 HANDLE h;
33#else
34 int fd;
35#endif
36};
37
38#endif /* IO_HANDLE_H_ */
diff --git a/src/util/pseudonym.c b/src/util/pseudonym.c
index 6d9146613..f529d11f8 100644
--- a/src/util/pseudonym.c
+++ b/src/util/pseudonym.c
@@ -170,7 +170,8 @@ write_pseudonym_info (struct GNUNET_CONFIGURATION_Handle *cfg,
170 off + 1], 170 off + 1],
171 size, 171 size,
172 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL)); 172 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL));
173 GNUNET_DISK_file_write (fn, buf, tag, "660"); 173 GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
174 | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ);
174 GNUNET_free (fn); 175 GNUNET_free (fn);
175 GNUNET_free (buf); 176 GNUNET_free (buf);
176 /* create entry for pseudonym name in names */ 177 /* create entry for pseudonym name in names */
@@ -218,7 +219,7 @@ read_info (struct GNUNET_CONFIGURATION_Handle *cfg,
218 return GNUNET_SYSERR; 219 return GNUNET_SYSERR;
219 } 220 }
220 buf = GNUNET_malloc (len); 221 buf = GNUNET_malloc (len);
221 if (len != GNUNET_DISK_file_read (fn, len, buf)) 222 if (len != GNUNET_DISK_fn_read (fn, buf, len))
222 { 223 {
223 GNUNET_free (buf); 224 GNUNET_free (buf);
224 GNUNET_free (fn); 225 GNUNET_free (fn);
@@ -280,7 +281,7 @@ GNUNET_PSEUDONYM_id_to_name (struct GNUNET_CONFIGURATION_Handle *cfg,
280 GNUNET_HashCode nh; 281 GNUNET_HashCode nh;
281 char *fn; 282 char *fn;
282 unsigned long long len; 283 unsigned long long len;
283 int fd; 284 struct GNUNET_IO_Handle *fh;
284 unsigned int i; 285 unsigned int i;
285 unsigned int idx; 286 unsigned int idx;
286 char *ret; 287 char *ret;
@@ -317,12 +318,14 @@ GNUNET_PSEUDONYM_id_to_name (struct GNUNET_CONFIGURATION_Handle *cfg,
317 len = 0; 318 len = 0;
318 if (0 == STAT (fn, &sbuf)) 319 if (0 == STAT (fn, &sbuf))
319 GNUNET_DISK_file_size (fn, &len, GNUNET_YES); 320 GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
320 fd = GNUNET_DISK_file_open (fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); 321 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
322 | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
323 | GNUNET_DISK_PERM_USER_WRITE);
321 i = 0; 324 i = 0;
322 idx = -1; 325 idx = -1;
323 while ((len >= sizeof (GNUNET_HashCode)) && 326 while ((len >= sizeof (GNUNET_HashCode)) &&
324 (sizeof (GNUNET_HashCode) 327 (sizeof (GNUNET_HashCode)
325 == READ (fd, &nh, sizeof (GNUNET_HashCode)))) 328 == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
326 { 329 {
327 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode))) 330 if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
328 { 331 {
@@ -336,10 +339,10 @@ GNUNET_PSEUDONYM_id_to_name (struct GNUNET_CONFIGURATION_Handle *cfg,
336 { 339 {
337 idx = i; 340 idx = i;
338 if (sizeof (GNUNET_HashCode) != 341 if (sizeof (GNUNET_HashCode) !=
339 WRITE (fd, nsid, sizeof (GNUNET_HashCode))) 342 GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
340 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); 343 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
341 } 344 }
342 CLOSE (fd); 345 GNUNET_DISK_file_close (&fh);
343 ret = GNUNET_malloc (strlen (name) + 32); 346 ret = GNUNET_malloc (strlen (name) + 32);
344 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx); 347 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
345 GNUNET_free (name); 348 GNUNET_free (name);
@@ -362,7 +365,7 @@ GNUNET_PSEUDONYM_name_to_id (struct GNUNET_CONFIGURATION_Handle *cfg,
362 char *name; 365 char *name;
363 GNUNET_HashCode nh; 366 GNUNET_HashCode nh;
364 char *fn; 367 char *fn;
365 int fd; 368 struct GNUNET_IO_Handle *fh;
366 369
367 idx = -1; 370 idx = -1;
368 slen = strlen (ns_uname); 371 slen = strlen (ns_uname);
@@ -384,15 +387,17 @@ GNUNET_PSEUDONYM_name_to_id (struct GNUNET_CONFIGURATION_Handle *cfg,
384 GNUNET_free (fn); 387 GNUNET_free (fn);
385 return GNUNET_SYSERR; 388 return GNUNET_SYSERR;
386 } 389 }
387 fd = GNUNET_DISK_file_open (fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); 390 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
391 | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
392 | GNUNET_DISK_PERM_USER_WRITE);
388 GNUNET_free (fn); 393 GNUNET_free (fn);
389 LSEEK (fd, idx * sizeof (GNUNET_HashCode), SEEK_SET); 394 GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_SEEK_SET);
390 if (sizeof (GNUNET_HashCode) != READ (fd, nsid, sizeof (GNUNET_HashCode))) 395 if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
391 { 396 {
392 CLOSE (fd); 397 GNUNET_DISK_file_close (&fh);
393 return GNUNET_SYSERR; 398 return GNUNET_SYSERR;
394 } 399 }
395 CLOSE (fd); 400 GNUNET_DISK_file_close (&fh);
396 return GNUNET_OK; 401 return GNUNET_OK;
397} 402}
398 403
diff --git a/src/util/service.c b/src/util/service.c
index 732dbe9af..4264071a9 100644
--- a/src/util/service.c
+++ b/src/util/service.c
@@ -1154,7 +1154,7 @@ detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1154 GNUNET_break (0 == CLOSE (0)); 1154 GNUNET_break (0 == CLOSE (0));
1155 GNUNET_break (0 == CLOSE (1)); 1155 GNUNET_break (0 == CLOSE (1));
1156 GNUNET_break (0 == CLOSE (filedes[0])); 1156 GNUNET_break (0 == CLOSE (filedes[0]));
1157 nullfd = GNUNET_DISK_file_open ("/dev/null", O_RDWR | O_APPEND); 1157 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1158 if (nullfd < 0) 1158 if (nullfd < 0)
1159 return GNUNET_SYSERR; 1159 return GNUNET_SYSERR;
1160 /* set stdin/stdout to /dev/null */ 1160 /* set stdin/stdout to /dev/null */
diff --git a/src/util/test_disk.c b/src/util/test_disk.c
index 0d385afa7..e305889d8 100644
--- a/src/util/test_disk.c
+++ b/src/util/test_disk.c
@@ -36,13 +36,12 @@ testReadWrite ()
36 char tmp[100 + 1]; 36 char tmp[100 + 1];
37 int ret; 37 int ret;
38 38
39 if (GNUNET_OK != 39 if (GNUNET_OK != GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (
40 GNUNET_DISK_file_write (".testfile", TESTSTRING, strlen (TESTSTRING), 40 TESTSTRING), GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))
41 "644"))
42 return 1; 41 return 1;
43 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile")) 42 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
44 return 1; 43 return 1;
45 ret = GNUNET_DISK_file_read (".testfile", sizeof (tmp) - 1, tmp); 44 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
46 if (ret < 0) 45 if (ret < 0)
47 { 46 {
48 fprintf (stderr, 47 fprintf (stderr,
@@ -59,7 +58,7 @@ testReadWrite ()
59 } 58 }
60 GNUNET_DISK_file_copy (".testfile", ".testfile2"); 59 GNUNET_DISK_file_copy (".testfile", ".testfile2");
61 memset (tmp, 0, sizeof (tmp)); 60 memset (tmp, 0, sizeof (tmp));
62 ret = GNUNET_DISK_file_read (".testfile2", sizeof (tmp) - 1, tmp); 61 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
63 if (ret < 0) 62 if (ret < 0)
64 { 63 {
65 fprintf (stderr, 64 fprintf (stderr,
@@ -86,15 +85,16 @@ testReadWrite ()
86static int 85static int
87testOpenClose () 86testOpenClose ()
88{ 87{
89 int fd; 88 struct GNUNET_IO_Handle *fh;
90 unsigned long long size; 89 unsigned long long size;
91 long avail; 90 long avail;
92 91
93 fd = GNUNET_DISK_file_open (".testfile", 92 fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
94 O_RDWR | O_CREAT, S_IWUSR | S_IRUSR); 93 | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_READ
95 GNUNET_assert (-1 != fd); 94 | GNUNET_DISK_PERM_USER_WRITE);
96 GNUNET_break (5 == WRITE (fd, "Hello", 5)); 95 GNUNET_assert (GNUNET_NO == GNUNET_IO_handle_invalid (fh));
97 GNUNET_DISK_file_close (".testfile", fd); 96 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
97 GNUNET_DISK_file_close (&fh);
98 GNUNET_break (GNUNET_OK == 98 GNUNET_break (GNUNET_OK ==
99 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO)); 99 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
100 if (size != 5) 100 if (size != 5)
@@ -105,18 +105,19 @@ testOpenClose ()
105 GNUNET_log_skip (1); 105 GNUNET_log_skip (1);
106 avail = GNUNET_DISK_get_blocks_available (".testfile"); 106 avail = GNUNET_DISK_get_blocks_available (".testfile");
107 GNUNET_log_skip (0); 107 GNUNET_log_skip (0);
108 fd = GNUNET_DISK_file_open (".testfile", 108 fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
109 O_RDWR | O_CREAT, S_IWUSR | S_IRUSR); 109 | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_WRITE
110 GNUNET_assert (-1 != fd); 110 | GNUNET_DISK_PERM_USER_READ);
111 GNUNET_assert (GNUNET_NO == GNUNET_IO_handle_invalid (fh));
111 while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) && 112 while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
112 (avail != -1)) 113 (avail != -1))
113 if (16 != WRITE (fd, "HelloWorld123456", 16)) 114 if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
114 { 115 {
115 GNUNET_DISK_file_close (".testfile", fd); 116 GNUNET_DISK_file_close (&fh);
116 GNUNET_break (0 == UNLINK (".testfile")); 117 GNUNET_break (0 == UNLINK (".testfile"));
117 return 1; 118 return 1;
118 } 119 }
119 GNUNET_DISK_file_close (".testfile", fd); 120 GNUNET_DISK_file_close (&fh);
120 GNUNET_break (0 == UNLINK (".testfile")); 121 GNUNET_break (0 == UNLINK (".testfile"));
121 122
122 return 0; 123 return 0;
diff --git a/src/util/test_os_load.c b/src/util/test_os_load.c
index 2f82f60c7..8e9776883 100644
--- a/src/util/test_os_load.c
+++ b/src/util/test_os_load.c
@@ -99,7 +99,7 @@ static int
99testdisk () 99testdisk ()
100{ 100{
101 int ret; 101 int ret;
102 int fd; 102 struct GNUNET_IO_Handle *fh;
103 char buf[65536]; 103 char buf[65536];
104 struct GNUNET_TIME_Absolute start; 104 struct GNUNET_TIME_Absolute start;
105 struct GNUNET_CONFIGURATION_Handle *cfg; 105 struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -132,20 +132,20 @@ testdisk ()
132 return 0; 132 return 0;
133 } 133 }
134 memset (buf, 42, sizeof (buf)); 134 memset (buf, 42, sizeof (buf));
135 fd = 135 fh = GNUNET_DISK_file_open (".loadfile", GNUNET_DISK_OPEN_WRITE
136 GNUNET_DISK_file_open (".loadfile", O_WRONLY | O_CREAT, 136 | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_READ
137 S_IRUSR | S_IWUSR); 137 | GNUNET_DISK_PERM_USER_WRITE);
138 GNUNET_assert (fd != -1); 138 GNUNET_assert (GNUNET_NO == GNUNET_IO_handle_invalid(fh));
139 while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000) 139 while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
140 { 140 {
141 LSEEK (fd, GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 141 GNUNET_DISK_file_seek (fh, GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
142 1024 * 1024 * 1024), SEEK_SET); 142 1024 * 1024 * 1024), GNUNET_SEEK_SET);
143 GNUNET_assert (sizeof (buf) == WRITE (fd, buf, sizeof (buf))); 143 GNUNET_assert (sizeof (buf) == GNUNET_DISK_file_write (fh, buf, sizeof (buf)));
144 fsync (fd); 144 GNUNET_DISK_file_sync (fh);
145 if (ret < GNUNET_OS_load_disk_get (cfg)) 145 if (ret < GNUNET_OS_load_disk_get (cfg))
146 break; 146 break;
147 } 147 }
148 GNUNET_break (0 == CLOSE (fd)); 148 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh));
149 GNUNET_break (0 == UNLINK (".loadfile")); 149 GNUNET_break (0 == UNLINK (".loadfile"));
150 if (ret >= GNUNET_OS_load_disk_get (cfg)) 150 if (ret >= GNUNET_OS_load_disk_get (cfg))
151 { 151 {