diff options
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 1364 |
1 files changed, 688 insertions, 676 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index 31b610fe8..dd7db7d73 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -109,7 +109,8 @@ struct GetFileSizeData | |||
109 | }; | 109 | }; |
110 | 110 | ||
111 | 111 | ||
112 | int translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm) | 112 | int |
113 | translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm) | ||
113 | { | 114 | { |
114 | int mode; | 115 | int mode; |
115 | 116 | ||
@@ -149,6 +150,7 @@ static int | |||
149 | getSizeRec (void *cls, const char *fn) | 150 | getSizeRec (void *cls, const char *fn) |
150 | { | 151 | { |
151 | struct GetFileSizeData *gfsd = cls; | 152 | struct GetFileSizeData *gfsd = cls; |
153 | |||
152 | #ifdef HAVE_STAT64 | 154 | #ifdef HAVE_STAT64 |
153 | struct stat64 buf; | 155 | struct stat64 buf; |
154 | #else | 156 | #else |
@@ -157,26 +159,26 @@ getSizeRec (void *cls, const char *fn) | |||
157 | 159 | ||
158 | #ifdef HAVE_STAT64 | 160 | #ifdef HAVE_STAT64 |
159 | if (0 != STAT64 (fn, &buf)) | 161 | if (0 != STAT64 (fn, &buf)) |
160 | { | 162 | { |
161 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn); | 163 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat64", fn); |
162 | return GNUNET_SYSERR; | 164 | return GNUNET_SYSERR; |
163 | } | 165 | } |
164 | #else | 166 | #else |
165 | if (0 != STAT (fn, &buf)) | 167 | if (0 != STAT (fn, &buf)) |
166 | { | 168 | { |
167 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn); | 169 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fn); |
168 | return GNUNET_SYSERR; | 170 | return GNUNET_SYSERR; |
169 | } | 171 | } |
170 | #endif | 172 | #endif |
171 | if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) | 173 | if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) |
172 | gfsd->total += buf.st_size; | 174 | gfsd->total += buf.st_size; |
173 | if ((S_ISDIR (buf.st_mode)) && | 175 | if ((S_ISDIR (buf.st_mode)) && |
174 | (0 == ACCESS (fn, X_OK)) && | 176 | (0 == ACCESS (fn, X_OK)) && |
175 | ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) | 177 | ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) |
176 | { | 178 | { |
177 | if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) | 179 | if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) |
178 | return GNUNET_SYSERR; | 180 | return GNUNET_SYSERR; |
179 | } | 181 | } |
180 | return GNUNET_OK; | 182 | return GNUNET_OK; |
181 | } | 183 | } |
182 | 184 | ||
@@ -211,23 +213,24 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset, | |||
211 | enum GNUNET_DISK_Seek whence) | 213 | enum GNUNET_DISK_Seek whence) |
212 | { | 214 | { |
213 | if (h == NULL) | 215 | if (h == NULL) |
214 | { | 216 | { |
215 | errno = EINVAL; | 217 | errno = EINVAL; |
216 | return GNUNET_SYSERR; | 218 | return GNUNET_SYSERR; |
217 | } | 219 | } |
218 | 220 | ||
219 | #ifdef MINGW | 221 | #ifdef MINGW |
220 | DWORD ret; | 222 | DWORD ret; |
223 | |||
221 | static DWORD t[] = {[GNUNET_DISK_SEEK_SET] = FILE_BEGIN, | 224 | static DWORD t[] = {[GNUNET_DISK_SEEK_SET] = FILE_BEGIN, |
222 | [GNUNET_DISK_SEEK_CUR] = FILE_CURRENT,[GNUNET_DISK_SEEK_END] = FILE_END | 225 | [GNUNET_DISK_SEEK_CUR] = FILE_CURRENT,[GNUNET_DISK_SEEK_END] = FILE_END |
223 | }; | 226 | }; |
224 | 227 | ||
225 | ret = SetFilePointer (h->h, offset, NULL, t[whence]); | 228 | ret = SetFilePointer (h->h, offset, NULL, t[whence]); |
226 | if (ret == INVALID_SET_FILE_POINTER) | 229 | if (ret == INVALID_SET_FILE_POINTER) |
227 | { | 230 | { |
228 | SetErrnoFromWinError (GetLastError ()); | 231 | SetErrnoFromWinError (GetLastError ()); |
229 | return GNUNET_SYSERR; | 232 | return GNUNET_SYSERR; |
230 | } | 233 | } |
231 | return ret; | 234 | return ret; |
232 | #else | 235 | #else |
233 | static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET, | 236 | static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET, |
@@ -291,39 +294,39 @@ GNUNET_DISK_file_get_identifiers (const char *filename, | |||
291 | struct statvfs fbuf; | 294 | struct statvfs fbuf; |
292 | 295 | ||
293 | if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf))) | 296 | if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf))) |
294 | { | 297 | { |
295 | *dev = (uint64_t) fbuf.f_fsid; | 298 | *dev = (uint64_t) fbuf.f_fsid; |
296 | *ino = (uint64_t) sbuf.st_ino; | 299 | *ino = (uint64_t) sbuf.st_ino; |
297 | return GNUNET_OK; | 300 | return GNUNET_OK; |
298 | } | 301 | } |
299 | #elif SOMEBSD | 302 | #elif SOMEBSD |
300 | struct stat sbuf; | 303 | struct stat sbuf; |
301 | struct statfs fbuf; | 304 | struct statfs fbuf; |
302 | 305 | ||
303 | if ( (0 == stat (filename, &sbuf)) && | 306 | if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf))) |
304 | (0 == statfs (filename, &fbuf) ) ) | 307 | { |
305 | { | 308 | *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || |
306 | *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]); | 309 | ((uint64_t) fbuf.f_fsid.val[1]); |
307 | *ino = (uint64_t) sbuf.st_ino; | 310 | *ino = (uint64_t) sbuf.st_ino; |
308 | return GNUNET_OK; | 311 | return GNUNET_OK; |
309 | } | 312 | } |
310 | #elif WINDOWS | 313 | #elif WINDOWS |
311 | // FIXME NILS: test this | 314 | // FIXME NILS: test this |
312 | struct GNUNET_DISK_FileHandle *fh; | 315 | struct GNUNET_DISK_FileHandle *fh; |
313 | BY_HANDLE_FILE_INFORMATION info; | 316 | BY_HANDLE_FILE_INFORMATION info; |
314 | int succ; | 317 | int succ; |
315 | 318 | ||
316 | fh = GNUNET_DISK_file_open(filename, GNUNET_DISK_OPEN_READ, 0); | 319 | fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 0); |
317 | if (fh == NULL) | 320 | if (fh == NULL) |
318 | return GNUNET_SYSERR; | 321 | return GNUNET_SYSERR; |
319 | succ = GetFileInformationByHandle(fh->h, &info); | 322 | succ = GetFileInformationByHandle (fh->h, &info); |
320 | GNUNET_DISK_file_close(fh); | 323 | GNUNET_DISK_file_close (fh); |
321 | if (succ) | 324 | if (succ) |
322 | { | 325 | { |
323 | *dev = info.dwVolumeSerialNumber; | 326 | *dev = info.dwVolumeSerialNumber; |
324 | *ino = ((info.nFileIndexHigh << sizeof(DWORD)) | info.nFileIndexLow); | 327 | *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow); |
325 | return GNUNET_OK; | 328 | return GNUNET_OK; |
326 | } | 329 | } |
327 | else | 330 | else |
328 | return GNUNET_SYSERR; | 331 | return GNUNET_SYSERR; |
329 | 332 | ||
@@ -351,40 +354,39 @@ GNUNET_DISK_mktemp (const char *t) | |||
351 | char *tmpl; | 354 | char *tmpl; |
352 | char *fn; | 355 | char *fn; |
353 | 356 | ||
354 | if ( (t[0] != '/') && | 357 | if ((t[0] != '/') && (t[0] != '\\') |
355 | (t[0] != '\\') | ||
356 | #if WINDOWS | 358 | #if WINDOWS |
357 | && ! (isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) | 359 | && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) |
358 | #endif | 360 | #endif |
359 | ) | 361 | ) |
360 | { | 362 | { |
361 | tmpdir = getenv ("TMPDIR"); | 363 | tmpdir = getenv ("TMPDIR"); |
362 | tmpdir = tmpdir ? tmpdir : "/tmp"; | 364 | tmpdir = tmpdir ? tmpdir : "/tmp"; |
363 | GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); | 365 | GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); |
364 | } | 366 | } |
365 | else | 367 | else |
366 | { | 368 | { |
367 | GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); | 369 | GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); |
368 | } | 370 | } |
369 | #ifdef MINGW | 371 | #ifdef MINGW |
370 | fn = (char *) GNUNET_malloc (MAX_PATH + 1); | 372 | fn = (char *) GNUNET_malloc (MAX_PATH + 1); |
371 | if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn)) | 373 | if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn)) |
372 | { | 374 | { |
373 | GNUNET_free (fn); | 375 | GNUNET_free (fn); |
374 | GNUNET_free (tmpl); | 376 | GNUNET_free (tmpl); |
375 | return NULL; | 377 | return NULL; |
376 | } | 378 | } |
377 | GNUNET_free (tmpl); | 379 | GNUNET_free (tmpl); |
378 | #else | 380 | #else |
379 | fn = tmpl; | 381 | fn = tmpl; |
380 | #endif | 382 | #endif |
381 | fd = mkstemp (fn); | 383 | fd = mkstemp (fn); |
382 | if (fd == -1) | 384 | if (fd == -1) |
383 | { | 385 | { |
384 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); | 386 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); |
385 | GNUNET_free (fn); | 387 | GNUNET_free (fn); |
386 | return NULL; | 388 | return NULL; |
387 | } | 389 | } |
388 | if (0 != CLOSE (fd)) | 390 | if (0 != CLOSE (fd)) |
389 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn); | 391 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn); |
390 | return fn; | 392 | return fn; |
@@ -405,10 +407,10 @@ GNUNET_DISK_get_blocks_available (const char *part) | |||
405 | struct statvfs buf; | 407 | struct statvfs buf; |
406 | 408 | ||
407 | if (0 != statvfs (part, &buf)) | 409 | if (0 != statvfs (part, &buf)) |
408 | { | 410 | { |
409 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); | 411 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); |
410 | return -1; | 412 | return -1; |
411 | } | 413 | } |
412 | return buf.f_bavail; | 414 | return buf.f_bavail; |
413 | #elif MINGW | 415 | #elif MINGW |
414 | DWORD dwDummy; | 416 | DWORD dwDummy; |
@@ -423,21 +425,22 @@ GNUNET_DISK_get_blocks_available (const char *part) | |||
423 | GNUNET_free (path); | 425 | GNUNET_free (path); |
424 | szDrive[3] = 0; | 426 | szDrive[3] = 0; |
425 | if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy)) | 427 | if (!GetDiskFreeSpace (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy)) |
426 | { | 428 | { |
427 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 429 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
428 | _("`%s' failed for drive `%s': %u\n"), | 430 | _("`%s' failed for drive `%s': %u\n"), |
429 | "GetDiskFreeSpace", szDrive, GetLastError ()); | 431 | "GetDiskFreeSpace", szDrive, GetLastError ()); |
430 | 432 | ||
431 | return -1; | 433 | return -1; |
432 | } | 434 | } |
433 | return dwBlocks; | 435 | return dwBlocks; |
434 | #else | 436 | #else |
435 | struct statfs s; | 437 | struct statfs s; |
438 | |||
436 | if (0 != statfs (part, &s)) | 439 | if (0 != statfs (part, &s)) |
437 | { | 440 | { |
438 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); | 441 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "statfs", part); |
439 | return -1; | 442 | return -1; |
440 | } | 443 | } |
441 | return s.f_bavail; | 444 | return s.f_bavail; |
442 | #endif | 445 | #endif |
443 | } | 446 | } |
@@ -461,21 +464,21 @@ GNUNET_DISK_directory_test (const char *fil) | |||
461 | 464 | ||
462 | ret = STAT (fil, &filestat); | 465 | ret = STAT (fil, &filestat); |
463 | if (ret != 0) | 466 | if (ret != 0) |
467 | { | ||
468 | if (errno != ENOENT) | ||
464 | { | 469 | { |
465 | if (errno != ENOENT) | 470 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil); |
466 | { | 471 | return GNUNET_SYSERR; |
467 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", fil); | ||
468 | return GNUNET_SYSERR; | ||
469 | } | ||
470 | return GNUNET_NO; | ||
471 | } | 472 | } |
473 | return GNUNET_NO; | ||
474 | } | ||
472 | if (!S_ISDIR (filestat.st_mode)) | 475 | if (!S_ISDIR (filestat.st_mode)) |
473 | return GNUNET_NO; | 476 | return GNUNET_NO; |
474 | if (ACCESS (fil, R_OK | X_OK) < 0) | 477 | if (ACCESS (fil, R_OK | X_OK) < 0) |
475 | { | 478 | { |
476 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil); | 479 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", fil); |
477 | return GNUNET_SYSERR; | 480 | return GNUNET_SYSERR; |
478 | } | 481 | } |
479 | return GNUNET_YES; | 482 | return GNUNET_YES; |
480 | } | 483 | } |
481 | 484 | ||
@@ -500,27 +503,27 @@ GNUNET_DISK_file_test (const char *fil) | |||
500 | 503 | ||
501 | ret = STAT (rdir, &filestat); | 504 | ret = STAT (rdir, &filestat); |
502 | if (ret != 0) | 505 | if (ret != 0) |
506 | { | ||
507 | if (errno != ENOENT) | ||
503 | { | 508 | { |
504 | if (errno != ENOENT) | 509 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); |
505 | { | ||
506 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); | ||
507 | GNUNET_free (rdir); | ||
508 | return GNUNET_SYSERR; | ||
509 | } | ||
510 | GNUNET_free (rdir); | 510 | GNUNET_free (rdir); |
511 | return GNUNET_NO; | 511 | return GNUNET_SYSERR; |
512 | } | 512 | } |
513 | GNUNET_free (rdir); | ||
514 | return GNUNET_NO; | ||
515 | } | ||
513 | if (!S_ISREG (filestat.st_mode)) | 516 | if (!S_ISREG (filestat.st_mode)) |
514 | { | 517 | { |
515 | GNUNET_free (rdir); | 518 | GNUNET_free (rdir); |
516 | return GNUNET_NO; | 519 | return GNUNET_NO; |
517 | } | 520 | } |
518 | if (ACCESS (rdir, R_OK) < 0) | 521 | if (ACCESS (rdir, R_OK) < 0) |
519 | { | 522 | { |
520 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir); | 523 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "access", rdir); |
521 | GNUNET_free (rdir); | 524 | GNUNET_free (rdir); |
522 | return GNUNET_SYSERR; | 525 | return GNUNET_SYSERR; |
523 | } | 526 | } |
524 | GNUNET_free (rdir); | 527 | GNUNET_free (rdir); |
525 | return GNUNET_YES; | 528 | return GNUNET_YES; |
526 | } | 529 | } |
@@ -549,53 +552,52 @@ GNUNET_DISK_directory_create (const char *dir) | |||
549 | #else | 552 | #else |
550 | /* Local or Network path? */ | 553 | /* Local or Network path? */ |
551 | if (strncmp (rdir, "\\\\", 2) == 0) | 554 | if (strncmp (rdir, "\\\\", 2) == 0) |
555 | { | ||
556 | pos = 2; | ||
557 | while (rdir[pos]) | ||
552 | { | 558 | { |
553 | pos = 2; | 559 | if (rdir[pos] == '\\') |
554 | while (rdir[pos]) | 560 | { |
555 | { | 561 | pos++; |
556 | if (rdir[pos] == '\\') | 562 | break; |
557 | { | 563 | } |
558 | pos++; | 564 | pos++; |
559 | break; | ||
560 | } | ||
561 | pos++; | ||
562 | } | ||
563 | } | 565 | } |
566 | } | ||
564 | else | 567 | else |
565 | { | 568 | { |
566 | pos = 3; /* strlen("C:\\") */ | 569 | pos = 3; /* strlen("C:\\") */ |
567 | } | 570 | } |
568 | #endif | 571 | #endif |
569 | while (pos <= len) | 572 | while (pos <= len) |
573 | { | ||
574 | if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) | ||
570 | { | 575 | { |
571 | if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) | 576 | rdir[pos] = '\0'; |
572 | { | 577 | ret = GNUNET_DISK_directory_test (rdir); |
573 | rdir[pos] = '\0'; | 578 | if (ret == GNUNET_SYSERR) |
574 | ret = GNUNET_DISK_directory_test (rdir); | 579 | { |
575 | if (ret == GNUNET_SYSERR) | 580 | GNUNET_free (rdir); |
576 | { | 581 | return GNUNET_SYSERR; |
577 | GNUNET_free (rdir); | 582 | } |
578 | return GNUNET_SYSERR; | 583 | if (ret == GNUNET_NO) |
579 | } | 584 | { |
580 | if (ret == GNUNET_NO) | ||
581 | { | ||
582 | #ifndef MINGW | 585 | #ifndef MINGW |
583 | ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ | 586 | ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ |
584 | #else | 587 | #else |
585 | ret = mkdir (rdir); | 588 | ret = mkdir (rdir); |
586 | #endif | 589 | #endif |
587 | if ((ret != 0) && (errno != EEXIST)) | 590 | if ((ret != 0) && (errno != EEXIST)) |
588 | { | 591 | { |
589 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", | 592 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); |
590 | rdir); | 593 | GNUNET_free (rdir); |
591 | GNUNET_free (rdir); | 594 | return GNUNET_SYSERR; |
592 | return GNUNET_SYSERR; | ||
593 | } | ||
594 | } | ||
595 | rdir[pos] = DIR_SEPARATOR; | ||
596 | } | 595 | } |
597 | pos++; | 596 | } |
597 | rdir[pos] = DIR_SEPARATOR; | ||
598 | } | 598 | } |
599 | pos++; | ||
600 | } | ||
599 | GNUNET_free (rdir); | 601 | GNUNET_free (rdir); |
600 | return GNUNET_OK; | 602 | return GNUNET_OK; |
601 | } | 603 | } |
@@ -645,15 +647,15 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, | |||
645 | size_t len) | 647 | size_t len) |
646 | { | 648 | { |
647 | if (h == NULL) | 649 | if (h == NULL) |
648 | { | 650 | { |
649 | errno = EINVAL; | 651 | errno = EINVAL; |
650 | return GNUNET_SYSERR; | 652 | return GNUNET_SYSERR; |
651 | } | 653 | } |
652 | 654 | ||
653 | #ifdef MINGW | 655 | #ifdef MINGW |
654 | DWORD bytesRead; | 656 | DWORD bytesRead; |
655 | 657 | ||
656 | if(h->type != GNUNET_PIPE) | 658 | if (h->type != GNUNET_PIPE) |
657 | { | 659 | { |
658 | if (!ReadFile (h->h, result, len, &bytesRead, NULL)) | 660 | if (!ReadFile (h->h, result, len, &bytesRead, NULL)) |
659 | { | 661 | { |
@@ -665,13 +667,13 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, | |||
665 | { | 667 | { |
666 | if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) | 668 | if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) |
667 | { | 669 | { |
668 | if(GetLastError () != ERROR_IO_PENDING ) | 670 | if (GetLastError () != ERROR_IO_PENDING) |
669 | { | 671 | { |
670 | SetErrnoFromWinError (GetLastError ()); | 672 | SetErrnoFromWinError (GetLastError ()); |
671 | return GNUNET_SYSERR; | 673 | return GNUNET_SYSERR; |
672 | } | 674 | } |
673 | } | 675 | } |
674 | GetOverlappedResult(h->h, h->oOverlapRead, &bytesRead, TRUE); | 676 | GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); |
675 | } | 677 | } |
676 | return bytesRead; | 678 | return bytesRead; |
677 | #else | 679 | #else |
@@ -694,8 +696,7 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len) | |||
694 | struct GNUNET_DISK_FileHandle *fh; | 696 | struct GNUNET_DISK_FileHandle *fh; |
695 | ssize_t ret; | 697 | ssize_t ret; |
696 | 698 | ||
697 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, | 699 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); |
698 | GNUNET_DISK_PERM_NONE); | ||
699 | if (!fh) | 700 | if (!fh) |
700 | return GNUNET_SYSERR; | 701 | return GNUNET_SYSERR; |
701 | ret = GNUNET_DISK_file_read (fh, result, len); | 702 | ret = GNUNET_DISK_file_read (fh, result, len); |
@@ -717,15 +718,15 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | |||
717 | const void *buffer, size_t n) | 718 | const void *buffer, size_t n) |
718 | { | 719 | { |
719 | if (h == NULL) | 720 | if (h == NULL) |
720 | { | 721 | { |
721 | errno = EINVAL; | 722 | errno = EINVAL; |
722 | return GNUNET_SYSERR; | 723 | return GNUNET_SYSERR; |
723 | } | 724 | } |
724 | 725 | ||
725 | #ifdef MINGW | 726 | #ifdef MINGW |
726 | DWORD bytesWritten; | 727 | DWORD bytesWritten; |
727 | 728 | ||
728 | if(h->type != GNUNET_PIPE) | 729 | if (h->type != GNUNET_PIPE) |
729 | { | 730 | { |
730 | if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) | 731 | if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL)) |
731 | { | 732 | { |
@@ -740,19 +741,19 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | |||
740 | #endif | 741 | #endif |
741 | if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) | 742 | if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) |
742 | { | 743 | { |
743 | if(GetLastError () != ERROR_IO_PENDING ) | 744 | if (GetLastError () != ERROR_IO_PENDING) |
744 | { | 745 | { |
745 | SetErrnoFromWinError (GetLastError ()); | 746 | SetErrnoFromWinError (GetLastError ()); |
746 | #if DEBUG_PIPE | 747 | #if DEBUG_PIPE |
747 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); | 748 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); |
748 | #endif | 749 | #endif |
749 | return GNUNET_SYSERR; | 750 | return GNUNET_SYSERR; |
750 | } | 751 | } |
751 | } | 752 | } |
752 | #if DEBUG_PIPE | 753 | #if DEBUG_PIPE |
753 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); | 754 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); |
754 | #endif | 755 | #endif |
755 | GetOverlappedResult(h->h, h->oOverlapWrite, &bytesWritten, TRUE); | 756 | GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE); |
756 | } | 757 | } |
757 | return bytesWritten; | 758 | return bytesWritten; |
758 | #else | 759 | #else |
@@ -819,64 +820,64 @@ GNUNET_DISK_directory_scan (const char *dirName, | |||
819 | while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) | 820 | while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) |
820 | dname[strlen (dname) - 1] = '\0'; | 821 | dname[strlen (dname) - 1] = '\0'; |
821 | if (0 != STAT (dname, &istat)) | 822 | if (0 != STAT (dname, &istat)) |
822 | { | 823 | { |
823 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname); | 824 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", dname); |
824 | GNUNET_free (dname); | 825 | GNUNET_free (dname); |
825 | return GNUNET_SYSERR; | 826 | return GNUNET_SYSERR; |
826 | } | 827 | } |
827 | if (!S_ISDIR (istat.st_mode)) | 828 | if (!S_ISDIR (istat.st_mode)) |
828 | { | 829 | { |
829 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 830 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
830 | _("Expected `%s' to be a directory!\n"), dirName); | 831 | _("Expected `%s' to be a directory!\n"), dirName); |
831 | GNUNET_free (dname); | 832 | GNUNET_free (dname); |
832 | return GNUNET_SYSERR; | 833 | return GNUNET_SYSERR; |
833 | } | 834 | } |
834 | errno = 0; | 835 | errno = 0; |
835 | dinfo = OPENDIR (dname); | 836 | dinfo = OPENDIR (dname); |
836 | if ((errno == EACCES) || (dinfo == NULL)) | 837 | if ((errno == EACCES) || (dinfo == NULL)) |
837 | { | 838 | { |
838 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); | 839 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); |
839 | if (dinfo != NULL) | 840 | if (dinfo != NULL) |
840 | closedir (dinfo); | 841 | closedir (dinfo); |
841 | GNUNET_free (dname); | 842 | GNUNET_free (dname); |
842 | return GNUNET_SYSERR; | 843 | return GNUNET_SYSERR; |
843 | } | 844 | } |
844 | name_len = 256; | 845 | name_len = 256; |
845 | n_size = strlen (dname) + name_len + 2; | 846 | n_size = strlen (dname) + name_len + 2; |
846 | name = GNUNET_malloc (n_size); | 847 | name = GNUNET_malloc (n_size); |
847 | while ((finfo = readdir (dinfo)) != NULL) | 848 | while ((finfo = readdir (dinfo)) != NULL) |
849 | { | ||
850 | if ((0 == strcmp (finfo->d_name, ".")) || | ||
851 | (0 == strcmp (finfo->d_name, ".."))) | ||
852 | continue; | ||
853 | if (callback != NULL) | ||
848 | { | 854 | { |
849 | if ((0 == strcmp (finfo->d_name, ".")) || | 855 | if (name_len < strlen (finfo->d_name)) |
850 | (0 == strcmp (finfo->d_name, ".."))) | 856 | { |
851 | continue; | 857 | GNUNET_free (name); |
852 | if (callback != NULL) | 858 | name_len = strlen (finfo->d_name); |
853 | { | 859 | n_size = strlen (dname) + name_len + 2; |
854 | if (name_len < strlen (finfo->d_name)) | 860 | name = GNUNET_malloc (n_size); |
855 | { | 861 | } |
856 | GNUNET_free (name); | 862 | /* dname can end in "/" only if dname == "/"; |
857 | name_len = strlen (finfo->d_name); | 863 | * if dname does not end in "/", we need to add |
858 | n_size = strlen (dname) + name_len + 2; | 864 | * a "/" (otherwise, we must not!) */ |
859 | name = GNUNET_malloc (n_size); | 865 | GNUNET_snprintf (name, |
860 | } | 866 | n_size, |
861 | /* dname can end in "/" only if dname == "/"; | 867 | "%s%s%s", |
862 | if dname does not end in "/", we need to add | 868 | dname, |
863 | a "/" (otherwise, we must not!) */ | 869 | (strcmp (dname, DIR_SEPARATOR_STR) == |
864 | GNUNET_snprintf (name, | 870 | 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); |
865 | n_size, | 871 | if (GNUNET_OK != callback (callback_cls, name)) |
866 | "%s%s%s", | 872 | { |
867 | dname, | 873 | closedir (dinfo); |
868 | (strcmp (dname, DIR_SEPARATOR_STR) == | 874 | GNUNET_free (name); |
869 | 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); | 875 | GNUNET_free (dname); |
870 | if (GNUNET_OK != callback (callback_cls, name)) | 876 | return GNUNET_SYSERR; |
871 | { | 877 | } |
872 | closedir (dinfo); | ||
873 | GNUNET_free (name); | ||
874 | GNUNET_free (dname); | ||
875 | return GNUNET_SYSERR; | ||
876 | } | ||
877 | } | ||
878 | count++; | ||
879 | } | 878 | } |
879 | count++; | ||
880 | } | ||
880 | closedir (dinfo); | 881 | closedir (dinfo); |
881 | GNUNET_free (name); | 882 | GNUNET_free (name); |
882 | GNUNET_free (dname); | 883 | GNUNET_free (dname); |
@@ -960,27 +961,26 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator | |||
960 | 961 | ||
961 | GNUNET_assert (iter->next_name == NULL); | 962 | GNUNET_assert (iter->next_name == NULL); |
962 | if (can == GNUNET_YES) | 963 | if (can == GNUNET_YES) |
963 | { | 964 | { |
964 | closedir (iter->directory); | 965 | closedir (iter->directory); |
965 | GNUNET_free (iter->dirname); | 966 | GNUNET_free (iter->dirname); |
966 | GNUNET_free (iter); | 967 | GNUNET_free (iter); |
967 | return GNUNET_SYSERR; | 968 | return GNUNET_SYSERR; |
968 | } | 969 | } |
969 | while (NULL != (finfo = readdir (iter->directory))) | 970 | while (NULL != (finfo = readdir (iter->directory))) |
970 | { | 971 | { |
971 | if ((0 == strcmp (finfo->d_name, ".")) || | 972 | if ((0 == strcmp (finfo->d_name, ".")) || |
972 | (0 == strcmp (finfo->d_name, ".."))) | 973 | (0 == strcmp (finfo->d_name, ".."))) |
973 | continue; | 974 | continue; |
974 | GNUNET_asprintf (&iter->next_name, | 975 | GNUNET_asprintf (&iter->next_name, |
975 | "%s%s%s", | 976 | "%s%s%s", iter->dirname, DIR_SEPARATOR_STR, finfo->d_name); |
976 | iter->dirname, DIR_SEPARATOR_STR, finfo->d_name); | 977 | break; |
977 | break; | 978 | } |
978 | } | ||
979 | if (finfo == NULL) | 979 | if (finfo == NULL) |
980 | { | 980 | { |
981 | GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES); | 981 | GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES); |
982 | return GNUNET_NO; | 982 | return GNUNET_NO; |
983 | } | 983 | } |
984 | GNUNET_SCHEDULER_add_with_priority (iter->priority, | 984 | GNUNET_SCHEDULER_add_with_priority (iter->priority, |
985 | &directory_iterator_task, iter); | 985 | &directory_iterator_task, iter); |
986 | return GNUNET_YES; | 986 | return GNUNET_YES; |
@@ -1011,11 +1011,11 @@ GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio, | |||
1011 | di->callback_cls = callback_cls; | 1011 | di->callback_cls = callback_cls; |
1012 | di->directory = OPENDIR (dirName); | 1012 | di->directory = OPENDIR (dirName); |
1013 | if (di->directory == NULL) | 1013 | if (di->directory == NULL) |
1014 | { | 1014 | { |
1015 | GNUNET_free (di); | 1015 | GNUNET_free (di); |
1016 | callback (callback_cls, NULL, NULL, NULL); | 1016 | callback (callback_cls, NULL, NULL, NULL); |
1017 | return; | 1017 | return; |
1018 | } | 1018 | } |
1019 | di->dirname = GNUNET_strdup (dirName); | 1019 | di->dirname = GNUNET_strdup (dirName); |
1020 | di->priority = prio; | 1020 | di->priority = prio; |
1021 | GNUNET_DISK_directory_iterator_next (di, GNUNET_NO); | 1021 | GNUNET_DISK_directory_iterator_next (di, GNUNET_NO); |
@@ -1058,21 +1058,21 @@ GNUNET_DISK_directory_remove (const char *fileName) | |||
1058 | return GNUNET_OK; | 1058 | return GNUNET_OK; |
1059 | if ((errno != EISDIR) && | 1059 | if ((errno != EISDIR) && |
1060 | /* EISDIR is not sufficient in all cases, e.g. | 1060 | /* EISDIR is not sufficient in all cases, e.g. |
1061 | sticky /tmp directory may result in EPERM on BSD. | 1061 | * sticky /tmp directory may result in EPERM on BSD. |
1062 | So we also explicitly check "isDirectory" */ | 1062 | * So we also explicitly check "isDirectory" */ |
1063 | (GNUNET_YES != GNUNET_DISK_directory_test (fileName))) | 1063 | (GNUNET_YES != GNUNET_DISK_directory_test (fileName))) |
1064 | { | 1064 | { |
1065 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); | 1065 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); |
1066 | return GNUNET_SYSERR; | 1066 | return GNUNET_SYSERR; |
1067 | } | 1067 | } |
1068 | if (GNUNET_SYSERR == | 1068 | if (GNUNET_SYSERR == |
1069 | GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL)) | 1069 | GNUNET_DISK_directory_scan (fileName, &remove_helper, NULL)) |
1070 | return GNUNET_SYSERR; | 1070 | return GNUNET_SYSERR; |
1071 | if (0 != RMDIR (fileName)) | 1071 | if (0 != RMDIR (fileName)) |
1072 | { | 1072 | { |
1073 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); | 1073 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", fileName); |
1074 | return GNUNET_SYSERR; | 1074 | return GNUNET_SYSERR; |
1075 | } | 1075 | } |
1076 | return GNUNET_OK; | 1076 | return GNUNET_OK; |
1077 | } | 1077 | } |
1078 | 1078 | ||
@@ -1109,22 +1109,22 @@ GNUNET_DISK_file_copy (const char *src, const char *dst) | |||
1109 | GNUNET_DISK_PERM_GROUP_READ | | 1109 | GNUNET_DISK_PERM_GROUP_READ | |
1110 | GNUNET_DISK_PERM_GROUP_WRITE); | 1110 | GNUNET_DISK_PERM_GROUP_WRITE); |
1111 | if (!out) | 1111 | if (!out) |
1112 | { | 1112 | { |
1113 | GNUNET_DISK_file_close (in); | 1113 | GNUNET_DISK_file_close (in); |
1114 | return GNUNET_SYSERR; | 1114 | return GNUNET_SYSERR; |
1115 | } | 1115 | } |
1116 | buf = GNUNET_malloc (COPY_BLK_SIZE); | 1116 | buf = GNUNET_malloc (COPY_BLK_SIZE); |
1117 | while (pos < size) | 1117 | while (pos < size) |
1118 | { | 1118 | { |
1119 | len = COPY_BLK_SIZE; | 1119 | len = COPY_BLK_SIZE; |
1120 | if (len > size - pos) | 1120 | if (len > size - pos) |
1121 | len = size - pos; | 1121 | len = size - pos; |
1122 | if (len != GNUNET_DISK_file_read (in, buf, len)) | 1122 | if (len != GNUNET_DISK_file_read (in, buf, len)) |
1123 | goto FAIL; | 1123 | goto FAIL; |
1124 | if (len != GNUNET_DISK_file_write (out, buf, len)) | 1124 | if (len != GNUNET_DISK_file_write (out, buf, len)) |
1125 | goto FAIL; | 1125 | goto FAIL; |
1126 | pos += len; | 1126 | pos += len; |
1127 | } | 1127 | } |
1128 | GNUNET_free (buf); | 1128 | GNUNET_free (buf); |
1129 | GNUNET_DISK_file_close (in); | 1129 | GNUNET_DISK_file_close (in); |
1130 | GNUNET_DISK_file_close (out); | 1130 | GNUNET_DISK_file_close (out); |
@@ -1149,17 +1149,17 @@ GNUNET_DISK_filename_canonicalize (char *fn) | |||
1149 | 1149 | ||
1150 | idx = fn; | 1150 | idx = fn; |
1151 | while (*idx) | 1151 | while (*idx) |
1152 | { | 1152 | { |
1153 | c = *idx; | 1153 | c = *idx; |
1154 | |||
1155 | if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || | ||
1156 | c == '"' || c == '<' || c == '>' || c == '|') | ||
1157 | { | ||
1158 | *idx = '_'; | ||
1159 | } | ||
1160 | 1154 | ||
1161 | idx++; | 1155 | if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || |
1156 | c == '"' || c == '<' || c == '>' || c == '|') | ||
1157 | { | ||
1158 | *idx = '_'; | ||
1162 | } | 1159 | } |
1160 | |||
1161 | idx++; | ||
1162 | } | ||
1163 | } | 1163 | } |
1164 | 1164 | ||
1165 | 1165 | ||
@@ -1179,12 +1179,12 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user) | |||
1179 | 1179 | ||
1180 | pws = getpwnam (user); | 1180 | pws = getpwnam (user); |
1181 | if (pws == NULL) | 1181 | if (pws == NULL) |
1182 | { | 1182 | { |
1183 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 1183 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
1184 | _("Cannot obtain information about user `%s': %s\n"), | 1184 | _("Cannot obtain information about user `%s': %s\n"), |
1185 | user, STRERROR (errno)); | 1185 | user, STRERROR (errno)); |
1186 | return GNUNET_SYSERR; | 1186 | return GNUNET_SYSERR; |
1187 | } | 1187 | } |
1188 | if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) | 1188 | if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) |
1189 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename); | 1189 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chown", filename); |
1190 | #endif | 1190 | #endif |
@@ -1205,10 +1205,10 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, | |||
1205 | off_t lockEnd, int excl) | 1205 | off_t lockEnd, int excl) |
1206 | { | 1206 | { |
1207 | if (fh == NULL) | 1207 | if (fh == NULL) |
1208 | { | 1208 | { |
1209 | errno = EINVAL; | 1209 | errno = EINVAL; |
1210 | return GNUNET_SYSERR; | 1210 | return GNUNET_SYSERR; |
1211 | } | 1211 | } |
1212 | 1212 | ||
1213 | #ifndef MINGW | 1213 | #ifndef MINGW |
1214 | struct flock fl; | 1214 | struct flock fl; |
@@ -1227,12 +1227,11 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart, | |||
1227 | o.Offset = lockStart; | 1227 | o.Offset = lockStart; |
1228 | 1228 | ||
1229 | if (!LockFileEx (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | 1229 | if (!LockFileEx (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) |
1230 | | LOCKFILE_FAIL_IMMEDIATELY, 0, lockEnd - lockStart, 0, | 1230 | | LOCKFILE_FAIL_IMMEDIATELY, 0, lockEnd - lockStart, 0, &o)) |
1231 | &o)) | 1231 | { |
1232 | { | 1232 | SetErrnoFromWinError (GetLastError ()); |
1233 | SetErrnoFromWinError (GetLastError ()); | 1233 | return GNUNET_SYSERR; |
1234 | return GNUNET_SYSERR; | 1234 | } |
1235 | } | ||
1236 | 1235 | ||
1237 | return GNUNET_OK; | 1236 | return GNUNET_OK; |
1238 | #endif | 1237 | #endif |
@@ -1251,10 +1250,10 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, | |||
1251 | off_t unlockEnd) | 1250 | off_t unlockEnd) |
1252 | { | 1251 | { |
1253 | if (fh == NULL) | 1252 | if (fh == NULL) |
1254 | { | 1253 | { |
1255 | errno = EINVAL; | 1254 | errno = EINVAL; |
1256 | return GNUNET_SYSERR; | 1255 | return GNUNET_SYSERR; |
1257 | } | 1256 | } |
1258 | 1257 | ||
1259 | #ifndef MINGW | 1258 | #ifndef MINGW |
1260 | struct flock fl; | 1259 | struct flock fl; |
@@ -1273,10 +1272,10 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, | |||
1273 | o.Offset = unlockStart; | 1272 | o.Offset = unlockStart; |
1274 | 1273 | ||
1275 | if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o)) | 1274 | if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o)) |
1276 | { | 1275 | { |
1277 | SetErrnoFromWinError (GetLastError ()); | 1276 | SetErrnoFromWinError (GetLastError ()); |
1278 | return GNUNET_SYSERR; | 1277 | return GNUNET_SYSERR; |
1279 | } | 1278 | } |
1280 | 1279 | ||
1281 | return GNUNET_OK; | 1280 | return GNUNET_OK; |
1282 | #endif | 1281 | #endif |
@@ -1302,6 +1301,7 @@ GNUNET_DISK_file_open (const char *fn, | |||
1302 | { | 1301 | { |
1303 | char *expfn; | 1302 | char *expfn; |
1304 | struct GNUNET_DISK_FileHandle *ret; | 1303 | struct GNUNET_DISK_FileHandle *ret; |
1304 | |||
1305 | #ifdef MINGW | 1305 | #ifdef MINGW |
1306 | DWORD access; | 1306 | DWORD access; |
1307 | DWORD disp; | 1307 | DWORD disp; |
@@ -1324,11 +1324,11 @@ GNUNET_DISK_file_open (const char *fn, | |||
1324 | else if (flags & GNUNET_DISK_OPEN_WRITE) | 1324 | else if (flags & GNUNET_DISK_OPEN_WRITE) |
1325 | oflags = O_WRONLY; | 1325 | oflags = O_WRONLY; |
1326 | else | 1326 | else |
1327 | { | 1327 | { |
1328 | GNUNET_break (0); | 1328 | GNUNET_break (0); |
1329 | GNUNET_free (expfn); | 1329 | GNUNET_free (expfn); |
1330 | return NULL; | 1330 | return NULL; |
1331 | } | 1331 | } |
1332 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) | 1332 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) |
1333 | oflags |= (O_CREAT | O_EXCL); | 1333 | oflags |= (O_CREAT | O_EXCL); |
1334 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1334 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
@@ -1336,22 +1336,22 @@ GNUNET_DISK_file_open (const char *fn, | |||
1336 | if (flags & GNUNET_DISK_OPEN_APPEND) | 1336 | if (flags & GNUNET_DISK_OPEN_APPEND) |
1337 | oflags |= O_APPEND; | 1337 | oflags |= O_APPEND; |
1338 | if (flags & GNUNET_DISK_OPEN_CREATE) | 1338 | if (flags & GNUNET_DISK_OPEN_CREATE) |
1339 | { | 1339 | { |
1340 | (void) GNUNET_DISK_directory_create_for_file (expfn); | 1340 | (void) GNUNET_DISK_directory_create_for_file (expfn); |
1341 | oflags |= O_CREAT; | 1341 | oflags |= O_CREAT; |
1342 | mode = translate_unix_perms(perm); | 1342 | mode = translate_unix_perms (perm); |
1343 | } | 1343 | } |
1344 | 1344 | ||
1345 | fd = open (expfn, oflags | O_LARGEFILE, mode); | 1345 | fd = open (expfn, oflags | O_LARGEFILE, mode); |
1346 | if (fd == -1) | 1346 | if (fd == -1) |
1347 | { | 1347 | { |
1348 | if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) | 1348 | if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) |
1349 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); | 1349 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); |
1350 | else | 1350 | else |
1351 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); | 1351 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); |
1352 | GNUNET_free (expfn); | 1352 | GNUNET_free (expfn); |
1353 | return NULL; | 1353 | return NULL; |
1354 | } | 1354 | } |
1355 | #else | 1355 | #else |
1356 | access = 0; | 1356 | access = 0; |
1357 | disp = OPEN_ALWAYS; | 1357 | disp = OPEN_ALWAYS; |
@@ -1364,48 +1364,47 @@ GNUNET_DISK_file_open (const char *fn, | |||
1364 | access = FILE_WRITE_DATA; | 1364 | access = FILE_WRITE_DATA; |
1365 | 1365 | ||
1366 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) | 1366 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) |
1367 | { | 1367 | { |
1368 | disp = CREATE_NEW; | 1368 | disp = CREATE_NEW; |
1369 | } | 1369 | } |
1370 | else if (flags & GNUNET_DISK_OPEN_CREATE) | 1370 | else if (flags & GNUNET_DISK_OPEN_CREATE) |
1371 | { | 1371 | { |
1372 | (void) GNUNET_DISK_directory_create_for_file (expfn); | 1372 | (void) GNUNET_DISK_directory_create_for_file (expfn); |
1373 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1373 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1374 | disp = CREATE_ALWAYS; | 1374 | disp = CREATE_ALWAYS; |
1375 | else | 1375 | else |
1376 | disp = OPEN_ALWAYS; | 1376 | disp = OPEN_ALWAYS; |
1377 | } | 1377 | } |
1378 | else if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1378 | else if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1379 | { | 1379 | { |
1380 | disp = TRUNCATE_EXISTING; | 1380 | disp = TRUNCATE_EXISTING; |
1381 | } | 1381 | } |
1382 | else | 1382 | else |
1383 | { | 1383 | { |
1384 | disp = OPEN_EXISTING; | 1384 | disp = OPEN_EXISTING; |
1385 | } | 1385 | } |
1386 | 1386 | ||
1387 | /* TODO: access priviledges? */ | 1387 | /* TODO: access priviledges? */ |
1388 | h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ | 1388 | h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ |
1389 | | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, | 1389 | | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); |
1390 | NULL); | ||
1391 | if (h == INVALID_HANDLE_VALUE) | 1390 | if (h == INVALID_HANDLE_VALUE) |
1391 | { | ||
1392 | SetErrnoFromWinError (GetLastError ()); | ||
1393 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); | ||
1394 | GNUNET_free (expfn); | ||
1395 | return NULL; | ||
1396 | } | ||
1397 | |||
1398 | if (flags & GNUNET_DISK_OPEN_APPEND) | ||
1399 | if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) | ||
1392 | { | 1400 | { |
1393 | SetErrnoFromWinError (GetLastError ()); | 1401 | SetErrnoFromWinError (GetLastError ()); |
1394 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn); | 1402 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", |
1403 | expfn); | ||
1404 | CloseHandle (h); | ||
1395 | GNUNET_free (expfn); | 1405 | GNUNET_free (expfn); |
1396 | return NULL; | 1406 | return NULL; |
1397 | } | 1407 | } |
1398 | |||
1399 | if (flags & GNUNET_DISK_OPEN_APPEND) | ||
1400 | if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) | ||
1401 | { | ||
1402 | SetErrnoFromWinError (GetLastError ()); | ||
1403 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", | ||
1404 | expfn); | ||
1405 | CloseHandle (h); | ||
1406 | GNUNET_free (expfn); | ||
1407 | return NULL; | ||
1408 | } | ||
1409 | #endif | 1408 | #endif |
1410 | 1409 | ||
1411 | ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle)); | 1410 | ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle)); |
@@ -1429,28 +1428,28 @@ int | |||
1429 | GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) | 1428 | GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) |
1430 | { | 1429 | { |
1431 | if (h == NULL) | 1430 | if (h == NULL) |
1432 | { | 1431 | { |
1433 | errno = EINVAL; | 1432 | errno = EINVAL; |
1434 | return GNUNET_SYSERR; | 1433 | return GNUNET_SYSERR; |
1435 | } | 1434 | } |
1436 | 1435 | ||
1437 | #if MINGW | 1436 | #if MINGW |
1438 | if (!CloseHandle (h->h)) | 1437 | if (!CloseHandle (h->h)) |
1439 | { | 1438 | { |
1440 | SetErrnoFromWinError (GetLastError ()); | 1439 | SetErrnoFromWinError (GetLastError ()); |
1441 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); | 1440 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); |
1442 | GNUNET_free (h->oOverlapRead); | 1441 | GNUNET_free (h->oOverlapRead); |
1443 | GNUNET_free (h->oOverlapWrite); | 1442 | GNUNET_free (h->oOverlapWrite); |
1444 | GNUNET_free (h); | 1443 | GNUNET_free (h); |
1445 | return GNUNET_SYSERR; | 1444 | return GNUNET_SYSERR; |
1446 | } | 1445 | } |
1447 | #else | 1446 | #else |
1448 | if (close (h->fd) != 0) | 1447 | if (close (h->fd) != 0) |
1449 | { | 1448 | { |
1450 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); | 1449 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close"); |
1451 | GNUNET_free (h); | 1450 | GNUNET_free (h); |
1452 | return GNUNET_SYSERR; | 1451 | return GNUNET_SYSERR; |
1453 | } | 1452 | } |
1454 | #endif | 1453 | #endif |
1455 | GNUNET_free (h); | 1454 | GNUNET_free (h); |
1456 | return GNUNET_OK; | 1455 | return GNUNET_OK; |
@@ -1482,43 +1481,44 @@ GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1482 | unsigned int needed; | 1481 | unsigned int needed; |
1483 | 1482 | ||
1484 | if (GNUNET_OK != | 1483 | if (GNUNET_OK != |
1485 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 1484 | GNUNET_CONFIGURATION_get_value_filename (cfg, serviceName, "HOME", &pfx)) |
1486 | serviceName, "HOME", &pfx)) | ||
1487 | return NULL; | 1485 | return NULL; |
1488 | if (pfx == NULL) | 1486 | if (pfx == NULL) |
1489 | { | 1487 | { |
1490 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1488 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1491 | _("No `%s' specified for service `%s' in configuration.\n"), | 1489 | _("No `%s' specified for service `%s' in configuration.\n"), |
1492 | "HOME", serviceName); | 1490 | "HOME", serviceName); |
1493 | return NULL; | 1491 | return NULL; |
1494 | } | 1492 | } |
1495 | needed = strlen (pfx) + 2; | 1493 | needed = strlen (pfx) + 2; |
1496 | if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\')) | 1494 | if ((pfx[strlen (pfx) - 1] != '/') && (pfx[strlen (pfx) - 1] != '\\')) |
1497 | needed++; | 1495 | needed++; |
1498 | va_start (ap, serviceName); | 1496 | va_start (ap, serviceName); |
1499 | while (1) | 1497 | while (1) |
1500 | { | 1498 | { |
1501 | c = va_arg (ap, const char *); | 1499 | c = va_arg (ap, const char *); |
1502 | if (c == NULL) | 1500 | |
1503 | break; | 1501 | if (c == NULL) |
1504 | needed += strlen (c); | 1502 | break; |
1505 | if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) | 1503 | needed += strlen (c); |
1506 | needed++; | 1504 | if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) |
1507 | } | 1505 | needed++; |
1506 | } | ||
1508 | va_end (ap); | 1507 | va_end (ap); |
1509 | ret = GNUNET_malloc (needed); | 1508 | ret = GNUNET_malloc (needed); |
1510 | strcpy (ret, pfx); | 1509 | strcpy (ret, pfx); |
1511 | GNUNET_free (pfx); | 1510 | GNUNET_free (pfx); |
1512 | va_start (ap, serviceName); | 1511 | va_start (ap, serviceName); |
1513 | while (1) | 1512 | while (1) |
1514 | { | 1513 | { |
1515 | c = va_arg (ap, const char *); | 1514 | c = va_arg (ap, const char *); |
1516 | if (c == NULL) | 1515 | |
1517 | break; | 1516 | if (c == NULL) |
1518 | if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) | 1517 | break; |
1519 | strcat (ret, DIR_SEPARATOR_STR); | 1518 | if ((c[strlen (c) - 1] != '/') && (c[strlen (c) - 1] != '\\')) |
1520 | strcat (ret, c); | 1519 | strcat (ret, DIR_SEPARATOR_STR); |
1521 | } | 1520 | strcat (ret, c); |
1521 | } | ||
1522 | va_end (ap); | 1522 | va_end (ap); |
1523 | if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\')) | 1523 | if ((ret[strlen (ret) - 1] != '/') && (ret[strlen (ret) - 1] != '\\')) |
1524 | (void) GNUNET_DISK_directory_create_for_file (ret); | 1524 | (void) GNUNET_DISK_directory_create_for_file (ret); |
@@ -1571,52 +1571,52 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | |||
1571 | enum GNUNET_DISK_MapType access, size_t len) | 1571 | enum GNUNET_DISK_MapType access, size_t len) |
1572 | { | 1572 | { |
1573 | if (h == NULL) | 1573 | if (h == NULL) |
1574 | { | 1574 | { |
1575 | errno = EINVAL; | 1575 | errno = EINVAL; |
1576 | return NULL; | 1576 | return NULL; |
1577 | } | 1577 | } |
1578 | 1578 | ||
1579 | #ifdef MINGW | 1579 | #ifdef MINGW |
1580 | DWORD mapAccess, protect; | 1580 | DWORD mapAccess, protect; |
1581 | 1581 | ||
1582 | if ((access & GNUNET_DISK_MAP_TYPE_READ) && | 1582 | if ((access & GNUNET_DISK_MAP_TYPE_READ) && |
1583 | (access & GNUNET_DISK_MAP_TYPE_WRITE)) | 1583 | (access & GNUNET_DISK_MAP_TYPE_WRITE)) |
1584 | { | 1584 | { |
1585 | protect = PAGE_READWRITE; | 1585 | protect = PAGE_READWRITE; |
1586 | mapAccess = FILE_MAP_ALL_ACCESS; | 1586 | mapAccess = FILE_MAP_ALL_ACCESS; |
1587 | } | 1587 | } |
1588 | else if (access & GNUNET_DISK_MAP_TYPE_READ) | 1588 | else if (access & GNUNET_DISK_MAP_TYPE_READ) |
1589 | { | 1589 | { |
1590 | protect = PAGE_READONLY; | 1590 | protect = PAGE_READONLY; |
1591 | mapAccess = FILE_MAP_READ; | 1591 | mapAccess = FILE_MAP_READ; |
1592 | } | 1592 | } |
1593 | else if (access & GNUNET_DISK_MAP_TYPE_WRITE) | 1593 | else if (access & GNUNET_DISK_MAP_TYPE_WRITE) |
1594 | { | 1594 | { |
1595 | protect = PAGE_READWRITE; | 1595 | protect = PAGE_READWRITE; |
1596 | mapAccess = FILE_MAP_WRITE; | 1596 | mapAccess = FILE_MAP_WRITE; |
1597 | } | 1597 | } |
1598 | else | 1598 | else |
1599 | { | 1599 | { |
1600 | GNUNET_break (0); | 1600 | GNUNET_break (0); |
1601 | return NULL; | 1601 | return NULL; |
1602 | } | 1602 | } |
1603 | 1603 | ||
1604 | *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle)); | 1604 | *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle)); |
1605 | (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); | 1605 | (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); |
1606 | if ((*m)->h == INVALID_HANDLE_VALUE) | 1606 | if ((*m)->h == INVALID_HANDLE_VALUE) |
1607 | { | 1607 | { |
1608 | SetErrnoFromWinError (GetLastError ()); | 1608 | SetErrnoFromWinError (GetLastError ()); |
1609 | GNUNET_free (*m); | 1609 | GNUNET_free (*m); |
1610 | return NULL; | 1610 | return NULL; |
1611 | } | 1611 | } |
1612 | 1612 | ||
1613 | (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); | 1613 | (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); |
1614 | if (!(*m)->addr) | 1614 | if (!(*m)->addr) |
1615 | { | 1615 | { |
1616 | SetErrnoFromWinError (GetLastError ()); | 1616 | SetErrnoFromWinError (GetLastError ()); |
1617 | CloseHandle ((*m)->h); | 1617 | CloseHandle ((*m)->h); |
1618 | GNUNET_free (*m); | 1618 | GNUNET_free (*m); |
1619 | } | 1619 | } |
1620 | 1620 | ||
1621 | return (*m)->addr; | 1621 | return (*m)->addr; |
1622 | #else | 1622 | #else |
@@ -1631,10 +1631,10 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | |||
1631 | (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); | 1631 | (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); |
1632 | GNUNET_assert (NULL != (*m)->addr); | 1632 | GNUNET_assert (NULL != (*m)->addr); |
1633 | if (MAP_FAILED == (*m)->addr) | 1633 | if (MAP_FAILED == (*m)->addr) |
1634 | { | 1634 | { |
1635 | GNUNET_free (*m); | 1635 | GNUNET_free (*m); |
1636 | return NULL; | 1636 | return NULL; |
1637 | } | 1637 | } |
1638 | (*m)->len = len; | 1638 | (*m)->len = len; |
1639 | return (*m)->addr; | 1639 | return (*m)->addr; |
1640 | #endif | 1640 | #endif |
@@ -1649,21 +1649,22 @@ int | |||
1649 | GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h) | 1649 | GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h) |
1650 | { | 1650 | { |
1651 | int ret; | 1651 | int ret; |
1652 | |||
1652 | if (h == NULL) | 1653 | if (h == NULL) |
1653 | { | 1654 | { |
1654 | errno = EINVAL; | 1655 | errno = EINVAL; |
1655 | return GNUNET_SYSERR; | 1656 | return GNUNET_SYSERR; |
1656 | } | 1657 | } |
1657 | 1658 | ||
1658 | #ifdef MINGW | 1659 | #ifdef MINGW |
1659 | ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; | 1660 | ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; |
1660 | if (ret != GNUNET_OK) | 1661 | if (ret != GNUNET_OK) |
1661 | SetErrnoFromWinError (GetLastError ()); | 1662 | SetErrnoFromWinError (GetLastError ()); |
1662 | if (!CloseHandle (h->h) && (ret == GNUNET_OK)) | 1663 | if (!CloseHandle (h->h) && (ret == GNUNET_OK)) |
1663 | { | 1664 | { |
1664 | ret = GNUNET_SYSERR; | 1665 | ret = GNUNET_SYSERR; |
1665 | SetErrnoFromWinError (GetLastError ()); | 1666 | SetErrnoFromWinError (GetLastError ()); |
1666 | } | 1667 | } |
1667 | #else | 1668 | #else |
1668 | ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; | 1669 | ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; |
1669 | #endif | 1670 | #endif |
@@ -1681,10 +1682,10 @@ int | |||
1681 | GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | 1682 | GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) |
1682 | { | 1683 | { |
1683 | if (h == NULL) | 1684 | if (h == NULL) |
1684 | { | 1685 | { |
1685 | errno = EINVAL; | 1686 | errno = EINVAL; |
1686 | return GNUNET_SYSERR; | 1687 | return GNUNET_SYSERR; |
1687 | } | 1688 | } |
1688 | 1689 | ||
1689 | #ifdef MINGW | 1690 | #ifdef MINGW |
1690 | int ret; | 1691 | int ret; |
@@ -1715,9 +1716,7 @@ static int | |||
1715 | create_selectable_pipe (PHANDLE read_pipe_ptr, | 1716 | create_selectable_pipe (PHANDLE read_pipe_ptr, |
1716 | PHANDLE write_pipe_ptr, | 1717 | PHANDLE write_pipe_ptr, |
1717 | LPSECURITY_ATTRIBUTES sa_ptr, | 1718 | LPSECURITY_ATTRIBUTES sa_ptr, |
1718 | DWORD psize, | 1719 | DWORD psize, DWORD dwReadMode, DWORD dwWriteMode) |
1719 | DWORD dwReadMode, | ||
1720 | DWORD dwWriteMode) | ||
1721 | { | 1720 | { |
1722 | /* Default to error. */ | 1721 | /* Default to error. */ |
1723 | *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; | 1722 | *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; |
@@ -1731,108 +1730,107 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, | |||
1731 | char pipename[MAX_PATH]; | 1730 | char pipename[MAX_PATH]; |
1732 | 1731 | ||
1733 | /* Retry CreateNamedPipe as long as the pipe name is in use. | 1732 | /* Retry CreateNamedPipe as long as the pipe name is in use. |
1734 | Retrying will probably never be necessary, but we want | 1733 | * Retrying will probably never be necessary, but we want |
1735 | to be as robust as possible. */ | 1734 | * to be as robust as possible. */ |
1736 | while (1) | 1735 | while (1) |
1737 | { | 1736 | { |
1738 | static volatile LONG pipe_unique_id; | 1737 | static volatile LONG pipe_unique_id; |
1739 | 1738 | ||
1740 | snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", | 1739 | snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", |
1741 | getpid (), InterlockedIncrement ((LONG *)&pipe_unique_id)); | 1740 | getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); |
1742 | #if DEBUG_PIPE | 1741 | #if DEBUG_PIPE |
1743 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); | 1742 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1743 | "CreateNamedPipe: name = %s, size = %lu\n", pipename, psize); | ||
1744 | #endif | 1744 | #endif |
1745 | /* Use CreateNamedPipe instead of CreatePipe, because the latter | 1745 | /* Use CreateNamedPipe instead of CreatePipe, because the latter |
1746 | returns a write handle that does not permit FILE_READ_ATTRIBUTES | 1746 | * returns a write handle that does not permit FILE_READ_ATTRIBUTES |
1747 | access, on versions of win32 earlier than WinXP SP2. | 1747 | * access, on versions of win32 earlier than WinXP SP2. |
1748 | CreatePipe also stupidly creates a full duplex pipe, which is | 1748 | * CreatePipe also stupidly creates a full duplex pipe, which is |
1749 | a waste, since only a single direction is actually used. | 1749 | * a waste, since only a single direction is actually used. |
1750 | It's important to only allow a single instance, to ensure that | 1750 | * It's important to only allow a single instance, to ensure that |
1751 | the pipe was not created earlier by some other process, even if | 1751 | * the pipe was not created earlier by some other process, even if |
1752 | the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE | 1752 | * the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE |
1753 | because that is only available for Win2k SP2 and WinXP. */ | 1753 | * because that is only available for Win2k SP2 and WinXP. */ |
1754 | read_pipe = CreateNamedPipeA (pipename, | 1754 | read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ |
1755 | PIPE_ACCESS_INBOUND | dwReadMode, | 1755 | psize, /* output buffer size */ |
1756 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, | 1756 | psize, /* input buffer size */ |
1757 | 1, /* max instances */ | 1757 | NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); |
1758 | psize, /* output buffer size */ | 1758 | |
1759 | psize, /* input buffer size */ | 1759 | if (read_pipe != INVALID_HANDLE_VALUE) |
1760 | NMPWAIT_USE_DEFAULT_WAIT, | 1760 | { |
1761 | sa_ptr); | ||
1762 | |||
1763 | if (read_pipe != INVALID_HANDLE_VALUE) | ||
1764 | { | ||
1765 | #if DEBUG_PIPE | 1761 | #if DEBUG_PIPE |
1766 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); | 1762 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", |
1763 | read_pipe); | ||
1767 | #endif | 1764 | #endif |
1768 | break; | 1765 | break; |
1769 | } | 1766 | } |
1770 | 1767 | ||
1771 | DWORD err = GetLastError (); | 1768 | DWORD err = GetLastError (); |
1772 | switch (err) | 1769 | |
1773 | { | 1770 | switch (err) |
1774 | case ERROR_PIPE_BUSY: | 1771 | { |
1775 | /* The pipe is already open with compatible parameters. | 1772 | case ERROR_PIPE_BUSY: |
1776 | Pick a new name and retry. */ | 1773 | /* The pipe is already open with compatible parameters. |
1774 | * Pick a new name and retry. */ | ||
1777 | #if DEBUG_PIPE | 1775 | #if DEBUG_PIPE |
1778 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); | 1776 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); |
1779 | #endif | 1777 | #endif |
1780 | continue; | 1778 | continue; |
1781 | case ERROR_ACCESS_DENIED: | 1779 | case ERROR_ACCESS_DENIED: |
1782 | /* The pipe is already open with incompatible parameters. | 1780 | /* The pipe is already open with incompatible parameters. |
1783 | Pick a new name and retry. */ | 1781 | * Pick a new name and retry. */ |
1784 | #if DEBUG_PIPE | 1782 | #if DEBUG_PIPE |
1785 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); | 1783 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); |
1786 | #endif | 1784 | #endif |
1787 | continue; | 1785 | continue; |
1788 | case ERROR_CALL_NOT_IMPLEMENTED: | 1786 | case ERROR_CALL_NOT_IMPLEMENTED: |
1789 | /* We are on an older Win9x platform without named pipes. | 1787 | /* We are on an older Win9x platform without named pipes. |
1790 | Return an anonymous pipe as the best approximation. */ | 1788 | * Return an anonymous pipe as the best approximation. */ |
1791 | #if DEBUG_PIPE | 1789 | #if DEBUG_PIPE |
1792 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe not implemented, resorting to " | 1790 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1793 | "CreatePipe: size = %lu\n", psize); | 1791 | "CreateNamedPipe not implemented, resorting to " |
1792 | "CreatePipe: size = %lu\n", psize); | ||
1794 | #endif | 1793 | #endif |
1795 | if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) | 1794 | if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) |
1796 | { | 1795 | { |
1797 | #if DEBUG_PIPE | 1796 | #if DEBUG_PIPE |
1798 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", *read_pipe_ptr); | 1797 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", |
1799 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", *write_pipe_ptr); | 1798 | *read_pipe_ptr); |
1799 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", | ||
1800 | *write_pipe_ptr); | ||
1800 | #endif | 1801 | #endif |
1801 | return GNUNET_OK; | 1802 | return GNUNET_OK; |
1802 | } | 1803 | } |
1803 | err = GetLastError (); | 1804 | err = GetLastError (); |
1804 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); | 1805 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); |
1805 | return err; | 1806 | return err; |
1806 | default: | 1807 | default: |
1807 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); | 1808 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); |
1808 | return err; | 1809 | return err; |
1809 | } | ||
1810 | /* NOTREACHED */ | ||
1811 | } | 1810 | } |
1811 | /* NOTREACHED */ | ||
1812 | } | ||
1812 | #if DEBUG_PIPE | 1813 | #if DEBUG_PIPE |
1813 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); | 1814 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); |
1814 | #endif | 1815 | #endif |
1815 | 1816 | ||
1816 | /* Open the named pipe for writing. | 1817 | /* Open the named pipe for writing. |
1817 | Be sure to permit FILE_READ_ATTRIBUTES access. */ | 1818 | * Be sure to permit FILE_READ_ATTRIBUTES access. */ |
1818 | write_pipe = CreateFileA (pipename, | 1819 | write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ |
1819 | GENERIC_WRITE | FILE_READ_ATTRIBUTES, | 1820 | sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ |
1820 | 0, /* share mode */ | 1821 | 0); /* handle to template file */ |
1821 | sa_ptr, | ||
1822 | OPEN_EXISTING, | ||
1823 | dwWriteMode, /* flags and attributes */ | ||
1824 | 0); /* handle to template file */ | ||
1825 | 1822 | ||
1826 | if (write_pipe == INVALID_HANDLE_VALUE) | 1823 | if (write_pipe == INVALID_HANDLE_VALUE) |
1827 | { | 1824 | { |
1828 | /* Failure. */ | 1825 | /* Failure. */ |
1829 | DWORD err = GetLastError (); | 1826 | DWORD err = GetLastError (); |
1827 | |||
1830 | #if DEBUG_PIPE | 1828 | #if DEBUG_PIPE |
1831 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); | 1829 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); |
1832 | #endif | 1830 | #endif |
1833 | CloseHandle (read_pipe); | 1831 | CloseHandle (read_pipe); |
1834 | return err; | 1832 | return err; |
1835 | } | 1833 | } |
1836 | #if DEBUG_PIPE | 1834 | #if DEBUG_PIPE |
1837 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); | 1835 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); |
1838 | #endif | 1836 | #endif |
@@ -1858,9 +1856,8 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) | |||
1858 | struct GNUNET_DISK_PipeHandle *p; | 1856 | struct GNUNET_DISK_PipeHandle *p; |
1859 | struct GNUNET_DISK_FileHandle *fds; | 1857 | struct GNUNET_DISK_FileHandle *fds; |
1860 | 1858 | ||
1861 | p = | 1859 | p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + |
1862 | GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + | 1860 | 2 * sizeof (struct GNUNET_DISK_FileHandle)); |
1863 | 2 * sizeof (struct GNUNET_DISK_FileHandle)); | ||
1864 | fds = (struct GNUNET_DISK_FileHandle *) &p[1]; | 1861 | fds = (struct GNUNET_DISK_FileHandle *) &p[1]; |
1865 | p->fd[0] = &fds[0]; | 1862 | p->fd[0] = &fds[0]; |
1866 | p->fd[1] = &fds[1]; | 1863 | p->fd[1] = &fds[1]; |
@@ -1872,13 +1869,13 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) | |||
1872 | 1869 | ||
1873 | ret = pipe (fd); | 1870 | ret = pipe (fd); |
1874 | if (ret == -1) | 1871 | if (ret == -1) |
1875 | { | 1872 | { |
1876 | eno = errno; | 1873 | eno = errno; |
1877 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); | 1874 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); |
1878 | GNUNET_free (p); | 1875 | GNUNET_free (p); |
1879 | errno = eno; | 1876 | errno = eno; |
1880 | return NULL; | 1877 | return NULL; |
1881 | } | 1878 | } |
1882 | p->fd[0]->fd = fd[0]; | 1879 | p->fd[0]->fd = fd[0]; |
1883 | p->fd[1]->fd = fd[1]; | 1880 | p->fd[1]->fd = fd[1]; |
1884 | ret = 0; | 1881 | ret = 0; |
@@ -1902,60 +1899,64 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) | |||
1902 | if (0 > fcntl (fd[1], F_SETFD, flags)) | 1899 | if (0 > fcntl (fd[1], F_SETFD, flags)) |
1903 | ret = -1; | 1900 | ret = -1; |
1904 | if (ret == -1) | 1901 | if (ret == -1) |
1905 | { | 1902 | { |
1906 | eno = errno; | 1903 | eno = errno; |
1907 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fcntl"); | 1904 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fcntl"); |
1908 | GNUNET_break (0 == close (p->fd[0]->fd)); | 1905 | GNUNET_break (0 == close (p->fd[0]->fd)); |
1909 | GNUNET_break (0 == close (p->fd[1]->fd)); | 1906 | GNUNET_break (0 == close (p->fd[1]->fd)); |
1910 | GNUNET_free (p); | 1907 | GNUNET_free (p); |
1911 | errno = eno; | 1908 | errno = eno; |
1912 | return NULL; | 1909 | return NULL; |
1913 | } | 1910 | } |
1914 | #else | 1911 | #else |
1915 | BOOL ret; | 1912 | BOOL ret; |
1916 | HANDLE tmp_handle; | 1913 | HANDLE tmp_handle; |
1917 | 1914 | ||
1918 | ret = create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED); | 1915 | ret = |
1916 | create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, | ||
1917 | FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED); | ||
1919 | if (!ret) | 1918 | if (!ret) |
1920 | { | 1919 | { |
1921 | GNUNET_free (p); | 1920 | GNUNET_free (p); |
1922 | SetErrnoFromWinError (GetLastError ()); | 1921 | SetErrnoFromWinError (GetLastError ()); |
1923 | return NULL; | 1922 | return NULL; |
1924 | } | 1923 | } |
1925 | if (!DuplicateHandle (GetCurrentProcess (), p->fd[0]->h, | 1924 | if (!DuplicateHandle (GetCurrentProcess (), p->fd[0]->h, |
1926 | GetCurrentProcess (), &tmp_handle, 0, inherit_read == GNUNET_YES ? TRUE : FALSE, | 1925 | GetCurrentProcess (), &tmp_handle, 0, |
1927 | DUPLICATE_SAME_ACCESS)) | 1926 | inherit_read == GNUNET_YES ? TRUE : FALSE, |
1928 | { | 1927 | DUPLICATE_SAME_ACCESS)) |
1929 | SetErrnoFromWinError (GetLastError ()); | 1928 | { |
1930 | CloseHandle (p->fd[0]->h); | 1929 | SetErrnoFromWinError (GetLastError ()); |
1931 | CloseHandle (p->fd[1]->h); | 1930 | CloseHandle (p->fd[0]->h); |
1932 | GNUNET_free (p); | 1931 | CloseHandle (p->fd[1]->h); |
1933 | return NULL; | 1932 | GNUNET_free (p); |
1934 | } | 1933 | return NULL; |
1935 | CloseHandle (p->fd[0]->h); | 1934 | } |
1936 | p->fd[0]->h = tmp_handle; | 1935 | CloseHandle (p->fd[0]->h); |
1937 | 1936 | p->fd[0]->h = tmp_handle; | |
1938 | if (!DuplicateHandle (GetCurrentProcess (), p->fd[1]->h, | 1937 | |
1939 | GetCurrentProcess (), &tmp_handle, 0, inherit_write == GNUNET_YES ? TRUE : FALSE, | 1938 | if (!DuplicateHandle (GetCurrentProcess (), p->fd[1]->h, |
1940 | DUPLICATE_SAME_ACCESS)) | 1939 | GetCurrentProcess (), &tmp_handle, 0, |
1941 | { | 1940 | inherit_write == GNUNET_YES ? TRUE : FALSE, |
1942 | SetErrnoFromWinError (GetLastError ()); | 1941 | DUPLICATE_SAME_ACCESS)) |
1943 | CloseHandle (p->fd[0]->h); | 1942 | { |
1944 | CloseHandle (p->fd[1]->h); | 1943 | SetErrnoFromWinError (GetLastError ()); |
1945 | GNUNET_free (p); | 1944 | CloseHandle (p->fd[0]->h); |
1946 | return NULL; | 1945 | CloseHandle (p->fd[1]->h); |
1947 | } | 1946 | GNUNET_free (p); |
1947 | return NULL; | ||
1948 | } | ||
1948 | CloseHandle (p->fd[1]->h); | 1949 | CloseHandle (p->fd[1]->h); |
1949 | p->fd[1]->h = tmp_handle; | 1950 | p->fd[1]->h = tmp_handle; |
1950 | if (!blocking) | 1951 | if (!blocking) |
1951 | { | 1952 | { |
1952 | DWORD mode; | 1953 | DWORD mode; |
1953 | 1954 | ||
1954 | mode = PIPE_NOWAIT; | 1955 | mode = PIPE_NOWAIT; |
1955 | SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); | 1956 | SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); |
1956 | SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); | 1957 | SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); |
1957 | /* this always fails on Windows 95, so we don't care about error handling */ | 1958 | /* this always fails on Windows 95, so we don't care about error handling */ |
1958 | } | 1959 | } |
1959 | p->fd[0]->type = GNUNET_PIPE; | 1960 | p->fd[0]->type = GNUNET_PIPE; |
1960 | p->fd[1]->type = GNUNET_PIPE; | 1961 | p->fd[1]->type = GNUNET_PIPE; |
1961 | 1962 | ||
@@ -1984,51 +1985,51 @@ GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write) | |||
1984 | */ | 1985 | */ |
1985 | int | 1986 | int |
1986 | GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, | 1987 | GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, |
1987 | enum GNUNET_DISK_PipeEnd end) | 1988 | enum GNUNET_DISK_PipeEnd end) |
1988 | { | 1989 | { |
1989 | int ret = GNUNET_OK; | 1990 | int ret = GNUNET_OK; |
1990 | int save; | 1991 | int save; |
1991 | 1992 | ||
1992 | #ifdef MINGW | 1993 | #ifdef MINGW |
1993 | if (end == GNUNET_DISK_PIPE_END_READ) | 1994 | if (end == GNUNET_DISK_PIPE_END_READ) |
1995 | { | ||
1996 | if (!CloseHandle (p->fd[0]->h)) | ||
1994 | { | 1997 | { |
1995 | if (!CloseHandle (p->fd[0]->h)) | 1998 | SetErrnoFromWinError (GetLastError ()); |
1996 | { | 1999 | ret = GNUNET_SYSERR; |
1997 | SetErrnoFromWinError (GetLastError ()); | ||
1998 | ret = GNUNET_SYSERR; | ||
1999 | } | ||
2000 | p->fd[0]->h = INVALID_HANDLE_VALUE; | ||
2001 | } | 2000 | } |
2001 | p->fd[0]->h = INVALID_HANDLE_VALUE; | ||
2002 | } | ||
2002 | else if (end == GNUNET_DISK_PIPE_END_WRITE) | 2003 | else if (end == GNUNET_DISK_PIPE_END_WRITE) |
2004 | { | ||
2005 | if (!CloseHandle (p->fd[1]->h)) | ||
2003 | { | 2006 | { |
2004 | if (!CloseHandle (p->fd[1]->h)) | 2007 | SetErrnoFromWinError (GetLastError ()); |
2005 | { | 2008 | ret = GNUNET_SYSERR; |
2006 | SetErrnoFromWinError (GetLastError ()); | ||
2007 | ret = GNUNET_SYSERR; | ||
2008 | } | ||
2009 | p->fd[1]->h = INVALID_HANDLE_VALUE; | ||
2010 | } | 2009 | } |
2010 | p->fd[1]->h = INVALID_HANDLE_VALUE; | ||
2011 | } | ||
2011 | save = errno; | 2012 | save = errno; |
2012 | #else | 2013 | #else |
2013 | save = 0; | 2014 | save = 0; |
2014 | if (end == GNUNET_DISK_PIPE_END_READ) | 2015 | if (end == GNUNET_DISK_PIPE_END_READ) |
2016 | { | ||
2017 | if (0 != close (p->fd[0]->fd)) | ||
2015 | { | 2018 | { |
2016 | if (0 != close (p->fd[0]->fd)) | 2019 | ret = GNUNET_SYSERR; |
2017 | { | 2020 | save = errno; |
2018 | ret = GNUNET_SYSERR; | ||
2019 | save = errno; | ||
2020 | } | ||
2021 | p->fd[0]->fd = -1; | ||
2022 | } | 2021 | } |
2022 | p->fd[0]->fd = -1; | ||
2023 | } | ||
2023 | else if (end == GNUNET_DISK_PIPE_END_WRITE) | 2024 | else if (end == GNUNET_DISK_PIPE_END_WRITE) |
2025 | { | ||
2026 | if (0 != close (p->fd[1]->fd)) | ||
2024 | { | 2027 | { |
2025 | if (0 != close (p->fd[1]->fd)) | 2028 | ret = GNUNET_SYSERR; |
2026 | { | 2029 | save = errno; |
2027 | ret = GNUNET_SYSERR; | ||
2028 | save = errno; | ||
2029 | } | ||
2030 | p->fd[1]->fd = -1; | ||
2031 | } | 2030 | } |
2031 | p->fd[1]->fd = -1; | ||
2032 | } | ||
2032 | #endif | 2033 | #endif |
2033 | errno = save; | 2034 | errno = save; |
2034 | return ret; | 2035 | return ret; |
@@ -2048,35 +2049,35 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | |||
2048 | 2049 | ||
2049 | #ifdef MINGW | 2050 | #ifdef MINGW |
2050 | if (!CloseHandle (p->fd[0]->h)) | 2051 | if (!CloseHandle (p->fd[0]->h)) |
2051 | { | 2052 | { |
2052 | SetErrnoFromWinError (GetLastError ()); | 2053 | SetErrnoFromWinError (GetLastError ()); |
2053 | ret = GNUNET_SYSERR; | 2054 | ret = GNUNET_SYSERR; |
2054 | } | 2055 | } |
2055 | if (!CloseHandle (p->fd[1]->h)) | 2056 | if (!CloseHandle (p->fd[1]->h)) |
2056 | { | 2057 | { |
2057 | SetErrnoFromWinError (GetLastError ()); | 2058 | SetErrnoFromWinError (GetLastError ()); |
2058 | ret = GNUNET_SYSERR; | 2059 | ret = GNUNET_SYSERR; |
2059 | } | 2060 | } |
2060 | save = errno; | 2061 | save = errno; |
2061 | #else | 2062 | #else |
2062 | save = 0; | 2063 | save = 0; |
2063 | if (p->fd[0]->fd != -1) | 2064 | if (p->fd[0]->fd != -1) |
2065 | { | ||
2066 | if (0 != close (p->fd[0]->fd)) | ||
2064 | { | 2067 | { |
2065 | if (0 != close (p->fd[0]->fd)) | 2068 | ret = GNUNET_SYSERR; |
2066 | { | 2069 | save = errno; |
2067 | ret = GNUNET_SYSERR; | ||
2068 | save = errno; | ||
2069 | } | ||
2070 | } | 2070 | } |
2071 | } | ||
2071 | 2072 | ||
2072 | if (p->fd[1]->fd != -1) | 2073 | if (p->fd[1]->fd != -1) |
2074 | { | ||
2075 | if (0 != close (p->fd[1]->fd)) | ||
2073 | { | 2076 | { |
2074 | if (0 != close (p->fd[1]->fd)) | 2077 | ret = GNUNET_SYSERR; |
2075 | { | 2078 | save = errno; |
2076 | ret = GNUNET_SYSERR; | ||
2077 | save = errno; | ||
2078 | } | ||
2079 | } | 2079 | } |
2080 | } | ||
2080 | #endif | 2081 | #endif |
2081 | GNUNET_free (p); | 2082 | GNUNET_free (p); |
2082 | errno = save; | 2083 | errno = save; |
@@ -2114,93 +2115,104 @@ GNUNET_DISK_npipe_create (char **fn, | |||
2114 | openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; | 2115 | openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; |
2115 | 2116 | ||
2116 | while (h == NULL) | 2117 | while (h == NULL) |
2118 | { | ||
2119 | DWORD error_code; | ||
2120 | |||
2121 | name = NULL; | ||
2122 | if (*fn != NULL) | ||
2117 | { | 2123 | { |
2118 | DWORD error_code; | 2124 | GNUNET_asprintf (&name, "\\\\.\\pipe\\%.246s", fn); |
2119 | name = NULL; | ||
2120 | if (*fn != NULL) | ||
2121 | { | ||
2122 | GNUNET_asprintf(&name, "\\\\.\\pipe\\%.246s", fn); | ||
2123 | #if DEBUG_NPIPE | 2125 | #if DEBUG_NPIPE |
2124 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to create an instance of named pipe `%s'\n", name); | 2126 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2127 | "Trying to create an instance of named pipe `%s'\n", name); | ||
2125 | #endif | 2128 | #endif |
2126 | h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED, | 2129 | h = CreateNamedPipe (name, openMode | FILE_FLAG_OVERLAPPED, |
2127 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, NULL); | 2130 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, |
2128 | } | 2131 | NULL); |
2129 | else | 2132 | } |
2130 | { | 2133 | else |
2131 | GNUNET_asprintf(fn, "\\\\.\\pipe\\gnunet-%llu", | 2134 | { |
2132 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX)); | 2135 | GNUNET_asprintf (fn, "\\\\.\\pipe\\gnunet-%llu", |
2136 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
2137 | UINT64_MAX)); | ||
2133 | #if DEBUG_NPIPE | 2138 | #if DEBUG_NPIPE |
2134 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to create unique named pipe `%s'\n", *fn); | 2139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2140 | "Trying to create unique named pipe `%s'\n", *fn); | ||
2135 | #endif | 2141 | #endif |
2136 | h = CreateNamedPipe (*fn, openMode | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE, | 2142 | h = CreateNamedPipe (*fn, |
2137 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, NULL); | 2143 | openMode | FILE_FLAG_OVERLAPPED | |
2138 | } | 2144 | FILE_FLAG_FIRST_PIPE_INSTANCE, |
2139 | error_code = GetLastError (); | 2145 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, |
2140 | if (name) | 2146 | NULL); |
2141 | GNUNET_free(name); | 2147 | } |
2142 | /* don't re-set name to NULL yet */ | 2148 | error_code = GetLastError (); |
2143 | if (h == INVALID_HANDLE_VALUE) | 2149 | if (name) |
2144 | { | 2150 | GNUNET_free (name); |
2145 | SetErrnoFromWinError(error_code); | 2151 | /* don't re-set name to NULL yet */ |
2152 | if (h == INVALID_HANDLE_VALUE) | ||
2153 | { | ||
2154 | SetErrnoFromWinError (error_code); | ||
2146 | #if DEBUG_NPIPE | 2155 | #if DEBUG_NPIPE |
2147 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pipe creation have failed because of %d, errno is %d\n", error_code, errno); | 2156 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2157 | "Pipe creation have failed because of %d, errno is %d\n", | ||
2158 | error_code, errno); | ||
2148 | #endif | 2159 | #endif |
2149 | if (name == NULL) | 2160 | if (name == NULL) |
2150 | { | 2161 | { |
2151 | #if DEBUG_NPIPE | 2162 | #if DEBUG_NPIPE |
2152 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pipe was to be unique, considering re-creation\n"); | 2163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2164 | "Pipe was to be unique, considering re-creation\n"); | ||
2153 | #endif | 2165 | #endif |
2154 | GNUNET_free (*fn); | 2166 | GNUNET_free (*fn); |
2155 | *fn = NULL; | 2167 | *fn = NULL; |
2156 | if (error_code != ERROR_ACCESS_DENIED && error_code != ERROR_PIPE_BUSY) | 2168 | if (error_code != ERROR_ACCESS_DENIED && error_code != ERROR_PIPE_BUSY) |
2157 | { | 2169 | { |
2158 | return NULL; | 2170 | return NULL; |
2159 | } | 2171 | } |
2160 | #if DEBUG_NPIPE | 2172 | #if DEBUG_NPIPE |
2161 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pipe name was not unique, trying again\n"); | 2173 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2174 | "Pipe name was not unique, trying again\n"); | ||
2162 | #endif | 2175 | #endif |
2163 | h = NULL; | 2176 | h = NULL; |
2164 | } | 2177 | } |
2165 | else | 2178 | else |
2166 | return NULL; | 2179 | return NULL; |
2167 | } | ||
2168 | } | 2180 | } |
2181 | } | ||
2169 | errno = 0; | 2182 | errno = 0; |
2170 | 2183 | ||
2171 | ret = GNUNET_malloc(sizeof(*ret)); | 2184 | ret = GNUNET_malloc (sizeof (*ret)); |
2172 | ret->h = h; | 2185 | ret->h = h; |
2173 | ret->type = GNUNET_PIPE; | 2186 | ret->type = GNUNET_PIPE; |
2174 | 2187 | ||
2175 | ret->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED)); | 2188 | ret->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED)); |
2176 | ret->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED)); | 2189 | ret->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED)); |
2177 | 2190 | ||
2178 | ret->oOverlapRead->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | 2191 | ret->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
2179 | ret->oOverlapWrite->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | 2192 | ret->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
2180 | 2193 | ||
2181 | return ret; | 2194 | return ret; |
2182 | #else | 2195 | #else |
2183 | if (*fn == NULL) | 2196 | if (*fn == NULL) |
2184 | { | 2197 | { |
2185 | char dir[] = "/tmp/gnunet-pipe-XXXXXX"; | 2198 | char dir[] = "/tmp/gnunet-pipe-XXXXXX"; |
2186 | |||
2187 | if (mkdtemp(dir) == NULL) | ||
2188 | { | ||
2189 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mkdtemp"); | ||
2190 | return NULL; | ||
2191 | } | ||
2192 | GNUNET_asprintf(fn, "%s/child-control", dir); | ||
2193 | } | ||
2194 | 2199 | ||
2195 | if (mkfifo(*fn, translate_unix_perms(perm)) == -1) | 2200 | if (mkdtemp (dir) == NULL) |
2196 | { | 2201 | { |
2197 | if ( (errno != EEXIST) || | 2202 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mkdtemp"); |
2198 | (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) ) | 2203 | return NULL; |
2199 | return NULL; | ||
2200 | } | 2204 | } |
2205 | GNUNET_asprintf (fn, "%s/child-control", dir); | ||
2206 | } | ||
2207 | |||
2208 | if (mkfifo (*fn, translate_unix_perms (perm)) == -1) | ||
2209 | { | ||
2210 | if ((errno != EEXIST) || (0 != (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))) | ||
2211 | return NULL; | ||
2212 | } | ||
2201 | 2213 | ||
2202 | flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); | 2214 | flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); |
2203 | return GNUNET_DISK_file_open(*fn, flags, perm); | 2215 | return GNUNET_DISK_file_open (*fn, flags, perm); |
2204 | #endif | 2216 | #endif |
2205 | } | 2217 | } |
2206 | 2218 | ||
@@ -2232,14 +2244,14 @@ GNUNET_DISK_npipe_open (const char *fn, | |||
2232 | openMode = GENERIC_WRITE; | 2244 | openMode = GENERIC_WRITE; |
2233 | 2245 | ||
2234 | h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING, | 2246 | h = CreateFile (fn, openMode, 0, NULL, OPEN_EXISTING, |
2235 | FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL); | 2247 | FILE_FLAG_OVERLAPPED | FILE_READ_ATTRIBUTES, NULL); |
2236 | if (h == INVALID_HANDLE_VALUE) | 2248 | if (h == INVALID_HANDLE_VALUE) |
2237 | { | 2249 | { |
2238 | SetErrnoFromWinError(GetLastError()); | 2250 | SetErrnoFromWinError (GetLastError ()); |
2239 | return NULL; | 2251 | return NULL; |
2240 | } | 2252 | } |
2241 | 2253 | ||
2242 | ret = GNUNET_malloc(sizeof(*ret)); | 2254 | ret = GNUNET_malloc (sizeof (*ret)); |
2243 | ret->h = h; | 2255 | ret->h = h; |
2244 | ret->type = GNUNET_PIPE; | 2256 | ret->type = GNUNET_PIPE; |
2245 | ret->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED)); | 2257 | ret->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED)); |
@@ -2250,7 +2262,7 @@ GNUNET_DISK_npipe_open (const char *fn, | |||
2250 | return ret; | 2262 | return ret; |
2251 | #else | 2263 | #else |
2252 | flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); | 2264 | flags = flags & (~GNUNET_DISK_OPEN_FAILIFEXISTS); |
2253 | return GNUNET_DISK_file_open(fn, flags, perm); | 2265 | return GNUNET_DISK_file_open (fn, flags, perm); |
2254 | #endif | 2266 | #endif |
2255 | } | 2267 | } |
2256 | 2268 | ||
@@ -2263,16 +2275,16 @@ int | |||
2263 | GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe) | 2275 | GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe) |
2264 | { | 2276 | { |
2265 | #ifndef MINGW | 2277 | #ifndef MINGW |
2266 | return close(pipe->fd) == 0 ? GNUNET_OK : GNUNET_SYSERR; | 2278 | return close (pipe->fd) == 0 ? GNUNET_OK : GNUNET_SYSERR; |
2267 | #else | 2279 | #else |
2268 | BOOL ret; | 2280 | BOOL ret; |
2269 | 2281 | ||
2270 | ret = CloseHandle(pipe->h); | 2282 | ret = CloseHandle (pipe->h); |
2271 | if (!ret) | 2283 | if (!ret) |
2272 | { | 2284 | { |
2273 | SetErrnoFromWinError(GetLastError()); | 2285 | SetErrnoFromWinError (GetLastError ()); |
2274 | return GNUNET_SYSERR; | 2286 | return GNUNET_SYSERR; |
2275 | } | 2287 | } |
2276 | else | 2288 | else |
2277 | return GNUNET_OK; | 2289 | return GNUNET_OK; |
2278 | #endif | 2290 | #endif |
@@ -2291,14 +2303,14 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, | |||
2291 | enum GNUNET_DISK_PipeEnd n) | 2303 | enum GNUNET_DISK_PipeEnd n) |
2292 | { | 2304 | { |
2293 | switch (n) | 2305 | switch (n) |
2294 | { | 2306 | { |
2295 | case GNUNET_DISK_PIPE_END_READ: | 2307 | case GNUNET_DISK_PIPE_END_READ: |
2296 | case GNUNET_DISK_PIPE_END_WRITE: | 2308 | case GNUNET_DISK_PIPE_END_WRITE: |
2297 | return p->fd[n]; | 2309 | return p->fd[n]; |
2298 | default: | 2310 | default: |
2299 | GNUNET_break (0); | 2311 | GNUNET_break (0); |
2300 | return NULL; | 2312 | return NULL; |
2301 | } | 2313 | } |
2302 | } | 2314 | } |
2303 | 2315 | ||
2304 | 2316 | ||