diff options
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 116 |
1 files changed, 96 insertions, 20 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index 8fd689070..e0227be70 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -1,21 +1,19 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2001--2013, 2016 GNUnet e.V. | 3 | Copyright (C) 2001--2013, 2016, 2018 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | it under the terms of the GNU General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
7 | by the Free Software Foundation; either version 3, or (at your | 7 | by the Free Software Foundation, either version 3 of the License, |
8 | option) any later version. | 8 | or (at your option) any later version. |
9 | 9 | ||
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | GNUnet is distributed in the hope that it will be useful, but |
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with GNUnet; see the file COPYING. If not, write to the | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 17 | */ |
20 | /** | 18 | /** |
21 | * @file util/disk.c | 19 | * @file util/disk.c |
@@ -832,6 +830,12 @@ GNUNET_DISK_directory_create_for_file (const char *filename) | |||
832 | errno = EINVAL; | 830 | errno = EINVAL; |
833 | return GNUNET_SYSERR; | 831 | return GNUNET_SYSERR; |
834 | } | 832 | } |
833 | if (0 == ACCESS (rdir, W_OK)) | ||
834 | { | ||
835 | GNUNET_free (rdir); | ||
836 | return GNUNET_OK; | ||
837 | } | ||
838 | |||
835 | len = strlen (rdir); | 839 | len = strlen (rdir); |
836 | while ((len > 0) && (rdir[len] != DIR_SEPARATOR)) | 840 | while ((len > 0) && (rdir[len] != DIR_SEPARATOR)) |
837 | len--; | 841 | len--; |
@@ -1401,13 +1405,28 @@ GNUNET_DISK_file_copy (const char *src, | |||
1401 | struct GNUNET_DISK_FileHandle *in; | 1405 | struct GNUNET_DISK_FileHandle *in; |
1402 | struct GNUNET_DISK_FileHandle *out; | 1406 | struct GNUNET_DISK_FileHandle *out; |
1403 | 1407 | ||
1404 | if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES)) | 1408 | if (GNUNET_OK != |
1409 | GNUNET_DISK_file_size (src, | ||
1410 | &size, | ||
1411 | GNUNET_YES, | ||
1412 | GNUNET_YES)) | ||
1413 | { | ||
1414 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
1415 | "stat", | ||
1416 | src); | ||
1405 | return GNUNET_SYSERR; | 1417 | return GNUNET_SYSERR; |
1418 | } | ||
1406 | pos = 0; | 1419 | pos = 0; |
1407 | in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, | 1420 | in = GNUNET_DISK_file_open (src, |
1421 | GNUNET_DISK_OPEN_READ, | ||
1408 | GNUNET_DISK_PERM_NONE); | 1422 | GNUNET_DISK_PERM_NONE); |
1409 | if (!in) | 1423 | if (! in) |
1424 | { | ||
1425 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
1426 | "open", | ||
1427 | src); | ||
1410 | return GNUNET_SYSERR; | 1428 | return GNUNET_SYSERR; |
1429 | } | ||
1411 | out = | 1430 | out = |
1412 | GNUNET_DISK_file_open (dst, | 1431 | GNUNET_DISK_file_open (dst, |
1413 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | | 1432 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | |
@@ -1418,6 +1437,9 @@ GNUNET_DISK_file_copy (const char *src, | |||
1418 | GNUNET_DISK_PERM_GROUP_WRITE); | 1437 | GNUNET_DISK_PERM_GROUP_WRITE); |
1419 | if (!out) | 1438 | if (!out) |
1420 | { | 1439 | { |
1440 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
1441 | "open", | ||
1442 | dst); | ||
1421 | GNUNET_DISK_file_close (in); | 1443 | GNUNET_DISK_file_close (in); |
1422 | return GNUNET_SYSERR; | 1444 | return GNUNET_SYSERR; |
1423 | } | 1445 | } |
@@ -1670,16 +1692,19 @@ GNUNET_DISK_file_open (const char *fn, | |||
1670 | return NULL; | 1692 | return NULL; |
1671 | } | 1693 | } |
1672 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) | 1694 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) |
1673 | oflags |= (O_CREAT | O_EXCL); | 1695 | oflags |= (O_CREAT | O_EXCL); |
1674 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1696 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1675 | oflags |= O_TRUNC; | 1697 | oflags |= O_TRUNC; |
1676 | if (flags & GNUNET_DISK_OPEN_APPEND) | 1698 | if (flags & GNUNET_DISK_OPEN_APPEND) |
1677 | oflags |= O_APPEND; | 1699 | oflags |= O_APPEND; |
1678 | if (flags & GNUNET_DISK_OPEN_CREATE) | 1700 | if(GNUNET_NO == GNUNET_DISK_file_test(fn)) |
1679 | { | 1701 | { |
1680 | (void) GNUNET_DISK_directory_create_for_file (expfn); | 1702 | if (flags & GNUNET_DISK_OPEN_CREATE ) |
1681 | oflags |= O_CREAT; | 1703 | { |
1682 | mode = translate_unix_perms (perm); | 1704 | (void) GNUNET_DISK_directory_create_for_file (expfn); |
1705 | oflags |= O_CREAT; | ||
1706 | mode = translate_unix_perms (perm); | ||
1707 | } | ||
1683 | } | 1708 | } |
1684 | 1709 | ||
1685 | fd = open (expfn, oflags | 1710 | fd = open (expfn, oflags |
@@ -2641,4 +2666,55 @@ GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, | |||
2641 | return GNUNET_OK; | 2666 | return GNUNET_OK; |
2642 | } | 2667 | } |
2643 | 2668 | ||
2669 | |||
2670 | /** | ||
2671 | * Remove the directory given under @a option in | ||
2672 | * section [PATHS] in configuration under @a cfg_filename | ||
2673 | * | ||
2674 | * @param cfg_filename configuration file to parse | ||
2675 | * @param option option with the dir name to purge | ||
2676 | */ | ||
2677 | void | ||
2678 | GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, | ||
2679 | const char *option) | ||
2680 | { | ||
2681 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
2682 | char *tmpname; | ||
2683 | |||
2684 | cfg = GNUNET_CONFIGURATION_create (); | ||
2685 | if (GNUNET_OK != | ||
2686 | GNUNET_CONFIGURATION_load (cfg, | ||
2687 | cfg_filename)) | ||
2688 | { | ||
2689 | GNUNET_break (0); | ||
2690 | GNUNET_CONFIGURATION_destroy (cfg); | ||
2691 | return; | ||
2692 | } | ||
2693 | if (GNUNET_OK != | ||
2694 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
2695 | "PATHS", | ||
2696 | option, | ||
2697 | &tmpname)) | ||
2698 | { | ||
2699 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
2700 | "PATHS", | ||
2701 | option); | ||
2702 | GNUNET_CONFIGURATION_destroy (cfg); | ||
2703 | return; | ||
2704 | } | ||
2705 | GNUNET_CONFIGURATION_destroy (cfg); | ||
2706 | if (GNUNET_SYSERR == | ||
2707 | GNUNET_DISK_directory_remove (tmpname)) | ||
2708 | { | ||
2709 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
2710 | "remove", | ||
2711 | tmpname); | ||
2712 | GNUNET_free (tmpname); | ||
2713 | return; | ||
2714 | } | ||
2715 | GNUNET_free (tmpname); | ||
2716 | } | ||
2717 | |||
2718 | |||
2719 | |||
2644 | /* end of disk.c */ | 2720 | /* end of disk.c */ |