LCOV - code coverage report
Current view: top level - cmdline - elem.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 350 384 91.1 %
Date: 2015-05-12 Functions: 45 47 95.7 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011 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 "elem.h"
      21             : #include "support.h"
      22             : #include "util.h"
      23             : 
      24             : /****************************************************************************/
      25             : /* snapraid */
      26             : 
      27        1362 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
      28             : {
      29             :         struct snapraid_content* content;
      30             : 
      31        1362 :         content = malloc_nofail(sizeof(struct snapraid_content));
      32        1362 :         pathimport(content->content, sizeof(content->content), path);
      33        1362 :         content->device = dev;
      34             : 
      35        1362 :         return content;
      36             : }
      37             : 
      38        1241 : void content_free(struct snapraid_content* content)
      39             : {
      40        1241 :         free(content);
      41        1241 : }
      42             : 
      43         473 : struct snapraid_filter* filter_alloc_file(int direction, const char* pattern)
      44             : {
      45             :         struct snapraid_filter* filter;
      46             :         char* i;
      47             :         char* first;
      48             :         char* last;
      49             : 
      50         473 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
      51         473 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
      52         473 :         filter->direction = direction;
      53             : 
      54             :         /* find first and last slash */
      55         473 :         first = 0;
      56         473 :         last = 0;
      57        5889 :         for (i = filter->pattern; *i; ++i) {
      58        5416 :                 if (*i == '/') {
      59           3 :                         if (!first)
      60           2 :                                 first = i;
      61           3 :                         last = i;
      62             :                 }
      63             :         }
      64             : 
      65             :         /* it's a file filter */
      66         473 :         filter->is_disk = 0;
      67             : 
      68         473 :         if (first == 0) {
      69             :                 /* no slash */
      70         471 :                 filter->is_path = 0;
      71         471 :                 filter->is_dir = 0;
      72           2 :         } else if (first == last && last[1] == 0) {
      73             :                 /* one slash at the end */
      74           1 :                 filter->is_path = 0;
      75           1 :                 filter->is_dir = 1;
      76           1 :                 last[0] = 0;
      77             :         } else {
      78             :                 /* at least a slash not at the end */
      79           1 :                 filter->is_path = 1;
      80           1 :                 if (last[1] == 0) {
      81           0 :                         filter->is_dir = 1;
      82           0 :                         last[0] = 0;
      83             :                 } else {
      84           1 :                         filter->is_dir = 0;
      85             :                 }
      86             : 
      87             :                 /* a slash must be the first char, as we reject PATH/DIR/FILE */
      88           1 :                 if (filter->pattern[0] != '/') {
      89             :                         /* LCOV_EXCL_START */
      90             :                         free(filter);
      91             :                         return 0;
      92             :                         /* LCOV_EXCL_STOP */
      93             :                 }
      94             :         }
      95             : 
      96         473 :         return filter;
      97             : }
      98             : 
      99           3 : struct snapraid_filter* filter_alloc_disk(int direction, const char* pattern)
     100             : {
     101             :         struct snapraid_filter* filter;
     102             : 
     103           3 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
     104           3 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
     105           3 :         filter->direction = direction;
     106             : 
     107             :         /* it's a disk filter */
     108           3 :         filter->is_disk = 1;
     109           3 :         filter->is_path = 0;
     110           3 :         filter->is_dir = 0;
     111             : 
     112             :         /* no slash allowed in disk names */
     113           3 :         if (strchr(filter->pattern, '/') != 0) {
     114             :                 /* LCOV_EXCL_START */
     115             :                 free(filter);
     116             :                 return 0;
     117             :                 /* LCOV_EXCL_STOP */
     118             :         }
     119             : 
     120           3 :         return filter;
     121             : }
     122             : 
     123         440 : void filter_free(struct snapraid_filter* filter)
     124             : {
     125         440 :         free(filter);
     126         440 : }
     127             : 
     128      125397 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
     129             : {
     130             :         const char* direction;
     131      125397 :         if (filter->direction < 0)
     132      125162 :                 direction = "exclude";
     133             :         else
     134         235 :                 direction = "include";
     135             : 
     136      125397 :         if (filter->is_disk)
     137           0 :                 pathprint(out, out_size, "%s %s:", direction, filter->pattern);
     138      125397 :         else if (filter->is_dir)
     139           0 :                 pathprint(out, out_size, "%s %s/", direction, filter->pattern);
     140             :         else
     141      125397 :                 pathprint(out, out_size, "%s %s", direction, filter->pattern);
     142             : 
     143      125397 :         return out;
     144             : }
     145             : 
     146    11927105 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, const char* name, int is_dir)
     147             : {
     148    11927105 :         int ret = 0;
     149             : 
     150             :         /* matches dirs with dirs and files with files */
     151    11927105 :         if (filter->is_dir && !is_dir)
     152        2938 :                 return 0;
     153    11924167 :         if (!filter->is_dir && is_dir)
     154     5960270 :                 return 0;
     155             : 
     156     5963897 :         if (filter->is_path) {
     157             :                 /* skip initial slash, as always missing from the path */
     158        2938 :                 if (fnmatch(filter->pattern + 1, path, FNM_PATHNAME | FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     159           0 :                         ret = filter->direction;
     160             :         } else {
     161     5960959 :                 if (fnmatch(filter->pattern, name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     162      127917 :                         ret = filter->direction;
     163             :         }
     164             : 
     165     5963897 :         if (reason != 0 && ret < 0)
     166      124927 :                 *reason = filter;
     167             : 
     168     5963897 :         return ret;
     169             : }
     170             : 
     171     5969644 : static int filter_recurse(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* const_path, int is_dir)
     172             : {
     173             :         char path[PATH_MAX];
     174             :         char* name;
     175             :         unsigned i;
     176             : 
     177     5969644 :         pathcpy(path, sizeof(path), const_path);
     178             : 
     179             :         /* filter for all the directories */
     180     5969644 :         name = path;
     181    86217249 :         for (i = 0; path[i] != 0; ++i) {
     182    80250595 :                 if (path[i] == '/') {
     183             :                         /* set a terminator */
     184     5960451 :                         path[i] = 0;
     185             : 
     186             :                         /* filter the directory */
     187     5960451 :                         if (filter_apply(filter, reason, path, name, 1) != 0)
     188        2990 :                                 return filter->direction;
     189             : 
     190             :                         /* restore the slash */
     191     5957461 :                         path[i] = '/';
     192             : 
     193             :                         /* next name */
     194     5957461 :                         name = path + i + 1;
     195             :                 }
     196             :         }
     197             : 
     198             :         /* filter the final file */
     199     5966654 :         if (filter_apply(filter, reason, path, name, is_dir) != 0)
     200      124927 :                 return filter->direction;
     201             : 
     202     5841727 :         return 0;
     203             : }
     204             : 
     205     3187555 : static int filter_element(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub, int is_dir)
     206             : {
     207             :         tommy_node* i;
     208             : 
     209     3187555 :         int direction = 1; /* by default include all */
     210             : 
     211             :         /* for each filter */
     212     9072239 :         for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
     213             :                 int ret;
     214     6021268 :                 struct snapraid_filter* filter = i->data;
     215             : 
     216     6021268 :                 if (filter->is_disk) {
     217       51624 :                         if (fnmatch(filter->pattern, disk, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     218        8667 :                                 ret = filter->direction;
     219             :                         else
     220       42957 :                                 ret = 0;
     221       51624 :                         if (reason != 0 && ret < 0)
     222           0 :                                 *reason = filter;
     223             :                 } else {
     224     5969644 :                         ret = filter_recurse(filter, reason, sub, is_dir);
     225             :                 }
     226             : 
     227     6021268 :                 if (ret > 0) {
     228             :                         /* include the file */
     229       11657 :                         return 0;
     230     6009611 :                 } else if (ret < 0) {
     231             :                         /* exclude the file */
     232      124927 :                         return -1;
     233             :                 } else {
     234             :                         /* default is opposite of the last filter */
     235     5884684 :                         direction = -filter->direction;
     236     5884684 :                         if (reason != 0 && direction < 0)
     237     2977425 :                                 *reason = filter;
     238             :                         /* continue with the next one */
     239             :                 }
     240             :         }
     241             : 
     242             :         /* directories are always included by default, otherwise we cannot apply rules */
     243             :         /* to the contained files */
     244     3050971 :         if (is_dir)
     245        2919 :                 return 0;
     246             : 
     247             :         /* files are excluded/included depending of the last rule processed */
     248     3048052 :         if (direction < 0)
     249       45888 :                 return -1;
     250             : 
     251     3002164 :         return 0;
     252             : }
     253             : 
     254     3184635 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     255             : {
     256     3184635 :         return filter_element(filterlist, reason, disk, sub, 0);
     257             : }
     258             : 
     259        2920 : int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     260             : {
     261        2920 :         return filter_element(filterlist, reason, disk, sub, 1);
     262             : }
     263             : 
     264       80652 : int filter_existence(int filter_missing, const char* dir, const char* sub)
     265             : {
     266             :         char path[PATH_MAX];
     267             :         struct stat st;
     268             : 
     269       80652 :         if (!filter_missing)
     270       34956 :                 return 0;
     271             : 
     272             :         /* we directly check if in the disk the file is present or not */
     273       45696 :         pathprint(path, sizeof(path), "%s%s", dir, sub);
     274             : 
     275       45696 :         if (lstat(path, &st) != 0) {
     276             :                 /* if the file doesn't exist, we don't filter it out */
     277        5019 :                 if (errno == ENOENT)
     278        5019 :                         return 0;
     279             :                 /* LCOV_EXCL_START */
     280             :                 log_fatal("Error in stat file '%s'. %s.\n", path, strerror(errno));
     281             :                 exit(EXIT_FAILURE);
     282             :                 /* LCOV_EXCL_STOP */
     283             :         }
     284             : 
     285             :         /* the file is present, so we filter it out */
     286       40677 :         return 1;
     287             : }
     288             : 
     289       38818 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_file* file)
     290             : {
     291             :         unsigned i;
     292             : 
     293       38818 :         if (!filter_error)
     294       16290 :                 return 0;
     295             : 
     296             :         /* check each block of the file */
     297       60129 :         for (i = 0; i < file->blockmax; ++i) {
     298             :                 struct snapraid_block* block;
     299             :                 snapraid_info info;
     300             : 
     301       46037 :                 block = &file->blockvec[i];
     302             : 
     303       46037 :                 info = info_get(infoarr, block->parity_pos);
     304             : 
     305             :                 /* if the file has a bad block, don't exclude it */
     306       46037 :                 if (info_get_bad(info))
     307        8436 :                         return 0;
     308             :         }
     309             : 
     310             :         /* the file is correct, so we filter it out */
     311       14092 :         return 1;
     312             : }
     313             : 
     314     3039576 : int filter_content(tommy_list* contentlist, const char* path)
     315             : {
     316             :         tommy_node* i;
     317             : 
     318    39360526 :         for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
     319    16640687 :                 struct snapraid_content* content = i->data;
     320             :                 char tmp[PATH_MAX];
     321             : 
     322    16640687 :                 if (pathcmp(content->content, path) == 0)
     323           0 :                         return -1;
     324             : 
     325             :                 /* exclude also the ".tmp" copy used to save it */
     326    16640687 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
     327    16640687 :                 if (pathcmp(tmp, path) == 0)
     328           0 :                         return -1;
     329             : 
     330             :                 /* exclude also the ".lock" file */
     331    16640687 :                 pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
     332    16640687 :                 if (pathcmp(tmp, path) == 0)
     333           0 :                         return -1;
     334             :         }
     335             : 
     336     3039576 :         return 0;
     337             : }
     338             : 
     339    14832379 : block_off_t block_file_pos(struct snapraid_block* block)
     340             : {
     341    14832379 :         struct snapraid_file* file = block_file_get(block);
     342             : 
     343    14832379 :         if (block < file->blockvec || block >= file->blockvec + file->blockmax) {
     344             :                 /* LCOV_EXCL_START */
     345             :                 log_fatal("Internal inconsistency in block %u ownership\n", block->parity_pos);
     346             :                 exit(EXIT_FAILURE);
     347             :                 /* LCOV_EXCL_STOP */
     348             :         }
     349             : 
     350    14832379 :         return block - file->blockvec;
     351             : }
     352             : 
     353     5375510 : int block_is_last(struct snapraid_block* block)
     354             : {
     355     5375510 :         struct snapraid_file* file = block_file_get(block);
     356             : 
     357     5375510 :         return block == file->blockvec + file->blockmax - 1;
     358             : }
     359             : 
     360     6527495 : unsigned block_file_size(struct snapraid_block* block, unsigned block_size)
     361             : {
     362     6527495 :         block_off_t pos = block_file_pos(block);
     363             : 
     364             :         /* if it's the last block */
     365     6527495 :         if (pos + 1 == block_file_get(block)->blockmax) {
     366             :                 unsigned remainder;
     367     2658644 :                 if (block_file_get(block)->size == 0)
     368           0 :                         return 0;
     369     2658644 :                 remainder = block_file_get(block)->size % block_size;
     370     2658644 :                 if (remainder == 0)
     371        1529 :                         remainder = block_size;
     372     2658644 :                 return remainder;
     373             :         }
     374             : 
     375     3868851 :         return block_size;
     376             : }
     377             : 
     378      164533 : struct snapraid_deleted* deleted_alloc(void)
     379             : {
     380             :         struct snapraid_deleted* deleted;
     381             : 
     382      164533 :         deleted = malloc_nofail(sizeof(struct snapraid_deleted));
     383             : 
     384             :         /* set the state as deleted */
     385      164533 :         deleted->block.file_mixed = BLOCK_STATE_DELETED;
     386             : 
     387      164533 :         return deleted;
     388             : }
     389             : 
     390      135934 : struct snapraid_deleted* deleted_dup(struct snapraid_block* block)
     391             : {
     392             :         struct snapraid_deleted* deleted;
     393             : 
     394      135934 :         deleted = deleted_alloc();
     395             : 
     396             :         /* copy data from deleted block */
     397      135934 :         deleted->block.parity_pos = block->parity_pos;
     398      135934 :         memcpy(deleted->block.hash, block->hash, HASH_SIZE);
     399             : 
     400      135934 :         return deleted;
     401             : }
     402             : 
     403      163984 : void deleted_free(struct snapraid_deleted* deleted)
     404             : {
     405      163984 :         free(deleted);
     406      163984 : }
     407             : 
     408     3578529 : struct snapraid_file* file_alloc(unsigned block_size, const char* sub, data_off_t size, uint64_t mtime_sec, int mtime_nsec, uint64_t inode, uint64_t physical)
     409             : {
     410             :         struct snapraid_file* file;
     411             :         block_off_t i;
     412             : 
     413     3578529 :         file = malloc_nofail(sizeof(struct snapraid_file));
     414     3578529 :         file->sub = strdup_nofail(sub);
     415     3578529 :         file->size = size;
     416     3578529 :         file->blockmax = (size + block_size - 1) / block_size;
     417     3578529 :         file->mtime_sec = mtime_sec;
     418     3578529 :         file->mtime_nsec = mtime_nsec;
     419     3578529 :         file->inode = inode;
     420     3578529 :         file->physical = physical;
     421     3578529 :         file->flag = 0;
     422     3578529 :         file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
     423             : 
     424             :         /* set the back pointer */
     425    12379416 :         for (i = 0; i < file->blockmax; ++i) {
     426     8800887 :                 file->blockvec[i].parity_pos = POS_INVALID;
     427     8800887 :                 file->blockvec[i].file_mixed = 0;
     428             : 
     429             :                 /* set the file */
     430     8800887 :                 block_file_set(&file->blockvec[i], file);
     431             : 
     432             :                 /* set an invalid hash */
     433     8800887 :                 block_state_set(&file->blockvec[i], BLOCK_STATE_CHG);
     434     8800887 :                 hash_invalid_set(file->blockvec[i].hash);
     435             :         }
     436             : 
     437     3578529 :         return file;
     438             : }
     439             : 
     440     3426080 : void file_free(struct snapraid_file* file)
     441             : {
     442     3426080 :         free(file->sub);
     443     3426080 :         free(file->blockvec);
     444     3426080 :         free(file);
     445     3426080 : }
     446             : 
     447         155 : void file_rename(struct snapraid_file* file, const char* sub)
     448             : {
     449         155 :         free(file->sub);
     450         155 :         file->sub = strdup_nofail(sub);
     451         155 : }
     452             : 
     453        6145 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
     454             : {
     455             :         block_off_t i;
     456             : 
     457        6145 :         if (src_file->size != dst_file->size) {
     458             :                 /* LCOV_EXCL_START */
     459             :                 log_fatal("Internal inconsistency in copy file with different size\n");
     460             :                 exit(EXIT_FAILURE);
     461             :                 /* LCOV_EXCL_STOP */
     462             :         }
     463             : 
     464        6145 :         if (src_file->mtime_sec != dst_file->mtime_sec) {
     465             :                 /* LCOV_EXCL_START */
     466             :                 log_fatal("Internal inconsistency in copy file with different mtime_sec\n");
     467             :                 exit(EXIT_FAILURE);
     468             :                 /* LCOV_EXCL_STOP */
     469             :         }
     470             : 
     471        6145 :         if (src_file->mtime_nsec != dst_file->mtime_nsec) {
     472             :                 /* LCOV_EXCL_START */
     473             :                 log_fatal("Internal inconsistency in copy file with different mtime_nsec\n");
     474             :                 exit(EXIT_FAILURE);
     475             :                 /* LCOV_EXCL_STOP */
     476             :         }
     477             : 
     478       17785 :         for (i = 0; i < dst_file->blockmax; ++i) {
     479             :                 /* set a block with hash computed but without parity */
     480       11640 :                 block_state_set(&dst_file->blockvec[i], BLOCK_STATE_REP);
     481             : 
     482             :                 /* copy the hash */
     483       11640 :                 memcpy(dst_file->blockvec[i].hash, src_file->blockvec[i].hash, HASH_SIZE);
     484             :         }
     485             : 
     486        6145 :         file_flag_set(dst_file, FILE_IS_COPY);
     487        6145 : }
     488             : 
     489       47808 : const char* file_name(const struct snapraid_file* file)
     490             : {
     491       47808 :         const char* r = strrchr(file->sub, '/');
     492             : 
     493       47808 :         if (!r)
     494           6 :                 r = file->sub;
     495             :         else
     496       47802 :                 ++r;
     497       47808 :         return r;
     498             : }
     499             : 
     500      848716 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
     501             : {
     502      848716 :         const uint64_t* arg = void_arg;
     503      848716 :         const struct snapraid_file* file = void_data;
     504             : 
     505      848716 :         if (*arg < file->inode)
     506           0 :                 return -1;
     507      848716 :         if (*arg > file->inode)
     508           0 :                 return 1;
     509      848716 :         return 0;
     510             : }
     511             : 
     512           0 : int file_inode_compare(const void* void_a, const void* void_b)
     513             : {
     514           0 :         const struct snapraid_file* file_a = void_a;
     515           0 :         const struct snapraid_file* file_b = void_b;
     516             : 
     517           0 :         if (file_a->inode < file_b->inode)
     518           0 :                 return -1;
     519           0 :         if (file_a->inode > file_b->inode)
     520           0 :                 return 1;
     521           0 :         return 0;
     522             : }
     523             : 
     524      125441 : int file_path_compare(const void* void_a, const void* void_b)
     525             : {
     526      125441 :         const struct snapraid_file* file_a = void_a;
     527      125441 :         const struct snapraid_file* file_b = void_b;
     528             : 
     529      125441 :         return strcmp(file_a->sub, file_b->sub);
     530             : }
     531             : 
     532       52928 : int file_physical_compare(const void* void_a, const void* void_b)
     533             : {
     534       52928 :         const struct snapraid_file* file_a = void_a;
     535       52928 :         const struct snapraid_file* file_b = void_b;
     536             : 
     537       52928 :         if (file_a->physical < file_b->physical)
     538       34086 :                 return -1;
     539       18842 :         if (file_a->physical > file_b->physical)
     540       18842 :                 return 1;
     541           0 :         return 0;
     542             : }
     543             : 
     544       98966 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
     545             : {
     546       98966 :         const char* arg = void_arg;
     547       98966 :         const struct snapraid_file* file = void_data;
     548             : 
     549       98966 :         return strcmp(arg, file->sub);
     550             : }
     551             : 
     552       23904 : int file_name_compare(const void* void_a, const void* void_b)
     553             : {
     554       23904 :         const struct snapraid_file* file_a = void_a;
     555       23904 :         const struct snapraid_file* file_b = void_b;
     556       23904 :         const char* name_a = file_name(file_a);
     557       23904 :         const char* name_b = file_name(file_b);
     558             : 
     559       23904 :         return strcmp(name_a, name_b);
     560             : }
     561             : 
     562       23658 : int file_stamp_compare(const void* void_a, const void* void_b)
     563             : {
     564       23658 :         const struct snapraid_file* file_a = void_a;
     565       23658 :         const struct snapraid_file* file_b = void_b;
     566             : 
     567       23658 :         if (file_a->size < file_b->size)
     568           0 :                 return -1;
     569       23658 :         if (file_a->size > file_b->size)
     570           0 :                 return 1;
     571             : 
     572       23658 :         if (file_a->mtime_sec < file_b->mtime_sec)
     573           0 :                 return -1;
     574       23658 :         if (file_a->mtime_sec > file_b->mtime_sec)
     575           0 :                 return 1;
     576             : 
     577       23658 :         if (file_a->mtime_nsec < file_b->mtime_nsec)
     578           0 :                 return -1;
     579       23658 :         if (file_a->mtime_nsec > file_b->mtime_nsec)
     580           0 :                 return 1;
     581             : 
     582       23658 :         return 0;
     583             : }
     584             : 
     585       23904 : int file_namestamp_compare(const void* void_a, const void* void_b)
     586             : {
     587             :         int ret;
     588             : 
     589       23904 :         ret = file_name_compare(void_a, void_b);
     590       23904 :         if (ret != 0)
     591         246 :                 return ret;
     592             : 
     593       23658 :         return file_stamp_compare(void_a, void_b);
     594             : }
     595             : 
     596           0 : int file_pathstamp_compare(const void* void_a, const void* void_b)
     597             : {
     598             :         int ret;
     599             : 
     600           0 :         ret = file_path_compare(void_a, void_b);
     601           0 :         if (ret != 0)
     602           0 :                 return ret;
     603             : 
     604           0 :         return file_stamp_compare(void_a, void_b);
     605             : }
     606             : 
     607      115744 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
     608             : {
     609             :         struct snapraid_link* link;
     610             : 
     611      115744 :         link = malloc_nofail(sizeof(struct snapraid_link));
     612      115744 :         link->sub = strdup_nofail(sub);
     613      115744 :         link->linkto = strdup_nofail(linkto);
     614      115744 :         link->flag = link_flag;
     615             : 
     616      115744 :         return link;
     617             : }
     618             : 
     619      110535 : void link_free(struct snapraid_link* link)
     620             : {
     621      110535 :         free(link->sub);
     622      110535 :         free(link->linkto);
     623      110535 :         free(link);
     624      110535 : }
     625             : 
     626       29409 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
     627             : {
     628       29409 :         const char* arg = void_arg;
     629       29409 :         const struct snapraid_link* link = void_data;
     630             : 
     631       29409 :         return strcmp(arg, link->sub);
     632             : }
     633             : 
     634        1252 : int link_alpha_compare(const void* void_a, const void* void_b)
     635             : {
     636        1252 :         const struct snapraid_link* link_a = void_a;
     637        1252 :         const struct snapraid_link* link_b = void_b;
     638             : 
     639        1252 :         return strcmp(link_a->sub, link_b->sub);
     640             : }
     641             : 
     642         760 : struct snapraid_dir* dir_alloc(const char* sub)
     643             : {
     644             :         struct snapraid_dir* dir;
     645             : 
     646         760 :         dir = malloc_nofail(sizeof(struct snapraid_dir));
     647         760 :         dir->sub = strdup_nofail(sub);
     648         760 :         dir->flag = 0;
     649             : 
     650         760 :         return dir;
     651             : }
     652             : 
     653         716 : void dir_free(struct snapraid_dir* dir)
     654             : {
     655         716 :         free(dir->sub);
     656         716 :         free(dir);
     657         716 : }
     658             : 
     659         209 : int dir_name_compare(const void* void_arg, const void* void_data)
     660             : {
     661         209 :         const char* arg = void_arg;
     662         209 :         const struct snapraid_dir* dir = void_data;
     663             : 
     664         209 :         return strcmp(arg, dir->sub);
     665             : }
     666             : 
     667        1411 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev)
     668             : {
     669             :         struct snapraid_disk* disk;
     670             : 
     671        1411 :         disk = malloc_nofail(sizeof(struct snapraid_disk));
     672        1411 :         pathcpy(disk->name, sizeof(disk->name), name);
     673        1411 :         pathimport(disk->dir, sizeof(disk->dir), dir);
     674             : 
     675             :         /* ensure that the dir terminate with "/" if it isn't empty */
     676        1411 :         pathslash(disk->dir, sizeof(disk->dir));
     677             : 
     678        1411 :         disk->smartctl[0] = 0;
     679        1411 :         disk->device = dev;
     680        1411 :         disk->tick = 0;
     681        1411 :         disk->total_blocks = 0;
     682        1411 :         disk->free_blocks = 0;
     683        1411 :         disk->first_free_block = 0;
     684        1411 :         disk->has_volatile_inodes = 0;
     685        1411 :         disk->has_unreliable_physical = 0;
     686        1411 :         disk->has_different_uuid = 0;
     687        1411 :         disk->has_unsupported_uuid = 0;
     688        1411 :         disk->had_empty_uuid = 0;
     689        1411 :         disk->mapping_idx = -1;
     690        1411 :         tommy_list_init(&disk->filelist);
     691        1411 :         tommy_list_init(&disk->deletedlist);
     692        1411 :         tommy_hashdyn_init(&disk->inodeset);
     693        1411 :         tommy_hashdyn_init(&disk->pathset);
     694        1411 :         tommy_hashdyn_init(&disk->stampset);
     695        1411 :         tommy_list_init(&disk->linklist);
     696        1411 :         tommy_hashdyn_init(&disk->linkset);
     697        1411 :         tommy_list_init(&disk->dirlist);
     698        1411 :         tommy_hashdyn_init(&disk->dirset);
     699        1411 :         tommy_arrayblk_init(&disk->blockarr);
     700             : 
     701        1411 :         return disk;
     702             : }
     703             : 
     704        1303 : void disk_free(struct snapraid_disk* disk)
     705             : {
     706        1303 :         tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
     707        1303 :         tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)deleted_free);
     708        1303 :         tommy_hashdyn_done(&disk->inodeset);
     709        1303 :         tommy_hashdyn_done(&disk->pathset);
     710        1303 :         tommy_hashdyn_done(&disk->stampset);
     711        1303 :         tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
     712        1303 :         tommy_hashdyn_done(&disk->linkset);
     713        1303 :         tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
     714        1303 :         tommy_hashdyn_done(&disk->dirset);
     715        1303 :         tommy_arrayblk_done(&disk->blockarr);
     716        1303 :         free(disk);
     717        1303 : }
     718             : 
     719         720 : int disk_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
     720             : {
     721             :         block_off_t i;
     722             : 
     723             :         /* if there is an element, it's not empty */
     724             :         /* even if links and dirs have no block allocation */
     725         720 :         if (!tommy_list_empty(&disk->filelist))
     726         712 :                 return 0;
     727           8 :         if (!tommy_list_empty(&disk->linklist))
     728           0 :                 return 0;
     729           8 :         if (!tommy_list_empty(&disk->dirlist))
     730           0 :                 return 0;
     731             : 
     732             :         /* limit the search to the size of the disk */
     733           8 :         if (blockmax > tommy_arrayblk_size(&disk->blockarr))
     734           2 :                 blockmax = tommy_arrayblk_size(&disk->blockarr);
     735             : 
     736             :         /* checks all the blocks to search for deleted ones */
     737             :         /* this search is slow, but it's done only if no file is present */
     738        4641 :         for (i = 0; i < blockmax; ++i) {
     739        4634 :                 struct snapraid_block* block = tommy_arrayblk_get(&disk->blockarr, i);
     740        4634 :                 unsigned block_state = block_state_get(block);
     741             : 
     742        4634 :                 switch (block_state) {
     743             :                 case BLOCK_STATE_EMPTY :
     744             :                         /* empty block are expected for an empty disk */
     745        4633 :                         break;
     746             :                 case BLOCK_STATE_DELETED :
     747             :                         /* if there is a deleted block, the disk is not empty */
     748           1 :                         return 0;
     749             :                 default :
     750             :                         /* LCOV_EXCL_START */
     751             :                         log_fatal("Internal inconsistency for used block in disk '%s' without files\n", disk->name);
     752             :                         exit(EXIT_FAILURE);
     753             :                         /* LCOV_EXCL_STOP */
     754             :                 }
     755             :         }
     756             : 
     757             :         /* finally, it's empty */
     758           7 :         return 1;
     759             : }
     760             : 
     761        1381 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
     762             : {
     763             :         struct snapraid_map* map;
     764             : 
     765        1381 :         map = malloc_nofail(sizeof(struct snapraid_map));
     766        1381 :         pathcpy(map->name, sizeof(map->name), name);
     767        1381 :         map->position = position;
     768        1381 :         map->total_blocks = total_blocks;
     769        1381 :         map->free_blocks = free_blocks;
     770        1381 :         pathcpy(map->uuid, sizeof(map->uuid), uuid);
     771             : 
     772        1381 :         return map;
     773             : }
     774             : 
     775        1279 : void map_free(struct snapraid_map* map)
     776             : {
     777        1279 :         free(map);
     778        1279 : }
     779             : 
     780      665521 : int info_time_compare(const void* void_a, const void* void_b)
     781             : {
     782      665521 :         const snapraid_info* info_a = void_a;
     783      665521 :         const snapraid_info* info_b = void_b;
     784      665521 :         time_t time_a = info_get_time(*info_a);
     785      665521 :         time_t time_b = info_get_time(*info_b);
     786             : 
     787      665521 :         if (time_a < time_b)
     788       26639 :                 return -1;
     789      638882 :         if (time_a > time_b)
     790      160266 :                 return 1;
     791      478616 :         return 0;
     792             : }
     793             : 

Generated by: LCOV version 1.10