LCOV - code coverage report
Current view: top level - cmdline - pool.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 64 68 94.1 %
Date: 2016-11-21 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2013 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #include "support.h"
      21             : #include "elem.h"
      22             : #include "state.h"
      23             : 
      24             : /**
      25             :  * Clean a directory tree removing all the symlinks and empty directories.
      26             :  * Return == 0 if the directory is empty, and it can be removed
      27             :  */
      28           5 : static int clean_dir(struct snapraid_state* state, const char* dir)
      29             : {
      30             :         DIR* d;
      31           5 :         int ignored = 0;
      32             : 
      33           5 :         d = opendir(dir);
      34           5 :         if (!d) {
      35             :                 /* LCOV_EXCL_START */
      36             :                 log_fatal("Error opening pool directory '%s'. %s.\n", dir, strerror(errno));
      37             :                 exit(EXIT_FAILURE);
      38             :                 /* LCOV_EXCL_STOP */
      39             :         }
      40             : 
      41             :         while (1) {
      42             :                 char path_next[PATH_MAX];
      43             :                 struct stat st;
      44             :                 const char* name;
      45             :                 struct dirent* dd;
      46             : 
      47             :                 /* clear errno to detect erroneous conditions */
      48        5768 :                 errno = 0;
      49        5768 :                 dd = readdir(d);
      50        5768 :                 if (dd == 0 && errno != 0) {
      51             :                         /* LCOV_EXCL_START */
      52             :                         log_fatal("Error reading pool directory '%s'. %s.\n", dir, strerror(errno));
      53             :                         exit(EXIT_FAILURE);
      54             :                         /* LCOV_EXCL_STOP */
      55             :                 }
      56        5768 :                 if (dd == 0 && errno == 0) {
      57           5 :                         break; /* finished */
      58             :                 }
      59             : 
      60             :                 /* skip "." and ".." files */
      61        5763 :                 name = dd->d_name;
      62        5763 :                 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
      63          10 :                         continue;
      64             : 
      65        5753 :                 pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
      66             : 
      67             : #if HAVE_STRUCT_DIRENT_D_STAT
      68             :                 /* convert dirent to lstat result */
      69             :                 dirent_lstat(dd, &st);
      70             : 
      71             :                 /* if the st_mode field is missing, takes care to fill it using normal lstat() */
      72             :                 /* at now this can happen only in Windows */
      73             :                 if (st.st_mode == 0) {
      74             :                         if (lstat(path_next, &st) != 0) {
      75             :                                 /* LCOV_EXCL_START */
      76             :                                 log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
      77             :                                 exit(EXIT_FAILURE);
      78             :                                 /* LCOV_EXCL_STOP */
      79             :                         }
      80             :                 }
      81             : #else
      82             :                 /* get lstat info about the file */
      83        5753 :                 if (lstat(path_next, &st) != 0) {
      84             :                         /* LCOV_EXCL_START */
      85             :                         log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
      86             :                         exit(EXIT_FAILURE);
      87             :                         /* LCOV_EXCL_STOP */
      88             :                 }
      89             : #endif
      90             : 
      91        5753 :                 if (S_ISLNK(st.st_mode)) {
      92             :                         int ret;
      93             : 
      94             :                         /* delete the link */
      95        5751 :                         ret = remove(path_next);
      96        5751 :                         if (ret < 0) {
      97             :                                 /* LCOV_EXCL_START */
      98             :                                 log_fatal("Error removing symlink '%s'. %s.\n", path_next, strerror(errno));
      99             :                                 exit(EXIT_FAILURE);
     100             :                                 /* LCOV_EXCL_STOP */
     101             :                         }
     102           2 :                 } else if (S_ISDIR(st.st_mode)) {
     103             :                         /* recurse */
     104           2 :                         pathslash(path_next, sizeof(path_next));
     105           2 :                         if (clean_dir(state, path_next) == 0) {
     106             :                                 int ret;
     107             : 
     108             :                                 /* directory is empty, try to remove it */
     109           2 :                                 ret = rmdir(path_next);
     110           2 :                                 if (ret < 0) {
     111             : #ifdef _WIN32
     112             :                                         if (errno == EACCES) {
     113             :                                                 /* in Windows just ignore EACCES errors removing directories */
     114             :                                                 /* because it could happen that the directory is in use */
     115             :                                                 /* and it cannot be removed */
     116             :                                                 log_fatal("Directory '%s' not removed because it's in use.\n", path_next);
     117             :                                                 ignored = 1;
     118             :                                         } else
     119             : #endif
     120             :                                         {
     121             :                                                 /* LCOV_EXCL_START */
     122             :                                                 log_fatal("Error removing pool directory '%s'. %s.\n", path_next, strerror(errno));
     123             :                                                 exit(EXIT_FAILURE);
     124             :                                                 /* LCOV_EXCL_STOP */
     125             :                                         }
     126             :                                 }
     127             :                         } else {
     128             :                                 /* something was ignored inside the subdir */
     129           0 :                                 ignored = 1;
     130             :                         }
     131             :                 } else {
     132           0 :                         ignored = 1;
     133           0 :                         msg_verbose("Ignoring pool file '%s'\n", path_next);
     134             :                 }
     135        5763 :         }
     136             : 
     137           5 :         if (closedir(d) != 0) {
     138             :                 /* LCOV_EXCL_START */
     139             :                 log_fatal("Error closing pool directory '%s'. %s.\n", dir, strerror(errno));
     140             :                 exit(EXIT_FAILURE);
     141             :                 /* LCOV_EXCL_STOP */
     142             :         }
     143             : 
     144           5 :         return ignored;
     145             : }
     146             : 
     147             : /**
     148             :  * Create a link to the specified disk entry.
     149             :  */
     150       17589 : static void make_link(const char* pool_dir, const char* share_dir, struct snapraid_disk* disk, const char* sub)
     151             : {
     152             :         char path[PATH_MAX];
     153             :         char linkto[PATH_MAX];
     154             :         char linkto_exported[PATH_MAX];
     155             :         int ret;
     156             : 
     157             :         /* make the source path */
     158       17589 :         pathprint(path, sizeof(path), "%s%s", pool_dir, sub);
     159             : 
     160             :         /* make the linkto path */
     161       17589 :         if (share_dir[0] != 0) {
     162             :                 /* with a shared directory, use it */
     163       17589 :                 pathprint(linkto, sizeof(linkto), "%s%s/%s", share_dir, disk->name, sub);
     164             :         } else {
     165             :                 /* without a share directory, use the local disk paths */
     166           0 :                 pathprint(linkto, sizeof(linkto), "%s%s", disk->dir, sub);
     167             :         }
     168             : 
     169             :         /* create the ancestor directories */
     170       17589 :         ret = mkancestor(path);
     171       17589 :         if (ret != 0) {
     172             :                 /* LCOV_EXCL_START */
     173             :                 exit(EXIT_FAILURE);
     174             :                 /* LCOV_EXCL_STOP */
     175             :         }
     176             : 
     177             :         /* convert back slashes */
     178       17589 :         pathexport(linkto_exported, sizeof(linkto_exported), linkto);
     179             : 
     180             :         /* create the symlink */
     181       17589 :         ret = symlink(linkto_exported, path);
     182       17589 :         if (ret != 0) {
     183         521 :                 if (errno == EEXIST) {
     184         521 :                         log_fatal("WARNING! Duplicate pooling for '%s'\n", path);
     185             : #ifdef _WIN32
     186             :                 } else if (errno == EPERM) {
     187             :                         /* LCOV_EXCL_START */
     188             :                         log_fatal("You must run as Adminstrator to be able to create symlinks.\n");
     189             :                         exit(EXIT_FAILURE);
     190             :                         /* LCOV_EXCL_STOP */
     191             : #endif
     192             :                 } else {
     193             :                         /* LCOV_EXCL_START */
     194             :                         log_fatal("Error writing symlink '%s'. %s.\n", path, strerror(errno));
     195             :                         exit(EXIT_FAILURE);
     196             :                         /* LCOV_EXCL_STOP */
     197             :                 }
     198             :         }
     199       17589 : }
     200             : 
     201           3 : void state_pool(struct snapraid_state* state)
     202             : {
     203             :         tommy_node* i;
     204             :         char pool_dir[PATH_MAX];
     205             :         char share_dir[PATH_MAX];
     206             :         unsigned count;
     207             : 
     208           3 :         if (state->pool[0] == 0) {
     209             :                 /* LCOV_EXCL_START */
     210             :                 log_fatal("To use the 'pool' command you must set the pool directory in the configuration file\n");
     211             :                 exit(EXIT_FAILURE);
     212             :                 /* LCOV_EXCL_STOP */
     213             :         }
     214             : 
     215           3 :         msg_progress("Cleaning...\n");
     216             : 
     217             :         /* pool directory with final slash */
     218           3 :         pathprint(pool_dir, sizeof(pool_dir), "%s", state->pool);
     219           3 :         pathslash(pool_dir, sizeof(pool_dir));
     220             : 
     221             :         /* share directory with final slash */
     222           3 :         pathprint(share_dir, sizeof(share_dir), "%s", state->share);
     223           3 :         pathslash(share_dir, sizeof(share_dir));
     224             : 
     225             :         /* first clear the previous pool tree */
     226           3 :         clean_dir(state, pool_dir);
     227             : 
     228           3 :         msg_progress("Pooling...\n");
     229             : 
     230             :         /* for each disk */
     231           3 :         count = 0;
     232          21 :         for (i = state->disklist; i != 0; i = i->next) {
     233             :                 tommy_node* j;
     234          18 :                 struct snapraid_disk* disk = i->data;
     235             : 
     236             :                 /* for each file */
     237       17018 :                 for (j = disk->filelist; j != 0; j = j->next) {
     238       17000 :                         struct snapraid_file* file = j->data;
     239       17000 :                         make_link(pool_dir, share_dir, disk, file->sub);
     240       17000 :                         ++count;
     241             :                 }
     242             : 
     243             :                 /* for each link */
     244         607 :                 for (j = disk->linklist; j != 0; j = j->next) {
     245         589 :                         struct snapraid_link* slink = j->data;
     246         589 :                         make_link(pool_dir, share_dir, disk, slink->sub);
     247         589 :                         ++count;
     248             :                 }
     249             : 
     250             :                 /* we ignore empty dirs in disk->dir */
     251             :         }
     252             : 
     253           3 :         if (count)
     254           2 :                 msg_status("%u links created\n", count);
     255             :         else
     256           1 :                 msg_status("No link created\n");
     257             : 
     258           3 :         log_tag("summary:link_count::%u\n", count);
     259           3 :         log_tag("summary:exit:ok\n");
     260           3 :         log_flush();
     261           3 : }
     262             : 

Generated by: LCOV version 1.10