aboutsummaryrefslogtreecommitdiff
path: root/src/util/disk.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-26 14:33:57 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-26 14:33:57 +0000
commitaffce940110bff113f6c204d5327b4c3df9b1749 (patch)
treea78f2bb0acad95f5388d516a3d2555ea9b26a185 /src/util/disk.c
parent1cf17ce5cbcb3bbf321dc4e605fcf28f8ac67325 (diff)
downloadgnunet-affce940110bff113f6c204d5327b4c3df9b1749.tar.gz
gnunet-affce940110bff113f6c204d5327b4c3df9b1749.zip
minor stylistic issues found while hunting #4619
Diffstat (limited to 'src/util/disk.c')
-rw-r--r--src/util/disk.c123
1 files changed, 79 insertions, 44 deletions
diff --git a/src/util/disk.c b/src/util/disk.c
index 100b312a4..40043549b 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2001--2013 GNUnet e.V. 3 Copyright (C) 2001--2013, 2016 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
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -1226,13 +1226,16 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1226 1226
1227 GNUNET_assert (NULL != dir_name); 1227 GNUNET_assert (NULL != dir_name);
1228 dname = GNUNET_STRINGS_filename_expand (dir_name); 1228 dname = GNUNET_STRINGS_filename_expand (dir_name);
1229 if (dname == NULL) 1229 if (NULL == dname)
1230 return GNUNET_SYSERR; 1230 return GNUNET_SYSERR;
1231 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) 1231 while ( (strlen (dname) > 0) &&
1232 (dname[strlen (dname) - 1] == DIR_SEPARATOR) )
1232 dname[strlen (dname) - 1] = '\0'; 1233 dname[strlen (dname) - 1] = '\0';
1233 if (0 != STAT (dname, &istat)) 1234 if (0 != STAT (dname, &istat))
1234 { 1235 {
1235 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname); 1236 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1237 "stat",
1238 dname);
1236 GNUNET_free (dname); 1239 GNUNET_free (dname);
1237 return GNUNET_SYSERR; 1240 return GNUNET_SYSERR;
1238 } 1241 }
@@ -1246,21 +1249,24 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1246 } 1249 }
1247 errno = 0; 1250 errno = 0;
1248 dinfo = OPENDIR (dname); 1251 dinfo = OPENDIR (dname);
1249 if ((errno == EACCES) || (NULL == dinfo)) 1252 if ( (EACCES == errno) ||
1253 (NULL == dinfo) )
1250 { 1254 {
1251 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); 1255 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1252 if (dinfo != NULL) 1256 "opendir",
1257 dname);
1258 if (NULL != dinfo)
1253 CLOSEDIR (dinfo); 1259 CLOSEDIR (dinfo);
1254 GNUNET_free (dname); 1260 GNUNET_free (dname);
1255 return GNUNET_SYSERR; 1261 return GNUNET_SYSERR;
1256 } 1262 }
1257 name_len = 256; 1263 name_len = 256;
1258 n_size = strlen (dname) + name_len + 2; 1264 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
1259 name = GNUNET_malloc (n_size); 1265 name = GNUNET_malloc (n_size);
1260 while (NULL != (finfo = READDIR (dinfo))) 1266 while (NULL != (finfo = READDIR (dinfo)))
1261 { 1267 {
1262 if ((0 == strcmp (finfo->d_name, ".")) || 1268 if ( (0 == strcmp (finfo->d_name, ".")) ||
1263 (0 == strcmp (finfo->d_name, ".."))) 1269 (0 == strcmp (finfo->d_name, "..")) )
1264 continue; 1270 continue;
1265 if (NULL != callback) 1271 if (NULL != callback)
1266 { 1272 {
@@ -1268,16 +1274,23 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1268 { 1274 {
1269 GNUNET_free (name); 1275 GNUNET_free (name);
1270 name_len = strlen (finfo->d_name); 1276 name_len = strlen (finfo->d_name);
1271 n_size = strlen (dname) + name_len + 2; 1277 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
1272 name = GNUNET_malloc (n_size); 1278 name = GNUNET_malloc (n_size);
1273 } 1279 }
1274 /* dname can end in "/" only if dname == "/"; 1280 /* dname can end in "/" only if dname == "/";
1275 * if dname does not end in "/", we need to add 1281 * if dname does not end in "/", we need to add
1276 * a "/" (otherwise, we must not!) */ 1282 * a "/" (otherwise, we must not!) */
1277 GNUNET_snprintf (name, n_size, "%s%s%s", dname, 1283 GNUNET_snprintf (name,
1278 (strcmp (dname, DIR_SEPARATOR_STR) == 1284 n_size,
1279 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); 1285 "%s%s%s",
1280 ret = callback (callback_cls, name); 1286 dname,
1287 (0 == strcmp (dname,
1288 DIR_SEPARATOR_STR))
1289 ? ""
1290 : DIR_SEPARATOR_STR,
1291 finfo->d_name);
1292 ret = callback (callback_cls,
1293 name);
1281 if (GNUNET_OK != ret) 1294 if (GNUNET_OK != ret)
1282 { 1295 {
1283 CLOSEDIR (dinfo); 1296 CLOSEDIR (dinfo);
@@ -1299,14 +1312,15 @@ GNUNET_DISK_directory_scan (const char *dir_name,
1299 1312
1300/** 1313/**
1301 * Function that removes the given directory by calling 1314 * Function that removes the given directory by calling
1302 * "GNUNET_DISK_directory_remove". 1315 * #GNUNET_DISK_directory_remove().
1303 * 1316 *
1304 * @param unused not used 1317 * @param unused not used
1305 * @param fn directory to remove 1318 * @param fn directory to remove
1306 * @return #GNUNET_OK 1319 * @return #GNUNET_OK
1307 */ 1320 */
1308static int 1321static int
1309remove_helper (void *unused, const char *fn) 1322remove_helper (void *unused,
1323 const char *fn)
1310{ 1324{
1311 (void) GNUNET_DISK_directory_remove (fn); 1325 (void) GNUNET_DISK_directory_remove (fn);
1312 return GNUNET_OK; 1326 return GNUNET_OK;
@@ -1314,7 +1328,7 @@ remove_helper (void *unused, const char *fn)
1314 1328
1315 1329
1316/** 1330/**
1317 * Remove all files in a directory (rm -rf). Call with 1331 * Remove all files in a directory (rm -r). Call with
1318 * caution. 1332 * caution.
1319 * 1333 *
1320 * @param filename the file to remove 1334 * @param filename the file to remove
@@ -1332,24 +1346,33 @@ GNUNET_DISK_directory_remove (const char *filename)
1332 } 1346 }
1333 if (0 != LSTAT (filename, &istat)) 1347 if (0 != LSTAT (filename, &istat))
1334 return GNUNET_NO; /* file may not exist... */ 1348 return GNUNET_NO; /* file may not exist... */
1335 (void) CHMOD (filename, S_IWUSR | S_IRUSR | S_IXUSR); 1349 (void) CHMOD (filename,
1336 if (UNLINK (filename) == 0) 1350 S_IWUSR | S_IRUSR | S_IXUSR);
1351 if (0 == UNLINK (filename))
1337 return GNUNET_OK; 1352 return GNUNET_OK;
1338 if ((errno != EISDIR) && 1353 if ( (errno != EISDIR) &&
1339 /* EISDIR is not sufficient in all cases, e.g. 1354 /* EISDIR is not sufficient in all cases, e.g.
1340 * sticky /tmp directory may result in EPERM on BSD. 1355 * sticky /tmp directory may result in EPERM on BSD.
1341 * So we also explicitly check "isDirectory" */ 1356 * So we also explicitly check "isDirectory" */
1342 (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES))) 1357 (GNUNET_YES !=
1343 { 1358 GNUNET_DISK_directory_test (filename,
1344 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); 1359 GNUNET_YES)) )
1360 {
1361 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1362 "rmdir",
1363 filename);
1345 return GNUNET_SYSERR; 1364 return GNUNET_SYSERR;
1346 } 1365 }
1347 if (GNUNET_SYSERR == 1366 if (GNUNET_SYSERR ==
1348 GNUNET_DISK_directory_scan (filename, &remove_helper, NULL)) 1367 GNUNET_DISK_directory_scan (filename,
1368 &remove_helper,
1369 NULL))
1349 return GNUNET_SYSERR; 1370 return GNUNET_SYSERR;
1350 if (0 != RMDIR (filename)) 1371 if (0 != RMDIR (filename))
1351 { 1372 {
1352 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); 1373 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1374 "rmdir",
1375 filename);
1353 return GNUNET_SYSERR; 1376 return GNUNET_SYSERR;
1354 } 1377 }
1355 return GNUNET_OK; 1378 return GNUNET_OK;
@@ -1428,8 +1451,7 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1428 char *idx; 1451 char *idx;
1429 char c; 1452 char c;
1430 1453
1431 idx = fn; 1454 for (idx = fn; *idx; idx++)
1432 while (*idx)
1433 { 1455 {
1434 c = *idx; 1456 c = *idx;
1435 1457
@@ -1438,8 +1460,6 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1438 { 1460 {
1439 *idx = '_'; 1461 *idx = '_';
1440 } 1462 }
1441
1442 idx++;
1443 } 1463 }
1444} 1464}
1445 1465
@@ -1450,24 +1470,33 @@ GNUNET_DISK_filename_canonicalize (char *fn)
1450 * 1470 *
1451 * @param filename name of file to change the owner of 1471 * @param filename name of file to change the owner of
1452 * @param user name of the new owner 1472 * @param user name of the new owner
1453 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 1473 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1454 */ 1474 */
1455int 1475int
1456GNUNET_DISK_file_change_owner (const char *filename, const char *user) 1476GNUNET_DISK_file_change_owner (const char *filename,
1477 const char *user)
1457{ 1478{
1458#ifndef MINGW 1479#ifndef MINGW
1459 struct passwd *pws; 1480 struct passwd *pws;
1460 1481
1461 pws = getpwnam (user); 1482 pws = getpwnam (user);
1462 if (pws == NULL) 1483 if (NULL == pws)
1463 { 1484 {
1464 LOG (GNUNET_ERROR_TYPE_ERROR, 1485 LOG (GNUNET_ERROR_TYPE_ERROR,
1465 _("Cannot obtain information about user `%s': %s\n"), user, 1486 _("Cannot obtain information about user `%s': %s\n"),
1487 user,
1466 STRERROR (errno)); 1488 STRERROR (errno));
1467 return GNUNET_SYSERR; 1489 return GNUNET_SYSERR;
1468 } 1490 }
1469 if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) 1491 if (0 != chown (filename,
1470 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename); 1492 pws->pw_uid,
1493 pws->pw_gid))
1494 {
1495 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1496 "chown",
1497 filename);
1498 return GNUNET_SYSERR;
1499 }
1471#endif 1500#endif
1472 return GNUNET_OK; 1501 return GNUNET_OK;
1473} 1502}
@@ -1475,15 +1504,18 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1475 1504
1476/** 1505/**
1477 * Lock a part of a file 1506 * Lock a part of a file
1507 *
1478 * @param fh file handle 1508 * @param fh file handle
1479 * @param lock_start absolute position from where to lock 1509 * @param lock_start absolute position from where to lock
1480 * @param lock_end absolute position until where to lock 1510 * @param lock_end absolute position until where to lock
1481 * @param excl GNUNET_YES for an exclusive lock 1511 * @param excl #GNUNET_YES for an exclusive lock
1482 * @return GNUNET_OK on success, GNUNET_SYSERR on error 1512 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1483 */ 1513 */
1484int 1514int
1485GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lock_start, 1515GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
1486 off_t lock_end, int excl) 1516 off_t lock_start,
1517 off_t lock_end,
1518 int excl)
1487{ 1519{
1488 if (fh == NULL) 1520 if (fh == NULL)
1489 { 1521 {
@@ -1527,13 +1559,15 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lock_start,
1527 1559
1528/** 1560/**
1529 * Unlock a part of a file 1561 * Unlock a part of a file
1562 *
1530 * @param fh file handle 1563 * @param fh file handle
1531 * @param unlock_start absolute position from where to unlock 1564 * @param unlock_start absolute position from where to unlock
1532 * @param unlock_end absolute position until where to unlock 1565 * @param unlock_end absolute position until where to unlock
1533 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1566 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1534 */ 1567 */
1535int 1568int
1536GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlock_start, 1569GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
1570 off_t unlock_start,
1537 off_t unlock_end) 1571 off_t unlock_end)
1538{ 1572{
1539 if (fh == NULL) 1573 if (fh == NULL)
@@ -1776,6 +1810,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1776 return ret; 1810 return ret;
1777} 1811}
1778 1812
1813
1779#ifdef WINDOWS 1814#ifdef WINDOWS
1780/** 1815/**
1781 * Get a GNUnet file handle from a W32 handle. 1816 * Get a GNUnet file handle from a W32 handle.