diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2012-11-07 18:26:33 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2012-11-07 18:26:33 +0000 |
commit | 6aef664a921b6c68f2260b8a94e24c1c34ea46af (patch) | |
tree | 61a6435802616d6f57c9623218794041feec634b /src/util/disk.c | |
parent | 45f6faea1582f1d1664af6836ea2d109853b1d63 (diff) | |
download | gnunet-6aef664a921b6c68f2260b8a94e24c1c34ea46af.tar.gz gnunet-6aef664a921b6c68f2260b8a94e24c1c34ea46af.zip |
making readable check optional
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 68 |
1 files changed, 21 insertions, 47 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index ae9135b0c..044c377d7 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -584,19 +584,19 @@ GNUNET_DISK_get_blocks_available (const char *part) | |||
584 | 584 | ||
585 | 585 | ||
586 | /** | 586 | /** |
587 | * Test if "fil" is a directory and readable. Also check if the directory is | 587 | * Test if "fil" is a directory and listable. Optionally, also check if the |
588 | * listable. Will not print an error message if the directory does not exist. | 588 | * directory is readable. Will not print an error message if the directory does |
589 | * Will log errors if GNUNET_SYSERR is returned (i.e., a file exists with the | 589 | * not exist. Will log errors if GNUNET_SYSERR is returned (i.e., a file exists |
590 | * same name). | 590 | * with the same name). |
591 | * | 591 | * |
592 | * @param fil filename to test | 592 | * @param fil filename to test |
593 | * @param is_listable GNUNET_YES to additionally check if "fil" is listable; | 593 | * @param is_readable GNUNET_YES to additionally check if "fil" is readable; |
594 | * GNUNET_NO to disable this check | 594 | * GNUNET_NO to disable this check |
595 | * @return GNUNET_YES if yes, GNUNET_NO if not; GNUNET_SYSERR if it | 595 | * @return GNUNET_YES if yes, GNUNET_NO if not; GNUNET_SYSERR if it |
596 | * does not exist | 596 | * does not exist or stat'ed |
597 | */ | 597 | */ |
598 | int | 598 | int |
599 | GNUNET_DISK_directory_test (const char *fil, int is_listable) | 599 | GNUNET_DISK_directory_test (const char *fil, int is_readable) |
600 | { | 600 | { |
601 | struct stat filestat; | 601 | struct stat filestat; |
602 | int ret; | 602 | int ret; |
@@ -605,22 +605,23 @@ GNUNET_DISK_directory_test (const char *fil, int is_listable) | |||
605 | if (ret != 0) | 605 | if (ret != 0) |
606 | { | 606 | { |
607 | if (errno != ENOENT) | 607 | if (errno != ENOENT) |
608 | { | ||
609 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); | 608 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); |
610 | return GNUNET_SYSERR; | 609 | return GNUNET_SYSERR; |
611 | } | ||
612 | return GNUNET_NO; | ||
613 | } | 610 | } |
614 | if (!S_ISDIR (filestat.st_mode)) | 611 | if (!S_ISDIR (filestat.st_mode)) |
612 | { | ||
613 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
614 | "A file already exits with the same name %s\n", fil); | ||
615 | return GNUNET_NO; | 615 | return GNUNET_NO; |
616 | if (GNUNET_YES == is_listable) | 616 | } |
617 | if (GNUNET_YES == is_readable) | ||
617 | ret = ACCESS (fil, R_OK | X_OK); | 618 | ret = ACCESS (fil, R_OK | X_OK); |
618 | else | 619 | else |
619 | ret = ACCESS (fil, R_OK); | 620 | ret = ACCESS (fil, X_OK); |
620 | if (ret < 0) | 621 | if (ret < 0) |
621 | { | 622 | { |
622 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); | 623 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); |
623 | return GNUNET_SYSERR; | 624 | return GNUNET_NO; |
624 | } | 625 | } |
625 | return GNUNET_YES; | 626 | return GNUNET_YES; |
626 | } | 627 | } |
@@ -682,9 +683,8 @@ int | |||
682 | GNUNET_DISK_directory_create (const char *dir) | 683 | GNUNET_DISK_directory_create (const char *dir) |
683 | { | 684 | { |
684 | char *rdir; | 685 | char *rdir; |
685 | unsigned int len; | 686 | int len; |
686 | unsigned int pos; | 687 | int pos; |
687 | unsigned int pos2; | ||
688 | int ret = GNUNET_OK; | 688 | int ret = GNUNET_OK; |
689 | 689 | ||
690 | rdir = GNUNET_STRINGS_filename_expand (dir); | 690 | rdir = GNUNET_STRINGS_filename_expand (dir); |
@@ -714,45 +714,18 @@ GNUNET_DISK_directory_create (const char *dir) | |||
714 | pos = 3; /* strlen("C:\\") */ | 714 | pos = 3; /* strlen("C:\\") */ |
715 | } | 715 | } |
716 | #endif | 716 | #endif |
717 | /* Check which low level directories already exist */ | ||
718 | pos2 = len; | ||
719 | rdir[len] = DIR_SEPARATOR; | ||
720 | while (pos <= pos2) | ||
721 | { | ||
722 | if (DIR_SEPARATOR == rdir[pos2]) | ||
723 | { | ||
724 | rdir[pos2] = '\0'; | ||
725 | ret = GNUNET_DISK_directory_test (rdir, GNUNET_YES); | ||
726 | if (GNUNET_SYSERR == ret) | ||
727 | { | ||
728 | GNUNET_free (rdir); | ||
729 | return GNUNET_SYSERR; | ||
730 | } | ||
731 | rdir[pos2] = DIR_SEPARATOR; | ||
732 | if (GNUNET_YES == ret) | ||
733 | { | ||
734 | pos2++; | ||
735 | break; | ||
736 | } | ||
737 | } | ||
738 | pos2--; | ||
739 | } | ||
740 | rdir[len] = '\0'; | ||
741 | if (pos < pos2) | ||
742 | pos = pos2; | ||
743 | /* Start creating directories */ | ||
744 | while (pos <= len) | 717 | while (pos <= len) |
745 | { | 718 | { |
746 | if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) | 719 | if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) |
747 | { | 720 | { |
748 | rdir[pos] = '\0'; | 721 | rdir[pos] = '\0'; |
749 | ret = GNUNET_DISK_directory_test (rdir, GNUNET_YES); | 722 | ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO); |
750 | if (ret == GNUNET_SYSERR) | 723 | if (GNUNET_NO == ret) |
751 | { | 724 | { |
752 | GNUNET_free (rdir); | 725 | GNUNET_free (rdir); |
753 | return GNUNET_SYSERR; | 726 | return GNUNET_SYSERR; |
754 | } | 727 | } |
755 | if (ret == GNUNET_NO) | 728 | if (GNUNET_SYSERR == ret) |
756 | { | 729 | { |
757 | #ifndef MINGW | 730 | #ifndef MINGW |
758 | ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ | 731 | ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ |
@@ -775,6 +748,7 @@ GNUNET_DISK_directory_create (const char *dir) | |||
775 | pos++; | 748 | pos++; |
776 | } | 749 | } |
777 | GNUNET_free (rdir); | 750 | GNUNET_free (rdir); |
751 | LOG (GNUNET_ERROR_TYPE_ERROR, "we are done here\n"); | ||
778 | return GNUNET_OK; | 752 | return GNUNET_OK; |
779 | } | 753 | } |
780 | 754 | ||