LCOV - code coverage report
Current view: top level - cmdline - scan.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 542 620 87.4 %
Date: 2015-11-20 Functions: 23 23 100.0 %

          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 "support.h"
      21             : #include "elem.h"
      22             : #include "state.h"
      23             : #include "parity.h"
      24             : 
      25             : struct snapraid_scan {
      26             :         struct snapraid_state* state; /**< State used. */
      27             :         struct snapraid_disk* disk; /**< Disk used. */
      28             : 
      29             :         /**
      30             :          * Counters of changes.
      31             :          */
      32             :         unsigned count_equal; /**< Files equal. */
      33             :         unsigned count_move; /**< Files with a different name, but equal inode, size and timestamp in the same disk. */
      34             :         unsigned count_restore; /**< Files with equal name, size and timestamp, but different inode. */
      35             :         unsigned count_change; /**< Files with same name, but different size and/or timestamp. */
      36             :         unsigned count_copy; /**< Files new, with same name size and timestamp of a file in a different disk. */
      37             :         unsigned count_insert; /**< Files new. */
      38             :         unsigned count_remove; /**< Files removed. */
      39             : 
      40             :         tommy_list file_insert_list; /**< Files to insert. */
      41             :         tommy_list link_insert_list; /**< Links to insert. */
      42             :         tommy_list dir_insert_list; /**< Dirs to insert. */
      43             : 
      44             :         /* nodes for data structures */
      45             :         tommy_node node;
      46             : };
      47             : 
      48             : /**
      49             :  * Remove the specified link from the data set.
      50             :  */
      51         956 : static void scan_link_remove(struct snapraid_scan* scan, struct snapraid_link* slink)
      52             : {
      53         956 :         struct snapraid_state* state = scan->state;
      54         956 :         struct snapraid_disk* disk = scan->disk;
      55             : 
      56             :         /* state changed */
      57         956 :         state->need_write = 1;
      58             : 
      59             :         /* remove the file from the link containers */
      60         956 :         tommy_hashdyn_remove_existing(&disk->linkset, &slink->nodeset);
      61         956 :         tommy_list_remove_existing(&disk->linklist, &slink->nodelist);
      62             : 
      63             :         /* deallocate */
      64         956 :         link_free(slink);
      65         956 : }
      66             : 
      67             : /**
      68             :  * Insert the specified link in the data set.
      69             :  */
      70        2457 : static void scan_link_insert(struct snapraid_scan* scan, struct snapraid_link* slink)
      71             : {
      72        2457 :         struct snapraid_state* state = scan->state;
      73        2457 :         struct snapraid_disk* disk = scan->disk;
      74             : 
      75             :         /* state changed */
      76        2457 :         state->need_write = 1;
      77             : 
      78             :         /* insert the link in the link containers */
      79        2457 :         tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
      80        2457 :         tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
      81        2457 : }
      82             : 
      83             : /**
      84             :  * Process a symbolic link.
      85             :  */
      86       32837 : static void scan_link(struct snapraid_scan* scan, int is_diff, const char* sub, const char* linkto, unsigned link_flag)
      87             : {
      88       32837 :         struct snapraid_state* state = scan->state;
      89       32837 :         struct snapraid_disk* disk = scan->disk;
      90             :         struct snapraid_link* slink;
      91             : 
      92             :         /* check if the link already exists */
      93       32837 :         slink = tommy_hashdyn_search(&disk->linkset, link_name_compare_to_arg, sub, link_name_hash(sub));
      94       32837 :         if (slink) {
      95             :                 /* check if multiple files have the same name */
      96       30380 :                 if (link_flag_has(slink, FILE_IS_PRESENT)) {
      97             :                         /* LCOV_EXCL_START */
      98             :                         log_fatal("Internal inconsistency for link '%s%s'\n", disk->dir, sub);
      99             :                         exit(EXIT_FAILURE);
     100             :                         /* LCOV_EXCL_STOP */
     101             :                 }
     102             : 
     103             :                 /* mark as present */
     104       30380 :                 link_flag_set(slink, FILE_IS_PRESENT);
     105             : 
     106             :                 /* check if the link is not changed and it's of the same kind */
     107       30380 :                 if (strcmp(slink->linkto, linkto) == 0 && link_flag == link_flag_get(slink, FILE_IS_LINK_MASK)) {
     108             :                         /* it's equal */
     109       30374 :                         ++scan->count_equal;
     110             : 
     111       60748 :                         if (state->opt.gui) {
     112         829 :                                 log_tag("scan:equal:%s:%s\n", disk->name, esc(slink->sub));
     113             :                         }
     114             :                 } else {
     115             :                         /* it's an update */
     116             : 
     117             :                         /* we have to save the linkto/type */
     118           6 :                         state->need_write = 1;
     119             : 
     120           6 :                         ++scan->count_change;
     121             : 
     122           6 :                         log_tag("scan:update:%s:%s\n", disk->name, esc(slink->sub));
     123           6 :                         if (is_diff) {
     124           3 :                                 printf("update %s%s\n", disk->dir, slink->sub);
     125             :                         }
     126             : 
     127             :                         /* update it */
     128           6 :                         free(slink->linkto);
     129           6 :                         slink->linkto = strdup_nofail(linkto);
     130           6 :                         link_flag_let(slink, link_flag, FILE_IS_LINK_MASK);
     131             :                 }
     132             : 
     133             :                 /* nothing more to do */
     134       63217 :                 return;
     135             :         } else {
     136             :                 /* create the new link */
     137        2457 :                 ++scan->count_insert;
     138             : 
     139        2457 :                 log_tag("scan:add:%s:%s\n", disk->name, esc(sub));
     140        2457 :                 if (is_diff) {
     141         599 :                         printf("add %s%s\n", disk->dir, sub);
     142             :                 }
     143             : 
     144             :                 /* and continue to insert it */
     145             :         }
     146             : 
     147             :         /* insert it */
     148        2457 :         slink = link_alloc(sub, linkto, link_flag);
     149             : 
     150             :         /* mark it as present */
     151        2457 :         link_flag_set(slink, FILE_IS_PRESENT);
     152             : 
     153             :         /* insert it in the delayed insert list */
     154        2457 :         tommy_list_insert_tail(&scan->link_insert_list, &slink->nodelist, slink);
     155             : }
     156             : 
     157             : /**
     158             :  * Insert the specified file in the parity.
     159             :  */
     160      100975 : static void scan_file_allocate(struct snapraid_scan* scan, struct snapraid_file* file)
     161             : {
     162      100975 :         struct snapraid_state* state = scan->state;
     163      100975 :         struct snapraid_disk* disk = scan->disk;
     164             :         block_off_t i;
     165             :         block_off_t parity_pos;
     166             : 
     167             :         /* state changed */
     168      100975 :         state->need_write = 1;
     169             : 
     170             :         /* allocate the blocks of the file */
     171      100975 :         parity_pos = disk->first_free_block;
     172      349689 :         for (i = 0; i < file->blockmax; ++i) {
     173             :                 struct snapraid_block* block;
     174             :                 struct snapraid_block* over_block;
     175             :                 snapraid_info info;
     176             : 
     177             :                 /* increment the position until the first really free block */
     178     1013456 :                 while (block_has_file(fs_par2block_get(disk, parity_pos)))
     179      516028 :                         ++parity_pos;
     180             : 
     181             :                 /* get block we are going to overwrite, if any */
     182      248714 :                 over_block = fs_par2block_get(disk, parity_pos);
     183             : 
     184             :                 /* deallocate it */
     185      248714 :                 if (over_block != BLOCK_EMPTY)
     186       93089 :                         fs_deallocate(disk, parity_pos);
     187             : 
     188             :                 /* get block specific info */
     189      248714 :                 info = info_get(&state->infoarr, parity_pos);
     190             : 
     191             :                 /* get the new block we are going to write */
     192      248714 :                 block = fs_file2block_get(file, i);
     193             : 
     194             :                 /* if the file block already has an updated hash without rehash */
     195      248714 :                 if (block_has_updated_hash(block) && !info_get_rehash(info)) {
     196             :                         /* the only possible case is for REP blocks */
     197       37269 :                         assert(block_state_get(block) == BLOCK_STATE_REP);
     198             : 
     199             :                         /* convert to a REP block */
     200       37269 :                         block_state_set(block, BLOCK_STATE_REP);
     201             : 
     202             :                         /* and keep the hash as it's */
     203             :                 } else {
     204             :                         unsigned over_state;
     205             : 
     206             :                         /* convert to a CHG block */
     207      211445 :                         block_state_set(block, BLOCK_STATE_CHG);
     208             : 
     209             :                         /* state of the block we are going to overwrite */
     210      211445 :                         over_state = block_state_get(over_block);
     211             : 
     212             :                         /* if the block is an empty one */
     213      211445 :                         if (over_state == BLOCK_STATE_EMPTY) {
     214             :                                 /* the block was empty and filled with zeros */
     215             :                                 /* set the hash to the special ZERO value */
     216      153288 :                                 hash_zero_set(block->hash);
     217             :                         } else {
     218             :                                 /* otherwise it's a DELETED one */
     219       58157 :                                 assert(over_state == BLOCK_STATE_DELETED);
     220             : 
     221             :                                 /* copy the past hash of the block */
     222       58157 :                                 memcpy(block->hash, over_block->hash, HASH_SIZE);
     223             : 
     224             :                                 /* if we have not already cleared the past hash */
     225       58157 :                                 if (!state->clear_past_hash) {
     226             :                                         /* in this case we don't know if the old state is still the one */
     227             :                                         /* stored inside the parity, because after an aborted sync, the parity */
     228             :                                         /* may be or may be not have been updated with the new data */
     229             :                                         /* Then we reset the hash to a bogus value */
     230             :                                         /* For example: */
     231             :                                         /* - One file is deleted */
     232             :                                         /* - Sync aborted after, updating the parity to the new state, */
     233             :                                         /*   but without saving the content file representing this new state. */
     234             :                                         /* - Another file is added again (exactly here) */
     235             :                                         /*   with the hash of DELETED block not represeting the real parity state */
     236         381 :                                         hash_invalid_set(block->hash);
     237             :                                 }
     238             :                         }
     239             :                 }
     240             : 
     241             :                 /* store in the disk map, after invalidating all the other blocks */
     242      248714 :                 fs_allocate(disk, parity_pos, file, i);
     243             : 
     244             :                 /* set the new free position */
     245      248714 :                 disk->first_free_block = parity_pos + 1;
     246             :         }
     247             : 
     248             :         /* insert in the list of contained files */
     249      100975 :         tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
     250      100975 : }
     251             : 
     252             : /**
     253             :  * Delete the specified file from the parity.
     254             :  *
     255             :  * Note that the parity remains allocated, but the blocks and the file are marked as DELETED.
     256             :  * The file is then inserted in the deleted set, and it should not be deallocated,
     257             :  * as the parity still references it.
     258             :  */
     259       56139 : static void scan_file_deallocate(struct snapraid_scan* scan, struct snapraid_file* file)
     260             : {
     261       56139 :         struct snapraid_state* state = scan->state;
     262       56139 :         struct snapraid_disk* disk = scan->disk;
     263             :         block_off_t i;
     264             : 
     265             :         /* remove from the list of contained files */
     266       56139 :         tommy_list_remove_existing(&disk->filelist, &file->nodelist);
     267             : 
     268             :         /* state changed */
     269       56139 :         state->need_write = 1;
     270             : 
     271             :         /* here we are supposed to adjust the ::first_free_block position */
     272             :         /* with the parity position we are deleting */
     273             :         /* but we also know that we do only delayed insert, after all the deletion, */
     274             :         /* so at this point ::first_free_block is always at 0, and we don't need to update it */
     275       56139 :         if (disk->first_free_block != 0) {
     276             :                 /* LCOV_EXCL_START */
     277             :                 log_fatal("Internal inconsistency for first free position at '%u' deallocating file '%s'\n", disk->first_free_block, file->sub);
     278             :                 exit(EXIT_FAILURE);
     279             :                 /* LCOV_EXCL_STOP */
     280             :         }
     281             : 
     282             :         /* free all the blocks of the file */
     283      194478 :         for (i = 0; i < file->blockmax; ++i) {
     284      138339 :                 struct snapraid_block* block = fs_file2block_get(file, i);
     285             :                 unsigned block_state;
     286             : 
     287             :                 /* in case we scan after an aborted sync, */
     288             :                 /* we could get also intermediate states */
     289      138339 :                 block_state = block_state_get(block);
     290      138339 :                 switch (block_state) {
     291             :                 case BLOCK_STATE_BLK :
     292             :                         /* we keep the hash making it an "old" hash, because the parity is still containing data for it */
     293       33544 :                         break;
     294             :                 case BLOCK_STATE_CHG :
     295             :                         /* if we have not already cleared the past hash */
     296       57596 :                         if (!state->clear_past_hash) {
     297             :                                 /* in these cases we don't know if the old state is still the one */
     298             :                                 /* stored inside the parity, because after an aborted sync, the parity */
     299             :                                 /* may be or may be not have been updated with the data that it's now */
     300             :                                 /* deleted. Then we reset the hash to a bogus value. */
     301             :                                 /* For example: */
     302             :                                 /* - One file is added */
     303             :                                 /* - Sync aborted after updating the parity to the new state, */
     304             :                                 /*   but without saving the content file representing this new state. */
     305             :                                 /* - File is now deleted after the aborted sync */
     306             :                                 /* - Sync again, deleting the blocks (exactly here) */
     307             :                                 /*   with the hash of CHG block not represeting the real parity state */
     308           0 :                                 hash_invalid_set(block->hash);
     309             :                         }
     310       57596 :                         break;
     311             :                 case BLOCK_STATE_REP :
     312             :                         /* we just don't know the old hash, and then we set it to invalid */
     313       47199 :                         hash_invalid_set(block->hash);
     314       47199 :                         break;
     315             :                 default :
     316             :                         /* LCOV_EXCL_START */
     317             :                         log_fatal("Internal inconsistency in file '%s' deallocating block '%u:%u' state %u\n", file->sub, i, file->blockmax, block_state);
     318             :                         exit(EXIT_FAILURE);
     319             :                         /* LCOV_EXCL_STOP */
     320             :                 }
     321             : 
     322             :                 /* set the block as deleted */
     323      138339 :                 block_state_set(block, BLOCK_STATE_DELETED);
     324             :         }
     325             : 
     326             :         /* mark the file as deleted */
     327       56139 :         file_flag_set(file, FILE_IS_DELETED);
     328             : 
     329             :         /* insert it in the list of deleted blocks */
     330       56139 :         tommy_list_insert_tail(&disk->deletedlist, &file->nodelist, file);
     331       56139 : }
     332             : 
     333      100975 : static void scan_file_delayed_allocate(struct snapraid_scan* scan, struct snapraid_file* file)
     334             : {
     335      100975 :         struct snapraid_state* state = scan->state;
     336      100975 :         struct snapraid_disk* disk = scan->disk;
     337             : 
     338             :         /* if we sort for physical offsets we have to read them for new files */
     339      100975 :         if (state->opt.force_order == SORT_PHYSICAL
     340       11432 :                 && file->physical == FILEPHY_UNREAD_OFFSET
     341             :         ) {
     342             :                 char path_next[PATH_MAX];
     343             : 
     344       11432 :                 pathprint(path_next, sizeof(path_next), "%s%s", disk->dir, file->sub);
     345             : 
     346       11432 :                 if (filephy(path_next, file->size, &file->physical) != 0) {
     347             :                         /* LCOV_EXCL_START */
     348             :                         log_fatal("Error in getting the physical offset of file '%s'. %s.\n", path_next, strerror(errno));
     349             :                         exit(EXIT_FAILURE);
     350             :                         /* LCOV_EXCL_STOP */
     351             :                 }
     352             :         }
     353             : 
     354             :         /* insert in the delayed list */
     355      100975 :         tommy_list_insert_tail(&scan->file_insert_list, &file->nodelist, file);
     356      100975 : }
     357             : 
     358             : /**
     359             :  * Check if a file is completely formed of blocks with invalid parity,
     360             :  * and no rehash is tagged, and if it has at least one block.
     361             :  */
     362      923721 : static int file_is_full_invalid_parity_and_stable(struct snapraid_state* state, struct snapraid_disk* disk, struct snapraid_file* file)
     363             : {
     364             :         block_off_t i;
     365             : 
     366             :         /* with no block, it never has an invalid parity */
     367      923721 :         if (file->blockmax == 0)
     368        1290 :                 return 0;
     369             : 
     370             :         /* check all blocks */
     371      976066 :         for (i = 0; i < file->blockmax; ++i) {
     372             :                 snapraid_info info;
     373      954281 :                 struct snapraid_block* block = fs_file2block_get(file, i);
     374             :                 block_off_t parity_pos;
     375             : 
     376             :                 /* exclude blocks with parity */
     377      954281 :                 if (!block_has_invalid_parity(block))
     378      900646 :                         return 0;
     379             : 
     380             :                 /*
     381             :                  * Get the parity position, but only after checking
     382             :                  * that the file has invalid parity.
     383             :                  * This is not really required, as we are sure
     384             :                  * that 'kept" files are mapped into parity.
     385             :                  */
     386       53635 :                 parity_pos = fs_file2par_get(disk, file, i);
     387             : 
     388             :                 /* get block specific info */
     389       53635 :                 info = info_get(&state->infoarr, parity_pos);
     390             : 
     391             :                 /* if rehash fails */
     392       53635 :                 if (info_get_rehash(info))
     393           0 :                         return 0;
     394             :         }
     395             : 
     396       21785 :         return 1;
     397             : }
     398             : 
     399             : /**
     400             :  * Check if a file is completely formed of blocks with an updated hash,
     401             :  * and no rehash is tagged, and if it has at least one block.
     402             :  */
     403       23659 : static int file_is_full_hashed_and_stable(struct snapraid_state* state, struct snapraid_disk* disk, struct snapraid_file* file)
     404             : {
     405             :         block_off_t i;
     406             : 
     407             :         /* with no block, it never has a hash */
     408       23659 :         if (file->blockmax == 0)
     409          27 :                 return 0;
     410             : 
     411             :         /* check all blocks */
     412       42158 :         for (i = 0; i < file->blockmax; ++i) {
     413             :                 snapraid_info info;
     414       35949 :                 struct snapraid_block* block = fs_file2block_get(file, i);
     415             :                 block_off_t parity_pos;
     416             : 
     417             :                 /* exclude blocks without hash */
     418       35949 :                 if (!block_has_updated_hash(block))
     419        1704 :                         return 0;
     420             : 
     421             :                 /*
     422             :                  * Get the parity position, but only AFTER checking
     423             :                  * that the file has an updated hash.
     424             :                  * This is required because not hashed files may be
     425             :                  * not yet mapped into parity.
     426             :                  *
     427             :                  * This happens when a new file is in the delayed insertion
     428             :                  * list, and scan finds a copy of it, and it tries to copy
     429             :                  * the hash info.
     430             :                  * As just allocated files have only CHG blocks, checking that
     431             :                  * the hash is updated is enough to exclude such cases.
     432             :                  */
     433       34245 :                 parity_pos = fs_file2par_get(disk, file, i);
     434             : 
     435             :                 /* get block specific info */
     436       34245 :                 info = info_get(&state->infoarr, parity_pos);
     437             : 
     438             :                 /* exclude blocks needing a rehash */
     439       34245 :                 if (info_get_rehash(info))
     440       15719 :                         return 0;
     441             :         }
     442             : 
     443        6209 :         return 1;
     444             : }
     445             : 
     446             : /**
     447             :  * Refresh the file info.
     448             :  *
     449             :  * This is needed by Windows as the normal way to list directories may report not
     450             :  * updated info. Only the GetFileInformationByHandle() func, called file-by-file,
     451             :  * really ensures to return synced info.
     452             :  *
     453             :  * If this happens, we read also the physical offset, to avoid to read it later.
     454             :  */
     455       79190 : static void scan_file_refresh(struct snapraid_scan* scan, const char* sub, struct stat* st, uint64_t* physical)
     456             : {
     457             : #if HAVE_LSTAT_SYNC
     458             :         struct snapraid_state* state = scan->state;
     459             :         struct snapraid_disk* disk = scan->disk;
     460             : 
     461             :         /* if the st_sync is not set, ensure to get synced info */
     462             :         if (st->st_sync == 0) {
     463             :                 char path_next[PATH_MAX];
     464             :                 struct stat synced_st;
     465             : 
     466             :                 pathprint(path_next, sizeof(path_next), "%s%s", disk->dir, sub);
     467             : 
     468             :                 /* if we sort for physical offsets we have to read them for new files */
     469             :                 if (state->opt.force_order == SORT_PHYSICAL
     470             :                         && *physical == FILEPHY_UNREAD_OFFSET
     471             :                 ) {
     472             :                         /* do nothing, leave the pointer to read the physical offset */
     473             :                 } else {
     474             :                         physical = 0; /* set the pointer to 0 to read nothing */
     475             :                 }
     476             : 
     477             :                 if (lstat_sync(path_next, &synced_st, physical) != 0) {
     478             :                         /* LCOV_EXCL_START */
     479             :                         log_fatal("Error in stat file '%s'. %s.\n", path_next, strerror(errno));
     480             :                         exit(EXIT_FAILURE);
     481             :                         /* LCOV_EXCL_STOP */
     482             :                 }
     483             : 
     484             :                 if (st->st_mtime != synced_st.st_mtime
     485             :                         || st->st_mtimensec != synced_st.st_mtimensec
     486             :                 ) {
     487             : #ifndef _WIN32
     488             :                         /*
     489             :                          * In Windows having different metadata is expected with open files
     490             :                          * because the metadata in the directory is updated only when the file
     491             :                          * is closed.
     492             :                          *
     493             :                          * See also:
     494             :                          * Why is the file size reported incorrectly for files that are still being written to?
     495             :                          * http://blogs.msdn.com/b/oldnewthing/archive/2011/12/26/10251026.aspx
     496             :                          */
     497             :                         log_fatal("WARNING! Detected uncached time change for file '%s'\n", sub);
     498             :                         log_fatal("It's better if you run SnapRAID without other processes running.\n");
     499             : #endif
     500             :                         st->st_mtime = synced_st.st_mtime;
     501             :                         st->st_mtimensec = synced_st.st_mtimensec;
     502             :                 }
     503             : 
     504             :                 if (st->st_size != synced_st.st_size) {
     505             : #ifndef _WIN32
     506             :                         log_fatal("WARNING! Detected uncached size change for file '%s'\n", sub);
     507             :                         log_fatal("It's better if you run SnapRAID without other processes running.\n");
     508             : #endif
     509             :                         st->st_size = synced_st.st_size;
     510             :                 }
     511             : 
     512             :                 if (st->st_ino != synced_st.st_ino) {
     513             :                         log_fatal("DANGER! Detected uncached inode change for file '%s'\n", sub);
     514             :                         log_fatal("Please ensure to run SnapRAID without other processes running.\n");
     515             :                         /* at this point, it's too late to change inode */
     516             :                         /* and having inconsistent inodes may result to internal failures */
     517             :                         /* so, it's better to abort */
     518             :                         exit(EXIT_FAILURE);
     519             :                 }
     520             :         }
     521             : #else
     522             :         (void)scan;
     523             :         (void)sub;
     524             :         (void)st;
     525             :         (void)physical;
     526             : #endif
     527       79190 : }
     528             : 
     529             : /**
     530             :  * Insert the file in the data set.
     531             :  */
     532      100975 : static void scan_file_insert(struct snapraid_scan* scan, struct snapraid_file* file)
     533             : {
     534      100975 :         struct snapraid_disk* disk = scan->disk;
     535             : 
     536             :         /* insert the file in the containers */
     537      100975 :         if (!file_flag_has(file, FILE_IS_WITHOUT_INODE))
     538      100975 :                 tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
     539      100975 :         tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
     540      100975 :         tommy_hashdyn_insert(&disk->stampset, &file->stampset, file, file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec));
     541             : 
     542             :         /* delayed allocation of the parity */
     543      100975 :         scan_file_delayed_allocate(scan, file);
     544      100975 : }
     545             : 
     546             : /**
     547             :  * Remove the file from the data set.
     548             :  *
     549             :  * File is then deleted.
     550             :  */
     551       56139 : static void scan_file_remove(struct snapraid_scan* scan, struct snapraid_file* file)
     552             : {
     553       56139 :         struct snapraid_disk* disk = scan->disk;
     554             : 
     555             :         /* remove the file from the containers */
     556       56139 :         if (!file_flag_has(file, FILE_IS_WITHOUT_INODE))
     557       56084 :                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
     558       56139 :         tommy_hashdyn_remove_existing(&disk->pathset, &file->pathset);
     559       56139 :         tommy_hashdyn_remove_existing(&disk->stampset, &file->stampset);
     560             : 
     561             :         /* deallocate the file from the parity */
     562       56139 :         scan_file_deallocate(scan, file);
     563       56139 : }
     564             : 
     565             : /**
     566             :  * Keep the file as it's (or with only a name/inode modification).
     567             :  *
     568             :  * If the file is kept, nothing has to be done.
     569             :  *
     570             :  * But if a file contains only blocks with invalid parity, it's reallocated to ensure
     571             :  * to always minimize the space used in the parity.
     572             :  *
     573             :  * This could happen after a failed sync, when some other files are deleted,
     574             :  * and then new ones can be moved backward to fill the hole created.
     575             :  */
     576      923721 : static void scan_file_keep(struct snapraid_scan* scan, struct snapraid_file* file)
     577             : {
     578      923721 :         struct snapraid_disk* disk = scan->disk;
     579             : 
     580             :         /* if the file is full invalid, schedule a reinsert at later stage */
     581      923721 :         if (file_is_full_invalid_parity_and_stable(scan->state, disk, file)) {
     582       21785 :                 struct snapraid_file* copy = file_dup(file);
     583             : 
     584             :                 /* remove the file */
     585       21785 :                 scan_file_remove(scan, file);
     586             : 
     587             :                 /* reinsert the copy in the delayed list */
     588       21785 :                 scan_file_insert(scan, copy);
     589             :         }
     590      923721 : }
     591             : 
     592             : /**
     593             :  * Process a file.
     594             :  */
     595     1003082 : static void scan_file(struct snapraid_scan* scan, int is_diff, const char* sub, struct stat* st, uint64_t physical)
     596             : {
     597     1003082 :         struct snapraid_state* state = scan->state;
     598     1003082 :         struct snapraid_disk* disk = scan->disk;
     599             :         struct snapraid_file* file;
     600             :         tommy_node* i;
     601             :         int is_original_file_size_different_than_zero;
     602             :         int is_file_already_present;
     603             :         int is_file_reported;
     604             : 
     605             :         /*
     606             :          * If the disk has persistent inodes and UUID, try a search on the past inodes,
     607             :          * to detect moved files.
     608             :          *
     609             :          * For persistent inodes we mean inodes that keep their values when the filesystem
     610             :          * is unmounted and remounted. This don't always happen.
     611             :          *
     612             :          * Cases found are:
     613             :          * - Linux FUSE with exFAT driver from https://code.google.com/p/exfat/.
     614             :          *   Inodes are reassigned at every mount restarting from 1 and incrementing.
     615             :          *   As worse, the exFAT support in FUSE doesn't use subsecond precision in timestamps
     616             :          *   making inode collision more easy (exFAT by design supports 10ms precision).
     617             :          * - Linux VFAT kernel (3.2) driver. Inodes are fully reassigned at every mount.
     618             :          *
     619             :          * In such cases, to avoid possible random collisions, it's better to disable the moved
     620             :          * file recognition.
     621             :          *
     622             :          * For persistent UUID we mean that it has the same UUID as before.
     623             :          * Otherwise, if the UUID is changed, likely it's a new recreated filesystem,
     624             :          * and then the inode have no meaning.
     625             :          *
     626             :          * Note that to disable the search by past inode, we do this implicitely
     627             :          * removing all the past inode before searching for files.
     628             :          * This ensures that no file is found with a past inode, but at the same time,
     629             :          * it allows to find new files with the same inode, to identify them as hardlinks.
     630             :          */
     631     1003082 :         int has_past_inodes = !disk->has_volatile_inodes && !disk->has_different_uuid && !disk->has_unsupported_uuid;
     632             : 
     633             :         /* always search with the new inode, in the all new inodes found until now, */
     634             :         /* with the eventual presence of also the past inodes */
     635     1003082 :         uint64_t inode = st->st_ino;
     636             : 
     637     1003082 :         file = tommy_hashdyn_search(&disk->inodeset, file_inode_compare_to_arg, &inode, file_inode_hash(inode));
     638             : 
     639             :         /* identify moved files with past inodes and hardlinks with the new inodes */
     640     1003082 :         if (file) {
     641             :                 /* check if the file is not changed */
     642      837733 :                 if (file->size == st->st_size
     643      826569 :                         && file->mtime_sec == st->st_mtime
     644      823506 :                         && (file->mtime_nsec == STAT_NSEC(st)
     645             :                         /* always accept the stored value if it's STAT_NSEC_INVALID */
     646             :                         /* it happens when upgrading from an old version of SnapRAID */
     647             :                         /* not yet supporting the nanosecond field */
     648           2 :                         || file->mtime_nsec == STAT_NSEC_INVALID
     649             :                         )
     650             :                 ) {
     651             :                         /* check if multiple files have the same inode */
     652      823504 :                         if (file_flag_has(file, FILE_IS_PRESENT)) {
     653             : #if HAVE_STRUCT_STAT_ST_NLINK
     654         171 :                                 if (st->st_nlink <= 1) {
     655             :                                         /* LCOV_EXCL_START */
     656             :                                         log_fatal("Internal inode '%" PRIu64 "' inconsistency for file '%s%s' already present\n", (uint64_t)st->st_ino, disk->dir, sub);
     657             :                                         exit(EXIT_FAILURE);
     658             :                                         /* LCOV_EXCL_STOP */
     659             :                                 }
     660             : #endif
     661             :                                 /* it's a hardlink */
     662         171 :                                 scan_link(scan, is_diff, sub, file->sub, FILE_IS_HARDLINK);
     663         171 :                                 return;
     664             :                         }
     665             : 
     666             :                         /* mark as present */
     667      823333 :                         file_flag_set(file, FILE_IS_PRESENT);
     668             : 
     669             :                         /* update the nanoseconds mtime only if different */
     670             :                         /* to avoid unneeded updates */
     671      823333 :                         if (file->mtime_nsec == STAT_NSEC_INVALID
     672           0 :                                 && STAT_NSEC(st) != file->mtime_nsec
     673             :                         ) {
     674           0 :                                 file->mtime_nsec = STAT_NSEC(st);
     675             : 
     676             :                                 /* we have to save the new mtime */
     677           0 :                                 state->need_write = 1;
     678             :                         }
     679             : 
     680      823333 :                         if (strcmp(file->sub, sub) != 0) {
     681             :                                 /* if the path is different, it means a moved file with the same inode */
     682         155 :                                 ++scan->count_move;
     683             : 
     684         155 :                                 log_tag("scan:move:%s:%s:%s\n", disk->name, esc(file->sub), esc(sub));
     685         155 :                                 if (is_diff) {
     686           0 :                                         printf("move %s%s -> %s%s\n", disk->dir, file->sub, disk->dir, sub);
     687             :                                 }
     688             : 
     689             :                                 /* remove from the name set */
     690         155 :                                 tommy_hashdyn_remove_existing(&disk->pathset, &file->pathset);
     691             : 
     692             :                                 /* save the new name */
     693         155 :                                 file_rename(file, sub);
     694             : 
     695             :                                 /* reinsert in the name set */
     696         155 :                                 tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
     697             : 
     698             :                                 /* we have to save the new name */
     699         155 :                                 state->need_write = 1;
     700             :                         } else {
     701             :                                 /* otherwise it's equal */
     702      823178 :                                 ++scan->count_equal;
     703             : 
     704      823178 :                                 if (state->opt.gui) {
     705       22559 :                                         log_tag("scan:equal:%s:%s\n", disk->name, esc(file->sub));
     706             :                                 }
     707             :                         }
     708             : 
     709             :                         /* mark the file as kept */
     710      823333 :                         scan_file_keep(scan, file);
     711             : 
     712             :                         /* nothing more to do */
     713      823333 :                         return;
     714             :                 }
     715             : 
     716             :                 /* here the file matches the inode, but not the other info */
     717             :                 /* if could be a modified file with the same name, */
     718             :                 /* or a restored/copied file that get assigned a previously used inode, */
     719             :                 /* or a filesystem with not persistent inodes */
     720             : 
     721             :                 /* for sure it cannot be already present */
     722       14229 :                 if (file_flag_has(file, FILE_IS_PRESENT)) {
     723             :                         /* LCOV_EXCL_START */
     724             :                         log_fatal("Internal inode '%" PRIu64 "' inconsistency for files '%s%s' and '%s%s' matching and already present but different\n", file->inode, disk->dir, sub, disk->dir, file->sub);
     725             :                         exit(EXIT_FAILURE);
     726             :                         /* LCOV_EXCL_STOP */
     727             :                 }
     728             : 
     729             :                 /* assume a previously used inode, it's the worst case */
     730             :                 /* and we handle it removing the duplicate stored inode. */
     731             :                 /* If the file is found by name (not necessarely in this function call), */
     732             :                 /* it will have the inode restored, otherwise, it will get removed */
     733             : 
     734             :                 /* remove from the inode set */
     735       14229 :                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
     736             : 
     737             :                 /* clear the inode */
     738             :                 /* this is not really needed for correct functionality */
     739             :                 /* because we are going to set FILE_IS_WITHOUT_INODE */
     740             :                 /* but it's easier for debugging to have invalid inodes set to 0 */
     741       14229 :                 file->inode = 0;
     742             : 
     743             :                 /* mark as missing inode */
     744       14229 :                 file_flag_set(file, FILE_IS_WITHOUT_INODE);
     745             : 
     746             :                 /* go further to find it by name */
     747             :         }
     748             : 
     749             :         /* initialize for later overwrite */
     750      179578 :         is_file_reported = 0;
     751      179578 :         is_original_file_size_different_than_zero = 0;
     752             : 
     753             :         /* then try finding it by name */
     754      179578 :         file = tommy_hashdyn_search(&disk->pathset, file_path_compare_to_arg, sub, file_path_hash(sub));
     755             : 
     756             :         /* keep track if the file already exists */
     757      179578 :         is_file_already_present = file != 0;
     758             : 
     759      179578 :         if (is_file_already_present) {
     760             :                 /* if the file is without an inode */
     761      105104 :                 if (file_flag_has(file, FILE_IS_WITHOUT_INODE)) {
     762             :                         /* set it now */
     763       14174 :                         file->inode = st->st_ino;
     764             : 
     765             :                         /* insert in the set */
     766       14174 :                         tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
     767             : 
     768             :                         /* unmark as missing inode */
     769       14174 :                         file_flag_clear(file, FILE_IS_WITHOUT_INODE);
     770             :                 } else {
     771             :                         /* here the inode has to be different, otherwise we would have found it before */
     772       90930 :                         if (file->inode == st->st_ino) {
     773             :                                 /* LCOV_EXCL_START */
     774             :                                 log_fatal("Internal inconsistency in inode '%" PRIu64 "' for files '%s%s' as unexpected matching\n", file->inode, disk->dir, sub);
     775             :                                 exit(EXIT_FAILURE);
     776             :                                 /* LCOV_EXCL_STOP */
     777             :                         }
     778             :                 }
     779             : 
     780             :                 /* for sure it cannot be already present */
     781      105104 :                 if (file_flag_has(file, FILE_IS_PRESENT)) {
     782             :                         /* LCOV_EXCL_START */
     783             :                         log_fatal("Internal inconsistency in path for file '%s%s' matching and already present\n", disk->dir, sub);
     784             :                         exit(EXIT_FAILURE);
     785             :                         /* LCOV_EXCL_STOP */
     786             :                 }
     787             : 
     788             :                 /* check if the file is not changed */
     789      105104 :                 if (file->size == st->st_size
     790      104510 :                         && file->mtime_sec == st->st_mtime
     791      100388 :                         && (file->mtime_nsec == STAT_NSEC(st)
     792             :                         /* always accept the stored value if it's STAT_NSEC_INVALID */
     793             :                         /* it happens when upgrading from an old version of SnapRAID */
     794             :                         /* not yet supporting the nanosecond field */
     795           0 :                         || file->mtime_nsec == STAT_NSEC_INVALID
     796             :                         )
     797             :                 ) {
     798             :                         /* mark as present */
     799      100388 :                         file_flag_set(file, FILE_IS_PRESENT);
     800             : 
     801             :                         /* update the nano seconds mtime only if different */
     802             :                         /* to avoid unneeded updates */
     803      100388 :                         if (file->mtime_nsec == STAT_NSEC_INVALID
     804           0 :                                 && STAT_NSEC(st) != STAT_NSEC_INVALID
     805             :                         ) {
     806           0 :                                 file->mtime_nsec = STAT_NSEC(st);
     807             : 
     808             :                                 /* we have to save the new mtime */
     809           0 :                                 state->need_write = 1;
     810             :                         }
     811             : 
     812             :                         /* if when processing the disk we used the past inodes values */
     813      100388 :                         if (has_past_inodes) {
     814             :                                 /* if persistent inodes are supported, we are sure that the inode number */
     815             :                                 /* is now different, because otherwise the file would have been found */
     816             :                                 /* when searching by inode. */
     817             :                                 /* if the inode is different, it means a rewritten file with the same path */
     818             :                                 /* like when restoring a backup that restores also the timestamp */
     819      100388 :                                 ++scan->count_restore;
     820             : 
     821      100388 :                                 log_tag("scan:restore:%s:%s\n", disk->name, esc(sub));
     822      100388 :                                 if (is_diff) {
     823           0 :                                         printf("restore %s%s\n", disk->dir, sub);
     824             :                                 }
     825             : 
     826             :                                 /* remove from the inode set */
     827      100388 :                                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
     828             : 
     829             :                                 /* save the new inode */
     830      100388 :                                 file->inode = st->st_ino;
     831             : 
     832             :                                 /* reinsert in the inode set */
     833      100388 :                                 tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
     834             : 
     835             :                                 /* we have to save the new inode */
     836      100388 :                                 state->need_write = 1;
     837             :                         } else {
     838             :                                 /* otherwise it's the case of not persistent inode, where doesn't */
     839             :                                 /* matter if the inode is different or equal, because they have no */
     840             :                                 /* meaning, and then we don't even save them */
     841           0 :                                 ++scan->count_equal;
     842             : 
     843           0 :                                 if (state->opt.gui) {
     844           0 :                                         log_tag("scan:equal:%s:%s\n", disk->name, esc(file->sub));
     845             :                                 }
     846             :                         }
     847             : 
     848             :                         /* mark the file as kept */
     849      100388 :                         scan_file_keep(scan, file);
     850             : 
     851             :                         /* nothing more to do */
     852      100388 :                         return;
     853             :                 }
     854             : 
     855             :                 /* here if the file is changed but with the correct name */
     856             : 
     857             :                 /* keep track if the original file was not of zero size */
     858        4716 :                 is_original_file_size_different_than_zero = file->size != 0;
     859             : 
     860             :                 /* remove it, and continue to insert it again */
     861        4716 :                 scan_file_remove(scan, file);
     862             : 
     863             :                 /* and continue to insert it again */
     864             :         }
     865             : 
     866             :         /* refresh the info, to ensure that they are synced, */
     867             :         /* note that we refresh only the info of the new or modified files */
     868             :         /* because this is slow operation */
     869       79190 :         scan_file_refresh(scan, sub, st, &physical);
     870             : 
     871             : #ifndef _WIN32
     872             :         /* do a safety check to ensure that the common ext4 case of zeroing */
     873             :         /* the size of a file after a crash doesn't propagate to the backup */
     874             :         /* this check is specific for Linux, so we disable it on Windows */
     875       79190 :         if (is_original_file_size_different_than_zero && st->st_size == 0) {
     876           0 :                 if (!state->opt.force_zero) {
     877             :                         /* LCOV_EXCL_START */
     878             :                         log_fatal("The file '%s%s' has unexpected zero size!\n", disk->dir, sub);
     879             :                         log_fatal("It's possible that after a kernel crash this file was lost,\n");
     880             :                         log_fatal("and you can use 'snapraid --filter %s fix' to recover it.\n", sub);
     881             :                         if (!is_diff) {
     882             :                                 log_fatal("If this an expected condition you can '%s' anyway using 'snapraid --force-zero %s'\n", state->command, state->command);
     883             :                                 exit(EXIT_FAILURE);
     884             :                         }
     885             :                         /* LCOV_EXCL_STOP */
     886             :                 }
     887             :         }
     888             : #else
     889             :         /* avoid the unused warning in Windows */
     890             :         (void)is_original_file_size_different_than_zero;
     891             : #endif
     892             : 
     893             :         /* insert it */
     894       79190 :         file = file_alloc(state->block_size, sub, st->st_size, st->st_mtime, STAT_NSEC(st), st->st_ino, physical);
     895             : 
     896             :         /* mark it as present */
     897       79190 :         file_flag_set(file, FILE_IS_PRESENT);
     898             : 
     899             :         /* if copy detection is enabled */
     900             :         /* note that the copy detection is tried also for updated files */
     901             :         /* this makes sense because it may happen to have two different copies */
     902             :         /* of the same file, and we move the right one over the wrong one */
     903             :         /* in such case we have a "copy" over an "update" */
     904       79190 :         if (!state->opt.force_nocopy) {
     905       79190 :                 tommy_uint32_t hash = file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec);
     906             : 
     907             :                 /* search for a file with the same name and stamp in all the disks */
     908      518611 :                 for (i = state->disklist; i != 0; i = i->next) {
     909      445630 :                         struct snapraid_disk* other_disk = i->data;
     910             :                         struct snapraid_file* other_file;
     911             : 
     912             :                         /* if the nanosecond part of the time stamp is valid, search */
     913             :                         /* for name and stamp, otherwise for path and stamp */
     914      445630 :                         if (file->mtime_nsec != 0 && file->mtime_nsec != STAT_NSEC_INVALID)
     915      445522 :                                 other_file = tommy_hashdyn_search(&other_disk->stampset, file_namestamp_compare, file, hash);
     916             :                         else
     917         108 :                                 other_file = tommy_hashdyn_search(&other_disk->stampset, file_pathstamp_compare, file, hash);
     918             : 
     919             :                         /* if found, and it's a fully hashed file */
     920      445630 :                         if (other_file && file_is_full_hashed_and_stable(scan->state, other_disk, other_file)) {
     921             :                                 /* assume that the file is a copy, and reuse the hash */
     922        6209 :                                 file_copy(other_file, file);
     923             : 
     924             :                                 /* revert old counter and use the copy one */
     925        6209 :                                 ++scan->count_copy;
     926             : 
     927        6209 :                                 log_tag("scan:copy:%s:%s:%s:%s\n", other_disk->name, esc(other_file->sub), disk->name, esc(file->sub));
     928        6209 :                                 if (is_diff) {
     929          31 :                                         printf("copy %s%s -> %s%s\n", other_disk->dir, other_file->sub, disk->dir, file->sub);
     930             :                                 }
     931             : 
     932             :                                 /* mark it as reported */
     933        6209 :                                 is_file_reported = 1;
     934             : 
     935             :                                 /* no need to continue the search */
     936        6209 :                                 break;
     937             :                         }
     938             :                 }
     939             :         }
     940             : 
     941             :         /* if not yet reported, do it now */
     942       79190 :         if (!is_file_reported) {
     943       72981 :                 if (is_file_already_present) {
     944        4716 :                         ++scan->count_change;
     945             : 
     946        4716 :                         log_tag("scan:update:%s:%s\n", disk->name, esc(sub));
     947        4716 :                         if (is_diff) {
     948         118 :                                 printf("update %s%s\n", disk->dir, sub);
     949             :                         }
     950             :                 } else {
     951       68265 :                         ++scan->count_insert;
     952             : 
     953       68265 :                         log_tag("scan:add:%s:%s\n", disk->name, esc(sub));
     954       68265 :                         if (is_diff) {
     955       17044 :                                 printf("add %s%s\n", disk->dir, sub);
     956             :                         }
     957             :                 }
     958             :         }
     959             : 
     960             :         /* insert the file in the delayed list */
     961       79190 :         scan_file_insert(scan, file);
     962             : }
     963             : 
     964             : /**
     965             :  * Remove the specified dir from the data set.
     966             :  */
     967           2 : static void scan_emptydir_remove(struct snapraid_scan* scan, struct snapraid_dir* dir)
     968             : {
     969           2 :         struct snapraid_state* state = scan->state;
     970           2 :         struct snapraid_disk* disk = scan->disk;
     971             : 
     972             :         /* state changed */
     973           2 :         state->need_write = 1;
     974             : 
     975             :         /* remove the file from the dir containers */
     976           2 :         tommy_hashdyn_remove_existing(&disk->dirset, &dir->nodeset);
     977           2 :         tommy_list_remove_existing(&disk->dirlist, &dir->nodelist);
     978             : 
     979             :         /* deallocate */
     980           2 :         dir_free(dir);
     981           2 : }
     982             : 
     983             : /**
     984             :  * Insert the specified dir in the data set.
     985             :  */
     986          10 : static void scan_emptydir_insert(struct snapraid_scan* scan, struct snapraid_dir* dir)
     987             : {
     988          10 :         struct snapraid_state* state = scan->state;
     989          10 :         struct snapraid_disk* disk = scan->disk;
     990             : 
     991             :         /* state changed */
     992          10 :         state->need_write = 1;
     993             : 
     994             :         /* insert the dir in the dir containers */
     995          10 :         tommy_hashdyn_insert(&disk->dirset, &dir->nodeset, dir, dir_name_hash(dir->sub));
     996          10 :         tommy_list_insert_tail(&disk->dirlist, &dir->nodelist, dir);
     997          10 : }
     998             : 
     999             : /**
    1000             :  * Process a dir.
    1001             :  */
    1002         227 : static void scan_emptydir(struct snapraid_scan* scan, const char* sub)
    1003             : {
    1004         227 :         struct snapraid_disk* disk = scan->disk;
    1005             :         struct snapraid_dir* dir;
    1006             : 
    1007             :         /* check if the dir already exists */
    1008         227 :         dir = tommy_hashdyn_search(&disk->dirset, dir_name_compare, sub, dir_name_hash(sub));
    1009         227 :         if (dir) {
    1010             :                 /* check if multiple files have the same name */
    1011         217 :                 if (dir_flag_has(dir, FILE_IS_PRESENT)) {
    1012             :                         /* LCOV_EXCL_START */
    1013             :                         log_fatal("Internal inconsistency for dir '%s%s'\n", disk->dir, sub);
    1014             :                         exit(EXIT_FAILURE);
    1015             :                         /* LCOV_EXCL_STOP */
    1016             :                 }
    1017             : 
    1018             :                 /* mark as present */
    1019         217 :                 dir_flag_set(dir, FILE_IS_PRESENT);
    1020             : 
    1021             :                 /* nothing more to do */
    1022         444 :                 return;
    1023             :         } else {
    1024             :                 /* and continue to insert it */
    1025             :         }
    1026             : 
    1027             :         /* insert it */
    1028          10 :         dir = dir_alloc(sub);
    1029             : 
    1030             :         /* mark it as present */
    1031          10 :         dir_flag_set(dir, FILE_IS_PRESENT);
    1032             : 
    1033             :         /* insert it in the delayed insert list */
    1034          10 :         tommy_list_insert_tail(&scan->dir_insert_list, &dir->nodelist, dir);
    1035             : }
    1036             : 
    1037             : struct dirent_sorted {
    1038             :         /* node for data structures */
    1039             :         tommy_node node;
    1040             : 
    1041             : #if HAVE_STRUCT_DIRENT_D_INO
    1042             :         uint64_t d_ino; /**< Inode number. */
    1043             : #endif
    1044             : #if HAVE_STRUCT_DIRENT_D_TYPE
    1045             :         uint32_t d_type; /**< File type. */
    1046             : #endif
    1047             : #if HAVE_STRUCT_DIRENT_D_STAT
    1048             :         struct stat d_stat; /**< Stat result. */
    1049             : #endif
    1050             :         char d_name[]; /**< Variable length name. It must be the last field. */
    1051             : };
    1052             : 
    1053             : #if HAVE_STRUCT_DIRENT_D_INO
    1054      103314 : static int dd_ino_compare(const void* void_a, const void* void_b)
    1055             : {
    1056      103314 :         const struct dirent_sorted* a = void_a;
    1057      103314 :         const struct dirent_sorted* b = void_b;
    1058             : 
    1059      103314 :         if (a->d_ino < b->d_ino)
    1060       49400 :                 return -1;
    1061       53914 :         if (a->d_ino > b->d_ino)
    1062       53914 :                 return 1;
    1063             : 
    1064           0 :         return 0;
    1065             : }
    1066             : #endif
    1067             : 
    1068    10485497 : static int dd_name_compare(const void* void_a, const void* void_b)
    1069             : {
    1070    10485497 :         const struct dirent_sorted* a = void_a;
    1071    10485497 :         const struct dirent_sorted* b = void_b;
    1072             : 
    1073    10485497 :         return strcmp(a->d_name, b->d_name);
    1074             : }
    1075             : 
    1076             : /**
    1077             :  * Return the stat info of a dir entry.
    1078             :  */
    1079             : #if HAVE_STRUCT_DIRENT_D_STAT
    1080             : #define DSTAT(file, dd, buf) dstat(dd)
    1081             : struct stat* dstat(struct dirent_sorted* dd)
    1082             : {
    1083             :         return &dd->d_stat;
    1084             : }
    1085             : #else
    1086             : #define DSTAT(file, dd, buf) dstat(file, buf)
    1087     1004217 : struct stat* dstat(const char* file, struct stat* st)
    1088             : {
    1089     1004217 :         if (lstat(file, st) != 0) {
    1090             :                 /* LCOV_EXCL_START */
    1091             :                 log_fatal("Error in stat file/directory '%s'. %s.\n", file, strerror(errno));
    1092             :                 exit(EXIT_FAILURE);
    1093             :                 /* LCOV_EXCL_STOP */
    1094             :         }
    1095     1004217 :         return st;
    1096             : }
    1097             : #endif
    1098             : 
    1099             : /**
    1100             :  * Process a directory.
    1101             :  * Return != 0 if at least one file or link is processed.
    1102             :  */
    1103        1614 : static int scan_dir(struct snapraid_scan* scan, int level, int is_diff, const char* dir, const char* sub)
    1104             : {
    1105        1614 :         struct snapraid_state* state = scan->state;
    1106        1614 :         struct snapraid_disk* disk = scan->disk;
    1107        1614 :         int processed = 0;
    1108             :         DIR* d;
    1109             :         tommy_list list;
    1110             :         tommy_node* node;
    1111             : 
    1112        1614 :         tommy_list_init(&list);
    1113             : 
    1114        1614 :         d = opendir(dir);
    1115        1614 :         if (!d) {
    1116             :                 /* LCOV_EXCL_START */
    1117             :                 log_fatal("Error opening directory '%s'. %s.\n", dir, strerror(errno));
    1118             :                 if (level == 0)
    1119             :                         log_fatal("If this is the disk mount point, remember to create it manually\n");
    1120             :                 else
    1121             :                         log_fatal("If it's a permission problem, you can exclude it in the config file with:\n\texclude /%s\n", sub);
    1122             :                 exit(EXIT_FAILURE);
    1123             :                 /* LCOV_EXCL_STOP */
    1124             :         }
    1125             : 
    1126             :         /* read the full directory */
    1127             :         while (1) {
    1128             :                 char path_next[PATH_MAX];
    1129             :                 char sub_next[PATH_MAX];
    1130             :                 struct dirent_sorted* entry;
    1131             :                 const char* name;
    1132             :                 struct dirent* dd;
    1133             :                 size_t name_len;
    1134             : 
    1135             :                 /* clear errno to detect erroneous conditions */
    1136     1041725 :                 errno = 0;
    1137     1041725 :                 dd = readdir(d);
    1138     1041725 :                 if (dd == 0 && errno != 0) {
    1139             :                         /* LCOV_EXCL_START */
    1140             :                         log_fatal("Error reading directory '%s'. %s.\n", dir, strerror(errno));
    1141             :                         log_fatal("You can exclude it in the config file with:\n\texclude /%s\n", sub);
    1142             :                         exit(EXIT_FAILURE);
    1143             :                         /* LCOV_EXCL_STOP */
    1144             :                 }
    1145     1041725 :                 if (dd == 0) {
    1146        1614 :                         break; /* finished */
    1147             :                 }
    1148             : 
    1149             :                 /* skip "." and ".." files */
    1150     1040111 :                 name = dd->d_name;
    1151     1040111 :                 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
    1152        6456 :                         continue;
    1153             : 
    1154     1036883 :                 pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
    1155     1036883 :                 pathprint(sub_next, sizeof(sub_next), "%s%s", sub, name);
    1156             : 
    1157             :                 /* check for not supported file names */
    1158     1036883 :                 if (name[0] == 0) {
    1159             :                         /* LCOV_EXCL_START */
    1160             :                         log_fatal("Unsupported name '%s' in file '%s'.\n", name, path_next);
    1161             :                         exit(EXIT_FAILURE);
    1162             :                         /* LCOV_EXCL_STOP */
    1163             :                 }
    1164             : 
    1165             :                 /* exclude hidden files even before calling lstat() */
    1166     1036883 :                 if (filter_hidden(state->filter_hidden, dd) != 0) {
    1167           0 :                         msg_verbose("Excluding hidden '%s'\n", path_next);
    1168           0 :                         continue;
    1169             :                 }
    1170             : 
    1171             :                 /* exclude content files even before calling lstat() */
    1172     1036883 :                 if (filter_content(&state->contentlist, path_next) != 0) {
    1173           0 :                         msg_verbose("Excluding content '%s'\n", path_next);
    1174           0 :                         continue;
    1175             :                 }
    1176             : 
    1177     1036883 :                 name_len = strlen(dd->d_name);
    1178     1036883 :                 entry = malloc_nofail(sizeof(struct dirent_sorted) + name_len + 1);
    1179             : 
    1180             :                 /* copy the dir entry */
    1181             : #if HAVE_STRUCT_DIRENT_D_INO
    1182     1036883 :                 entry->d_ino = dd->d_ino;
    1183             : #endif
    1184             : #if HAVE_STRUCT_DIRENT_D_TYPE
    1185     1036883 :                 entry->d_type = dd->d_type;
    1186             : #endif
    1187             : #if HAVE_STRUCT_DIRENT_D_STAT
    1188             :                 /* convert dirent to lstat result */
    1189             :                 dirent_lstat(dd, &entry->d_stat);
    1190             : 
    1191             :                 /* note that at this point the st_mode may be 0 */
    1192             : #endif
    1193     1036883 :                 memcpy(entry->d_name, dd->d_name, name_len + 1);
    1194             : 
    1195             :                 /* insert in the list */
    1196     1036883 :                 tommy_list_insert_tail(&list, &entry->node, entry);
    1197     1040111 :         }
    1198             : 
    1199        1614 :         if (closedir(d) != 0) {
    1200             :                 /* LCOV_EXCL_START */
    1201             :                 log_fatal("Error closing directory '%s'. %s.\n", dir, strerror(errno));
    1202             :                 exit(EXIT_FAILURE);
    1203             :                 /* LCOV_EXCL_STOP */
    1204             :         }
    1205             : 
    1206        1614 :         if (state->opt.force_order == SORT_ALPHA) {
    1207             :                 /* if requested sort alphabetically */
    1208             :                 /* this is mainly done for testing to ensure to always */
    1209             :                 /* process in the same way in different platforms */
    1210        1578 :                 tommy_list_sort(&list, dd_name_compare);
    1211             :         }
    1212             : #if HAVE_STRUCT_DIRENT_D_INO
    1213          36 :         else if (!disk->has_volatile_inodes) {
    1214             :                 /* if inodes are persistent */
    1215             :                 /* sort the list of dir entries by inodes */
    1216          36 :                 tommy_list_sort(&list, dd_ino_compare);
    1217             :         }
    1218             :         /* otherwise just keep the insertion order */
    1219             : #endif
    1220             : 
    1221             :         /* process the sorted dir entries */
    1222        1614 :         node = list;
    1223     1040111 :         while (node != 0) {
    1224             :                 char path_next[PATH_MAX];
    1225             :                 char sub_next[PATH_MAX];
    1226             :                 char out[PATH_MAX];
    1227     1036883 :                 struct snapraid_filter* reason = 0;
    1228     1036883 :                 struct dirent_sorted* dd = node->data;
    1229     1036883 :                 const char* name = dd->d_name;
    1230             :                 struct stat* st;
    1231             :                 int type;
    1232             : #if !HAVE_STRUCT_DIRENT_D_STAT
    1233             :                 struct stat st_buf;
    1234             : #endif
    1235             : 
    1236     1036883 :                 pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
    1237     1036883 :                 pathprint(sub_next, sizeof(sub_next), "%s%s", sub, name);
    1238             : 
    1239             :                 /* start with an unknown type */
    1240     1036883 :                 type = -1;
    1241     1036883 :                 st = 0;
    1242             : 
    1243             :                 /* if dirent has the type, use it */
    1244             : #if HAVE_STRUCT_DIRENT_D_TYPE
    1245     1036883 :                 switch (dd->d_type) {
    1246           0 :                 case DT_UNKNOWN : break;
    1247     1003082 :                 case DT_REG : type = 0; break;
    1248       32666 :                 case DT_LNK : type = 1; break;
    1249        1135 :                 case DT_DIR : type = 2; break;
    1250           0 :                 default : type = 3; break;
    1251             :                 }
    1252             : #endif
    1253             : 
    1254             :                 /* if type is still unknown */
    1255     1036883 :                 if (type < 0) {
    1256             :                         /* get the type from stat */
    1257           0 :                         st = DSTAT(path_next, dd, &st_buf);
    1258             : 
    1259             : #if HAVE_STRUCT_DIRENT_D_STAT
    1260             :                         /* if the st_mode field is missing, takes care to fill it using normal lstat() */
    1261             :                         /* at now this can happen only in Windows (with HAVE_STRUCT_DIRENT_D_STAT defined), */
    1262             :                         /* because we use a directory reading method that doesn't read info about ReparsePoint. */
    1263             :                         /* Note that here we cannot call here lstat_sync(), because we don't know what kind */
    1264             :                         /* of file is it, and lstat_sync() doesn't always work */
    1265             :                         if (st->st_mode == 0) {
    1266             :                                 if (lstat(path_next, st) != 0) {
    1267             :                                         /* LCOV_EXCL_START */
    1268             :                                         log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
    1269             :                                         exit(EXIT_FAILURE);
    1270             :                                         /* LCOV_EXCL_STOP */
    1271             :                                 }
    1272             :                         }
    1273             : #endif
    1274             : 
    1275           0 :                         if (S_ISREG(st->st_mode))
    1276           0 :                                 type = 0;
    1277           0 :                         else if (S_ISLNK(st->st_mode))
    1278           0 :                                 type = 1;
    1279           0 :                         else if (S_ISDIR(st->st_mode))
    1280           0 :                                 type = 2;
    1281             :                         else
    1282           0 :                                 type = 3;
    1283             :                 }
    1284             : 
    1285     1036883 :                 if (type == 0) { /* REG */
    1286     1003082 :                         if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1287             : 
    1288             :                                 /* late stat, if not yet called */
    1289     1003082 :                                 if (!st)
    1290     1003082 :                                         st = DSTAT(path_next, dd, &st_buf);
    1291             : 
    1292             : #if HAVE_LSTAT_SYNC
    1293             :                                 /* if the st_ino field is missing, takes care to fill it using the extended lstat() */
    1294             :                                 /* this can happen only in Windows */
    1295             :                                 if (st->st_ino == 0) {
    1296             :                                         if (lstat_sync(path_next, st, 0) != 0) {
    1297             :                                                 /* LCOV_EXCL_START */
    1298             :                                                 log_fatal("Error in stat file '%s'. %s.\n", path_next, strerror(errno));
    1299             :                                                 exit(EXIT_FAILURE);
    1300             :                                                 /* LCOV_EXCL_STOP */
    1301             :                                         }
    1302             :                                 }
    1303             : #endif
    1304             : 
    1305     1003082 :                                 scan_file(scan, is_diff, sub_next, st, FILEPHY_UNREAD_OFFSET);
    1306     1003082 :                                 processed = 1;
    1307             :                         } else {
    1308           0 :                                 msg_verbose("Excluding file '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1309             :                         }
    1310       33801 :                 } else if (type == 1) { /* LNK */
    1311       32666 :                         if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1312             :                                 char subnew[PATH_MAX];
    1313             :                                 int ret;
    1314             : 
    1315       32666 :                                 ret = readlink(path_next, subnew, sizeof(subnew));
    1316       32666 :                                 if (ret >= PATH_MAX) {
    1317             :                                         /* LCOV_EXCL_START */
    1318             :                                         log_fatal("Error in readlink file '%s'. Symlink too long.\n", path_next);
    1319             :                                         exit(EXIT_FAILURE);
    1320             :                                         /* LCOV_EXCL_STOP */
    1321             :                                 }
    1322       32666 :                                 if (ret < 0) {
    1323             :                                         /* LCOV_EXCL_START */
    1324             :                                         log_fatal("Error in readlink file '%s'. %s.\n", path_next, strerror(errno));
    1325             :                                         exit(EXIT_FAILURE);
    1326             :                                         /* LCOV_EXCL_STOP */
    1327             :                                 }
    1328             : 
    1329             :                                 /* readlink doesn't put the final 0 */
    1330       32666 :                                 subnew[ret] = 0;
    1331             : 
    1332             :                                 /* process as a symbolic link */
    1333       32666 :                                 scan_link(scan, is_diff, sub_next, subnew, FILE_IS_SYMLINK);
    1334       32666 :                                 processed = 1;
    1335             :                         } else {
    1336           0 :                                 msg_verbose("Excluding link '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1337             :                         }
    1338        1135 :                 } else if (type == 2) { /* DIR */
    1339        1135 :                         if (filter_dir(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1340             : #ifndef _WIN32
    1341             :                                 /* late stat, if not yet called */
    1342        1135 :                                 if (!st)
    1343        1135 :                                         st = DSTAT(path_next, dd, &st_buf);
    1344             : 
    1345             :                                 /* in Unix don't follow mount points in different devices */
    1346             :                                 /* in Windows we are already skipping them reporting them as special files */
    1347        1135 :                                 if ((uint64_t)st->st_dev != disk->device) {
    1348           0 :                                         log_fatal("WARNING! Ignoring mount point '%s' because it appears to be in a different device\n", path_next);
    1349             :                                 } else
    1350             : #endif
    1351             :                                 {
    1352             :                                         char sub_dir[PATH_MAX];
    1353             : 
    1354             :                                         /* recurse */
    1355        1135 :                                         pathslash(path_next, sizeof(path_next));
    1356        1135 :                                         pathcpy(sub_dir, sizeof(sub_dir), sub_next);
    1357        1135 :                                         pathslash(sub_dir, sizeof(sub_dir));
    1358        1135 :                                         if (scan_dir(scan, level + 1, is_diff, path_next, sub_dir) == 0) {
    1359             :                                                 /* scan the directory as empty dir */
    1360         227 :                                                 scan_emptydir(scan, sub_next);
    1361             :                                         }
    1362             :                                         /* or we processed something internally, or we have added the empty dir */
    1363        1135 :                                         processed = 1;
    1364             :                                 }
    1365             :                         } else {
    1366           0 :                                 msg_verbose("Excluding directory '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1367             :                         }
    1368             :                 } else {
    1369           0 :                         if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1370             :                                 /* late stat, if not yet called */
    1371           0 :                                 if (!st)
    1372           0 :                                         st = DSTAT(path_next, dd, &st_buf);
    1373             : 
    1374           0 :                                 log_fatal("WARNING! Ignoring special '%s' file '%s'\n", stat_desc(st), path_next);
    1375             :                         } else {
    1376           0 :                                 msg_verbose("Excluding special file '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1377             :                         }
    1378             :                 }
    1379             : 
    1380             :                 /* next entry */
    1381     1036883 :                 node = node->next;
    1382             : 
    1383             :                 /* free the present one */
    1384     1036883 :                 free(dd);
    1385             :         }
    1386             : 
    1387        1614 :         return processed;
    1388             : }
    1389             : 
    1390          80 : static int state_diffscan(struct snapraid_state* state, int is_diff)
    1391             : {
    1392             :         tommy_node* i;
    1393             :         tommy_node* j;
    1394             :         tommy_list scanlist;
    1395             :         int done;
    1396             :         fptr* msg;
    1397             :         struct snapraid_scan total;
    1398             :         int no_difference;
    1399             : 
    1400          80 :         tommy_list_init(&scanlist);
    1401             : 
    1402          80 :         if (is_diff)
    1403           7 :                 msg_progress("Comparing...\n");
    1404             : 
    1405             :         /* first scan all the directory and find new and deleted files */
    1406         559 :         for (i = state->disklist; i != 0; i = i->next) {
    1407         479 :                 struct snapraid_disk* disk = i->data;
    1408             :                 struct snapraid_scan* scan;
    1409             :                 tommy_node* node;
    1410             :                 int ret;
    1411             :                 int has_persistent_inode;
    1412             : 
    1413         479 :                 scan = malloc_nofail(sizeof(struct snapraid_scan));
    1414         479 :                 scan->state = state;
    1415         479 :                 scan->disk = disk;
    1416         479 :                 scan->count_equal = 0;
    1417         479 :                 scan->count_move = 0;
    1418         479 :                 scan->count_copy = 0;
    1419         479 :                 scan->count_restore = 0;
    1420         479 :                 scan->count_change = 0;
    1421         479 :                 scan->count_remove = 0;
    1422         479 :                 scan->count_insert = 0;
    1423         479 :                 tommy_list_init(&scan->file_insert_list);
    1424         479 :                 tommy_list_init(&scan->link_insert_list);
    1425         479 :                 tommy_list_init(&scan->dir_insert_list);
    1426             : 
    1427         479 :                 tommy_list_insert_tail(&scanlist, &scan->node, scan);
    1428             : 
    1429         479 :                 if (!is_diff)
    1430         437 :                         msg_progress("Scanning disk %s...\n", disk->name);
    1431             : 
    1432             :                 /* check if the disk supports persistent inodes */
    1433         479 :                 ret = fsinfo(disk->dir, &has_persistent_inode, 0, 0);
    1434         479 :                 if (ret < 0) {
    1435             :                         /* LCOV_EXCL_START */
    1436             :                         log_fatal("Error accessing disk '%s' to get filesystem info. %s.\n", disk->dir, strerror(errno));
    1437             :                         exit(EXIT_FAILURE);
    1438             :                         /* LCOV_EXCL_STOP */
    1439             :                 }
    1440         479 :                 if (!has_persistent_inode) {
    1441           0 :                         disk->has_volatile_inodes = 1;
    1442             :                 }
    1443             : 
    1444             :                 /* if inodes or UUID are not persistent/changed/unsupported */
    1445         479 :                 if (disk->has_volatile_inodes || disk->has_different_uuid || disk->has_unsupported_uuid) {
    1446             :                         /* remove all the inodes from the inode collection */
    1447             :                         /* if they are not persistent, all of them could be changed now */
    1448             :                         /* and we don't want to find false matching ones */
    1449             :                         /* see scan_file() for more details */
    1450          75 :                         node = disk->filelist;
    1451         150 :                         while (node) {
    1452           0 :                                 struct snapraid_file* file = node->data;
    1453             : 
    1454           0 :                                 node = node->next;
    1455             : 
    1456             :                                 /* remove from the inode set */
    1457           0 :                                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
    1458             : 
    1459             :                                 /* clear the inode */
    1460           0 :                                 file->inode = 0;
    1461             : 
    1462             :                                 /* mark as missing inode */
    1463           0 :                                 file_flag_set(file, FILE_IS_WITHOUT_INODE);
    1464             :                         }
    1465             :                 }
    1466             : 
    1467         479 :                 scan_dir(scan, 0, is_diff, disk->dir, "");
    1468             :         }
    1469             : 
    1470             :         /* we split the search in two phases because to detect files */
    1471             :         /* moved from one disk to another we have to start deletion */
    1472             :         /* only when all disks have all the new files found */
    1473             : 
    1474             :         /* now process all the new and deleted files */
    1475         559 :         for (i = scanlist; i != 0; i = i->next) {
    1476         479 :                 struct snapraid_scan* scan = i->data;
    1477         479 :                 struct snapraid_disk* disk = scan->disk;
    1478             :                 tommy_node* node;
    1479             :                 unsigned phy_count;
    1480             :                 unsigned phy_dup;
    1481             :                 uint64_t phy_last;
    1482             :                 struct snapraid_file* phy_file_last;
    1483             : 
    1484             :                 /* check for removed files */
    1485         479 :                 node = disk->filelist;
    1486      932532 :                 while (node) {
    1487      931574 :                         struct snapraid_file* file = node->data;
    1488             : 
    1489             :                         /* next node */
    1490      931574 :                         node = node->next;
    1491             : 
    1492             :                         /* remove if not present */
    1493      931574 :                         if (!file_flag_has(file, FILE_IS_PRESENT)) {
    1494       29638 :                                 ++scan->count_remove;
    1495             : 
    1496       29638 :                                 log_tag("scan:remove:%s:%s\n", disk->name, esc(file->sub));
    1497       29638 :                                 if (is_diff) {
    1498          97 :                                         printf("remove %s%s\n", disk->dir, file->sub);
    1499             :                                 }
    1500             : 
    1501       29638 :                                 scan_file_remove(scan, file);
    1502             :                         }
    1503             :                 }
    1504             : 
    1505             :                 /* check for removed links */
    1506         479 :                 node = disk->linklist;
    1507       32294 :                 while (node) {
    1508       31336 :                         struct snapraid_link* slink = node->data;
    1509             : 
    1510             :                         /* next node */
    1511       31336 :                         node = node->next;
    1512             : 
    1513             :                         /* remove if not present */
    1514       31336 :                         if (!link_flag_has(slink, FILE_IS_PRESENT)) {
    1515         956 :                                 ++scan->count_remove;
    1516             : 
    1517         956 :                                 log_tag("scan:remove:%s:%s\n", disk->name, esc(slink->sub));
    1518         956 :                                 if (is_diff) {
    1519          10 :                                         printf("remove %s%s\n", disk->dir, slink->sub);
    1520             :                                 }
    1521             : 
    1522         956 :                                 scan_link_remove(scan, slink);
    1523             :                         }
    1524             :                 }
    1525             : 
    1526             :                 /* check for removed dirs */
    1527         479 :                 node = disk->dirlist;
    1528        1177 :                 while (node) {
    1529         219 :                         struct snapraid_dir* dir = node->data;
    1530             : 
    1531             :                         /* next node */
    1532         219 :                         node = node->next;
    1533             : 
    1534             :                         /* remove if not present */
    1535         219 :                         if (!dir_flag_has(dir, FILE_IS_PRESENT)) {
    1536           2 :                                 scan_emptydir_remove(scan, dir);
    1537             :                         }
    1538             :                 }
    1539             : 
    1540             :                 /* sort the files before inserting them */
    1541             :                 /* we use a stable sort to ensure that if the reported physical offset/inode */
    1542             :                 /* are always 0, we keep at least the directory order */
    1543         479 :                 switch (state->opt.force_order) {
    1544             :                 case SORT_PHYSICAL :
    1545          12 :                         tommy_list_sort(&scan->file_insert_list, file_physical_compare);
    1546          12 :                         break;
    1547             :                 case SORT_INODE :
    1548           0 :                         tommy_list_sort(&scan->file_insert_list, file_inode_compare);
    1549           0 :                         break;
    1550             :                 case SORT_ALPHA :
    1551         467 :                         tommy_list_sort(&scan->file_insert_list, file_path_compare);
    1552         467 :                         break;
    1553             :                 case SORT_DIR :
    1554             :                         /* already in order */
    1555           0 :                         break;
    1556             :                 }
    1557             : 
    1558             :                 /* insert all the new files, we insert them only after the deletion */
    1559             :                 /* to reuse the just freed space */
    1560             :                 /* also check if the physical offset reported are fakes or not */
    1561         479 :                 node = scan->file_insert_list;
    1562         479 :                 phy_count = 0;
    1563         479 :                 phy_dup = 0;
    1564         479 :                 phy_last = FILEPHY_UNREAD_OFFSET;
    1565         479 :                 phy_file_last = 0;
    1566      101933 :                 while (node) {
    1567      100975 :                         struct snapraid_file* file = node->data;
    1568             : 
    1569             :                         /* if the file is not empty, count duplicate physical offsets */
    1570      100975 :                         if (state->opt.force_order == SORT_PHYSICAL && file->size != 0) {
    1571       11418 :                                 if (phy_file_last != 0 && file->physical == phy_last
    1572             :                                         /* files without offset are expected to have duplicates */
    1573           0 :                                         && phy_last != FILEPHY_WITHOUT_OFFSET
    1574             :                                 ) {
    1575             :                                         /* if verbose, print the list of duplicates real offsets */
    1576             :                                         /* other cases are for offsets not supported, so we don't need to report them file by file */
    1577           0 :                                         if (phy_last >= FILEPHY_REAL_OFFSET) {
    1578           0 :                                                 log_fatal("WARNING! Files '%s%s' and '%s%s' have the same physical offset %" PRId64 ".\n", disk->dir, phy_file_last->sub, disk->dir, file->sub, phy_last);
    1579             :                                         }
    1580           0 :                                         ++phy_dup;
    1581             :                                 }
    1582       11418 :                                 phy_file_last = file;
    1583       11418 :                                 phy_last = file->physical;
    1584       11418 :                                 ++phy_count;
    1585             :                         }
    1586             : 
    1587             :                         /* next node */
    1588      100975 :                         node = node->next;
    1589             : 
    1590             :                         /* insert in the parity */
    1591      100975 :                         scan_file_allocate(scan, file);
    1592             :                 }
    1593             : 
    1594             :                 /* mark the disk without reliable physical offset if it has duplicates */
    1595             :                 /* here it should never happen because we already sorted out hardlinks */
    1596         479 :                 if (state->opt.force_order == SORT_PHYSICAL && phy_dup > 0) {
    1597           0 :                         disk->has_unreliable_physical = 1;
    1598             :                 }
    1599             : 
    1600             :                 /* insert all the new links */
    1601         479 :                 node = scan->link_insert_list;
    1602        3415 :                 while (node) {
    1603        2457 :                         struct snapraid_link* slink = node->data;
    1604             : 
    1605             :                         /* next node */
    1606        2457 :                         node = node->next;
    1607             : 
    1608             :                         /* insert it */
    1609        2457 :                         scan_link_insert(scan, slink);
    1610             :                 }
    1611             : 
    1612             :                 /* insert all the new dirs */
    1613         479 :                 node = scan->dir_insert_list;
    1614         968 :                 while (node) {
    1615          10 :                         struct snapraid_dir* dir = node->data;
    1616             : 
    1617             :                         /* next node */
    1618          10 :                         node = node->next;
    1619             : 
    1620             :                         /* insert it */
    1621          10 :                         scan_emptydir_insert(scan, dir);
    1622             :                 }
    1623             :         }
    1624             : 
    1625             :         /* check for disks where all the previously existing files where removed */
    1626          80 :         if (!state->opt.force_empty) {
    1627          75 :                 int all_missing = 0;
    1628          75 :                 int all_rewritten = 0;
    1629          75 :                 done = 0;
    1630         524 :                 for (i = state->disklist, j = scanlist; i != 0; i = i->next, j = j->next) {
    1631         449 :                         struct snapraid_disk* disk = i->data;
    1632         449 :                         struct snapraid_scan* scan = j->data;
    1633             : 
    1634         449 :                         if (scan->count_equal == 0
    1635          60 :                                 && scan->count_move == 0
    1636          60 :                                 && scan->count_restore == 0
    1637          60 :                                 && (scan->count_remove != 0 || scan->count_change != 0)
    1638             :                         ) {
    1639           1 :                                 if (!done) {
    1640           1 :                                         done = 1;
    1641           1 :                                         log_fatal("WARNING! All the files previously present in disk '%s' at dir '%s'", disk->name, disk->dir);
    1642             :                                 } else {
    1643           0 :                                         log_fatal(", disk '%s' at dir '%s'", disk->name, disk->dir);
    1644             :                                 }
    1645             : 
    1646             :                                 /* detect the special condition of all files missing */
    1647           1 :                                 if (scan->count_change == 0)
    1648           1 :                                         all_missing = 1;
    1649             : 
    1650             :                                 /* detect the special condition of all files rewritten */
    1651           1 :                                 if (scan->count_remove == 0)
    1652           0 :                                         all_rewritten = 1;
    1653             :                         }
    1654             :                 }
    1655          75 :                 if (done) {
    1656           1 :                         log_fatal("\nare now missing or rewritten!\n");
    1657           1 :                         if (all_rewritten) {
    1658           0 :                                 log_fatal("This could happen when restoring a disk with a backup\n");
    1659           0 :                                 log_fatal("program that is not setting correctly the timestamps.\n");
    1660             :                         }
    1661           1 :                         if (all_missing) {
    1662           1 :                                 log_fatal("This could happen when some disks are not mounted\n");
    1663           1 :                                 log_fatal("in the expected directory.\n");
    1664             :                         }
    1665           1 :                         if (!is_diff) {
    1666           1 :                                 log_fatal("If you want to '%s' anyway, use 'snapraid --force-empty %s'.", state->command, state->command);
    1667           1 :                                 exit(EXIT_FAILURE);
    1668             :                         }
    1669             :                 }
    1670             :         }
    1671             : 
    1672             :         /* check for disks without the physical offset support */
    1673          79 :         if (state->opt.force_order == SORT_PHYSICAL) {
    1674           2 :                 done = 0;
    1675          14 :                 for (i = state->disklist; i != 0; i = i->next) {
    1676          12 :                         struct snapraid_disk* disk = i->data;
    1677             : 
    1678          12 :                         if (disk->has_unreliable_physical) {
    1679           0 :                                 if (!done) {
    1680           0 :                                         done = 1;
    1681           0 :                                         log_fatal("WARNING! Physical offsets not supported for disk '%s'", disk->name);
    1682             :                                 } else {
    1683           0 :                                         log_fatal(", '%s'", disk->name);
    1684             :                                 }
    1685             :                         }
    1686             :                 }
    1687           2 :                 if (done) {
    1688           0 :                         log_fatal(". Files order won't be optimal.\n");
    1689             :                 }
    1690             :         }
    1691             : 
    1692             :         /* check for disks without persistent inodes */
    1693          79 :         done = 0;
    1694         552 :         for (i = state->disklist; i != 0; i = i->next) {
    1695         473 :                 struct snapraid_disk* disk = i->data;
    1696             : 
    1697         473 :                 if (disk->has_volatile_inodes) {
    1698           0 :                         if (!done) {
    1699           0 :                                 done = 1;
    1700           0 :                                 log_fatal("WARNING! Inodes are not persistent for disks: '%s'", disk->name);
    1701             :                         } else {
    1702           0 :                                 log_fatal(", '%s'", disk->name);
    1703             :                         }
    1704             :                 }
    1705             :         }
    1706          79 :         if (done) {
    1707           0 :                 log_fatal(". Move operations won't be optimal.\n");
    1708             :         }
    1709             : 
    1710             :         /* check for disks with changed UUID */
    1711          79 :         done = 0;
    1712         552 :         for (i = state->disklist; i != 0; i = i->next) {
    1713         473 :                 struct snapraid_disk* disk = i->data;
    1714             : 
    1715             :                 /* don't print the message if the UUID changed because before */
    1716             :                 /* it was no set. */
    1717             :                 /* this is the normal condition for an empty disk because it */
    1718             :                 /* isn't stored */
    1719         473 :                 if (disk->has_different_uuid && !disk->had_empty_uuid) {
    1720           0 :                         if (!done) {
    1721           0 :                                 done = 1;
    1722           0 :                                 log_fatal("WARNING! UUID is changed for disks: '%s'", disk->name);
    1723             :                         } else {
    1724           0 :                                 log_fatal(", '%s'", disk->name);
    1725             :                         }
    1726             :                 }
    1727             :         }
    1728          79 :         if (done) {
    1729           0 :                 log_fatal(". Move operations won't be optimal.\n");
    1730             :         }
    1731             : 
    1732             :         /* check for disks with unsupported UUID */
    1733          79 :         done = 0;
    1734         552 :         for (i = state->disklist; i != 0; i = i->next) {
    1735         473 :                 struct snapraid_disk* disk = i->data;
    1736             : 
    1737         473 :                 if (disk->has_unsupported_uuid) {
    1738           0 :                         if (!done) {
    1739           0 :                                 done = 1;
    1740           0 :                                 log_fatal("WARNING! UUID is unsupported for disks: '%s'", disk->name);
    1741             :                         } else {
    1742           0 :                                 log_fatal(", '%s'", disk->name);
    1743             :                         }
    1744             :                 }
    1745             :         }
    1746          79 :         if (done) {
    1747           0 :                 log_fatal(". Move operations won't be optimal.\n");
    1748             :         }
    1749             : 
    1750          79 :         total.count_equal = 0;
    1751          79 :         total.count_move = 0;
    1752          79 :         total.count_copy = 0;
    1753          79 :         total.count_restore = 0;
    1754          79 :         total.count_change = 0;
    1755          79 :         total.count_remove = 0;
    1756          79 :         total.count_insert = 0;
    1757             : 
    1758         552 :         for (i = scanlist; i != 0; i = i->next) {
    1759         473 :                 struct snapraid_scan* scan = i->data;
    1760         473 :                 total.count_equal += scan->count_equal;
    1761         473 :                 total.count_move += scan->count_move;
    1762         473 :                 total.count_copy += scan->count_copy;
    1763         473 :                 total.count_restore += scan->count_restore;
    1764         473 :                 total.count_change += scan->count_change;
    1765         473 :                 total.count_remove += scan->count_remove;
    1766         473 :                 total.count_insert += scan->count_insert;
    1767             :         }
    1768             : 
    1769          79 :         if (is_diff) {
    1770           7 :                 msg_status("\n");
    1771           7 :                 msg = msg_status;
    1772             :         } else {
    1773          72 :                 msg = msg_verbose;
    1774             :         }
    1775             : 
    1776          79 :         msg("%8u equal\n", total.count_equal);
    1777          79 :         msg("%8u moved\n", total.count_move);
    1778          79 :         msg("%8u copied\n", total.count_copy);
    1779          79 :         msg("%8u restored\n", total.count_restore);
    1780          79 :         msg("%8u updated\n", total.count_change);
    1781          79 :         msg("%8u removed\n", total.count_remove);
    1782          79 :         msg("%8u added\n", total.count_insert);
    1783             : 
    1784          79 :         log_tag("summary:equal:%u\n", total.count_equal);
    1785          79 :         log_tag("summary:moved:%u\n", total.count_move);
    1786          79 :         log_tag("summary:copied:%u\n", total.count_copy);
    1787          79 :         log_tag("summary:restored:%u\n", total.count_restore);
    1788          79 :         log_tag("summary:updated:%u\n", total.count_change);
    1789          79 :         log_tag("summary:removed:%u\n", total.count_remove);
    1790          79 :         log_tag("summary:added:%u\n", total.count_insert);
    1791             : 
    1792         236 :         no_difference = !total.count_move && !total.count_copy && !total.count_restore
    1793         129 :                 && !total.count_change && !total.count_remove && !total.count_insert;
    1794             : 
    1795          79 :         if (is_diff) {
    1796           7 :                 if (no_difference) {
    1797           1 :                         msg_status("No differences\n");
    1798             :                 } else {
    1799           6 :                         msg_status("There are differences!\n");
    1800             :                 }
    1801             :         }
    1802             : 
    1803          79 :         if (no_difference) {
    1804           9 :                 log_tag("summary:exit:equal\n");
    1805             :         } else {
    1806          70 :                 log_tag("summary:exit:diff\n");
    1807             :         }
    1808          79 :         log_flush();
    1809             : 
    1810          79 :         tommy_list_foreach(&scanlist, (tommy_foreach_func*)free);
    1811             : 
    1812             :         /* check the filesystem on all disks */
    1813          79 :         state_fscheck(state, "after scan");
    1814             : 
    1815          79 :         if (is_diff) {
    1816             :                 /* check for file difference */
    1817           7 :                 if (!no_difference)
    1818           6 :                         return 1;
    1819             : 
    1820             :                 /* check also for incomplete "sync" */
    1821           1 :                 if (parity_is_invalid(state))
    1822           0 :                         return 1;
    1823             :         }
    1824             : 
    1825          73 :         return 0;
    1826             : }
    1827             : 
    1828           7 : int state_diff(struct snapraid_state* state)
    1829             : {
    1830           7 :         return state_diffscan(state, 1);
    1831             : }
    1832             : 
    1833          73 : void state_scan(struct snapraid_state* state)
    1834             : {
    1835          73 :         (void)state_diffscan(state, 0); /* ignore return value */
    1836          72 : }
    1837             : 

Generated by: LCOV version 1.10