diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-07-07 20:05:38 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-07-07 20:05:38 +0000 |
commit | ffb59a6a7fca6e70abfe5b87a997ed9cad989ec5 (patch) | |
tree | 58792ca0ea4ca6c461d0ffea8634b7dcf9f3fc90 /src/util/disk.c | |
parent | 8f0b281a4ec9c9419fb10892b6ffbd12d4bea1a0 (diff) | |
download | gnunet-ffb59a6a7fca6e70abfe5b87a997ed9cad989ec5.tar.gz gnunet-ffb59a6a7fca6e70abfe5b87a997ed9cad989ec5.zip |
cleaning up disk api
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 136 |
1 files changed, 67 insertions, 69 deletions
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 | ||
77 | struct GNUNET_IO_Handle | 77 | struct 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 | */ |
126 | int | 126 | int |
127 | GNUNET_DISK_handle_invalid (const struct GNUNET_IO_Handle *h) | 127 | GNUNET_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 | */ | ||
140 | static void | ||
141 | GNUNET_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 | */ |
157 | off_t | 144 | off_t |
158 | GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset, | 145 | GNUNET_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 | */ |
444 | int | 431 | ssize_t |
445 | GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len) | 432 | GNUNET_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 | */ |
475 | int | 464 | ssize_t |
476 | GNUNET_DISK_fn_read (const char * const fn, void *result, int len) | 465 | GNUNET_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 | */ |
498 | int | 489 | ssize_t |
499 | GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer, | 490 | GNUNET_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 | */ |
529 | int | 522 | ssize_t |
530 | GNUNET_DISK_fn_write (const char * const fn, const void *buffer, | 523 | GNUNET_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; |
873 | FAIL: | 866 | FAIL: |
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 | */ |
939 | int | 932 | int |
940 | GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart, | 933 | GNUNET_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 | */ |
978 | struct GNUNET_IO_Handle * | 971 | struct GNUNET_DISK_FileHandle * |
979 | GNUNET_DISK_file_open (const char *fn, int flags, ...) | 972 | GNUNET_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 | */ |
1103 | int | 1096 | int |
1104 | GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h) | 1097 | GNUNET_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 | ||
1194 | struct 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 | */ |
1211 | void * | 1213 | void * |
1212 | GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m, | 1214 | GNUNET_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 | */ |
1284 | int | 1286 | int |
1285 | GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len) | 1287 | GNUNET_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 | */ |
1320 | int | 1318 | int |
1321 | GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h) | 1319 | GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) |
1322 | { | 1320 | { |
1323 | if (h == NULL) | 1321 | if (h == NULL) |
1324 | { | 1322 | { |