LCOV - code coverage report
Current view: top level - cmdline - scan.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 554 622 89.1 %
Date: 2016-01-09 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       34294 : static void scan_link(struct snapraid_scan* scan, int is_diff, const char* sub, const char* linkto, unsigned link_flag)
      87             : {
      88       34294 :         struct snapraid_state* state = scan->state;
      89       34294 :         struct snapraid_disk* disk = scan->disk;
      90             :         struct snapraid_link* slink;
      91             : 
      92             :         /* check if the link already exists */
      93       34294 :         slink = tommy_hashdyn_search(&disk->linkset, link_name_compare_to_arg, sub, link_name_hash(sub));
      94       34294 :         if (slink) {
      95             :                 /* check if multiple files have the same name */
      96       31837 :                 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             :                         os_abort();
     100             :                         /* LCOV_EXCL_STOP */
     101             :                 }
     102             : 
     103             :                 /* mark as present */
     104       31837 :                 link_flag_set(slink, FILE_IS_PRESENT);
     105             : 
     106             :                 /* check if the link is not changed and it's of the same kind */
     107       31837 :                 if (strcmp(slink->linkto, linkto) == 0 && link_flag == link_flag_get(slink, FILE_IS_LINK_MASK)) {
     108             :                         /* it's equal */
     109       31831 :                         ++scan->count_equal;
     110             : 
     111       63662 :                         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       66131 :                 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      103840 : static void scan_file_allocate(struct snapraid_scan* scan, struct snapraid_file* file)
     161             : {
     162      103840 :         struct snapraid_state* state = scan->state;
     163      103840 :         struct snapraid_disk* disk = scan->disk;
     164             :         block_off_t i;
     165             :         block_off_t parity_pos;
     166             : 
     167             :         /* state changed */
     168      103840 :         state->need_write = 1;
     169             : 
     170             :         /* allocate the blocks of the file */
     171      103840 :         parity_pos = disk->first_free_block;
     172      355546 :         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     1072060 :                 while (block_has_file(fs_par2block_get(disk, parity_pos)))
     179      568648 :                         ++parity_pos;
     180             : 
     181             :                 /* get block we are going to overwrite, if any */
     182      251706 :                 over_block = fs_par2block_get(disk, parity_pos);
     183             : 
     184             :                 /* deallocate it */
     185      251706 :                 if (over_block != BLOCK_EMPTY)
     186       95873 :                         fs_deallocate(disk, parity_pos);
     187             : 
     188             :                 /* get block specific info */
     189      251706 :                 info = info_get(&state->infoarr, parity_pos);
     190             : 
     191             :                 /* get the new block we are going to write */
     192      251706 :                 block = fs_file2block_get(file, i);
     193             : 
     194             :                 /* if the file block already has an updated hash without rehash */
     195      251706 :                 if (block_has_updated_hash(block) && !info_get_rehash(info)) {
     196             :                         /* the only possible case is for REP blocks */
     197       86941 :                         assert(block_state_get(block) == BLOCK_STATE_REP);
     198             : 
     199             :                         /* convert to a REP block */
     200       86941 :                         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      164765 :                         block_state_set(block, BLOCK_STATE_CHG);
     208             : 
     209             :                         /* state of the block we are going to overwrite */
     210      164765 :                         over_state = block_state_get(over_block);
     211             : 
     212             :                         /* if the block is an empty one */
     213      164765 :                         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      115487 :                                 hash_zero_set(block->hash);
     217             :                         } else {
     218             :                                 /* otherwise it's a DELETED one */
     219       49278 :                                 assert(over_state == BLOCK_STATE_DELETED);
     220             : 
     221             :                                 /* copy the past hash of the block */
     222       49278 :                                 memcpy(block->hash, over_block->hash, HASH_SIZE);
     223             : 
     224             :                                 /* if we have not already cleared the past hash */
     225       49278 :                                 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 representing 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      251706 :                 fs_allocate(disk, parity_pos, file, i);
     243             : 
     244             :                 /* set the new free position */
     245      251706 :                 disk->first_free_block = parity_pos + 1;
     246             :         }
     247             : 
     248             :         /* insert in the list of contained files */
     249      103840 :         tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
     250      103840 : }
     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       58983 : static void scan_file_deallocate(struct snapraid_scan* scan, struct snapraid_file* file)
     260             : {
     261       58983 :         struct snapraid_state* state = scan->state;
     262       58983 :         struct snapraid_disk* disk = scan->disk;
     263             :         block_off_t i;
     264             : 
     265             :         /* remove from the list of contained files */
     266       58983 :         tommy_list_remove_existing(&disk->filelist, &file->nodelist);
     267             : 
     268             :         /* state changed */
     269       58983 :         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       58983 :         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             :                 os_abort();
     279             :                 /* LCOV_EXCL_STOP */
     280             :         }
     281             : 
     282             :         /* free all the blocks of the file */
     283      200146 :         for (i = 0; i < file->blockmax; ++i) {
     284      141163 :                 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      141163 :                 block_state = block_state_get(block);
     290      141163 :                 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       33358 :                         break;
     294             :                 case BLOCK_STATE_CHG :
     295             :                         /* if we have not already cleared the past hash */
     296       28000 :                         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 representing the real parity state */
     308           0 :                                 hash_invalid_set(block->hash);
     309             :                         }
     310       28000 :                         break;
     311             :                 case BLOCK_STATE_REP :
     312             :                         /* we just don't know the old hash, and then we set it to invalid */
     313       79805 :                         hash_invalid_set(block->hash);
     314       79805 :                         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             :                         os_abort();
     319             :                         /* LCOV_EXCL_STOP */
     320             :                 }
     321             : 
     322             :                 /* set the block as deleted */
     323      141163 :                 block_state_set(block, BLOCK_STATE_DELETED);
     324             :         }
     325             : 
     326             :         /* mark the file as deleted */
     327       58983 :         file_flag_set(file, FILE_IS_DELETED);
     328             : 
     329             :         /* insert it in the list of deleted blocks */
     330       58983 :         tommy_list_insert_tail(&disk->deletedlist, &file->nodelist, file);
     331       58983 : }
     332             : 
     333      103840 : static void scan_file_delayed_allocate(struct snapraid_scan* scan, struct snapraid_file* file)
     334             : {
     335      103840 :         struct snapraid_state* state = scan->state;
     336      103840 :         struct snapraid_disk* disk = scan->disk;
     337             : 
     338             :         /* if we sort for physical offsets we have to read them for new files */
     339      103840 :         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      103840 :         tommy_list_insert_tail(&scan->file_insert_list, &file->nodelist, file);
     356      103840 : }
     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      968997 : 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      968997 :         if (file->blockmax == 0)
     368        1354 :                 return 0;
     369             : 
     370             :         /* check all blocks */
     371     1032530 :         for (i = 0; i < file->blockmax; ++i) {
     372             :                 snapraid_info info;
     373     1007823 :                 struct snapraid_block* block = fs_file2block_get(file, i);
     374             :                 block_off_t parity_pos;
     375             : 
     376             :                 /* exclude blocks with parity */
     377     1007823 :                 if (!block_has_invalid_parity(block))
     378      923696 :                         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       84127 :                 parity_pos = fs_file2par_get(disk, file, i);
     387             : 
     388             :                 /* get block specific info */
     389       84127 :                 info = info_get(&state->infoarr, parity_pos);
     390             : 
     391             :                 /* if rehash fails */
     392       84127 :                 if (info_get_rehash(info))
     393       19240 :                         return 0;
     394             :         }
     395             : 
     396       24707 :         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       23661 : 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       23661 :         if (file->blockmax == 0)
     409          27 :                 return 0;
     410             : 
     411             :         /* check all blocks */
     412       81853 :         for (i = 0; i < file->blockmax; ++i) {
     413             :                 snapraid_info info;
     414       58220 :                 struct snapraid_block* block = fs_file2block_get(file, i);
     415             :                 block_off_t parity_pos;
     416             : 
     417             :                 /* exclude blocks without hash */
     418       58220 :                 if (!block_has_updated_hash(block))
     419           1 :                         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       58219 :                 parity_pos = fs_file2par_get(disk, file, i);
     434             : 
     435             :                 /* get block specific info */
     436       58219 :                 info = info_get(&state->infoarr, parity_pos);
     437             : 
     438             :                 /* exclude blocks needing a rehash */
     439       58219 :                 if (info_get_rehash(info))
     440           0 :                         return 0;
     441             :         }
     442             : 
     443       23633 :         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       79133 : 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       79133 : }
     528             : 
     529             : /**
     530             :  * Insert the file in the data set.
     531             :  */
     532      103840 : static void scan_file_insert(struct snapraid_scan* scan, struct snapraid_file* file)
     533             : {
     534      103840 :         struct snapraid_disk* disk = scan->disk;
     535             : 
     536             :         /* insert the file in the containers */
     537      103840 :         if (!file_flag_has(file, FILE_IS_WITHOUT_INODE))
     538      103840 :                 tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
     539      103840 :         tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
     540      103840 :         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      103840 :         scan_file_delayed_allocate(scan, file);
     544      103840 : }
     545             : 
     546             : /**
     547             :  * Remove the file from the data set.
     548             :  *
     549             :  * File is then deleted.
     550             :  */
     551       58983 : static void scan_file_remove(struct snapraid_scan* scan, struct snapraid_file* file)
     552             : {
     553       58983 :         struct snapraid_disk* disk = scan->disk;
     554             : 
     555             :         /* remove the file from the containers */
     556       58983 :         if (!file_flag_has(file, FILE_IS_WITHOUT_INODE))
     557       58942 :                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
     558       58983 :         tommy_hashdyn_remove_existing(&disk->pathset, &file->pathset);
     559       58983 :         tommy_hashdyn_remove_existing(&disk->stampset, &file->stampset);
     560             : 
     561             :         /* deallocate the file from the parity */
     562       58983 :         scan_file_deallocate(scan, file);
     563       58983 : }
     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      968997 : static void scan_file_keep(struct snapraid_scan* scan, struct snapraid_file* file)
     577             : {
     578      968997 :         struct snapraid_disk* disk = scan->disk;
     579             : 
     580             :         /* if the file is full invalid, schedule a reinsert at later stage */
     581      968997 :         if (file_is_full_invalid_parity_and_stable(scan->state, disk, file)) {
     582       24707 :                 struct snapraid_file* copy = file_dup(file);
     583             : 
     584             :                 /* remove the file */
     585       24707 :                 scan_file_remove(scan, file);
     586             : 
     587             :                 /* reinsert the copy in the delayed list */
     588       24707 :                 scan_file_insert(scan, copy);
     589             :         }
     590      968997 : }
     591             : 
     592             : /**
     593             :  * Process a file.
     594             :  */
     595     1048310 : static void scan_file(struct snapraid_scan* scan, int is_diff, const char* sub, struct stat* st, uint64_t physical)
     596             : {
     597     1048310 :         struct snapraid_state* state = scan->state;
     598     1048310 :         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 file-system
     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 file-system,
     624             :          * and then the inode have no meaning.
     625             :          *
     626             :          * Note that to disable the search by past inode, we do this implicitly
     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     1048310 :         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     1048310 :         uint64_t inode = st->st_ino;
     636             : 
     637     1048310 :         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     1048310 :         if (file) {
     641             :                 /* check if the file is not changed */
     642      889551 :                 if (file->size == st->st_size
     643      871917 :                         && file->mtime_sec == st->st_mtime
     644      868905 :                         && (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           6 :                         || file->mtime_nsec == STAT_NSEC_INVALID
     649             :                         )
     650             :                 ) {
     651             :                         /* check if multiple files have the same inode */
     652      868899 :                         if (file_flag_has(file, FILE_IS_PRESENT)) {
     653             : #if HAVE_STRUCT_STAT_ST_NLINK
     654         180 :                                 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             :                                         os_abort();
     658             :                                         /* LCOV_EXCL_STOP */
     659             :                                 }
     660             : #endif
     661             :                                 /* it's a hardlink */
     662         180 :                                 scan_link(scan, is_diff, sub, file->sub, FILE_IS_HARDLINK);
     663         180 :                                 return;
     664             :                         }
     665             : 
     666             :                         /* mark as present */
     667      868719 :                         file_flag_set(file, FILE_IS_PRESENT);
     668             : 
     669             :                         /* update the nanoseconds mtime only if different */
     670             :                         /* to avoid unneeded updates */
     671      868719 :                         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      868719 :                         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      868564 :                                 ++scan->count_equal;
     703             : 
     704      868564 :                                 if (state->opt.gui) {
     705       22587 :                                         log_tag("scan:equal:%s:%s\n", disk->name, esc(file->sub));
     706             :                                 }
     707             :                         }
     708             : 
     709             :                         /* mark the file as kept */
     710      868719 :                         scan_file_keep(scan, file);
     711             : 
     712             :                         /* nothing more to do */
     713      868719 :                         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 file-system with not persistent inodes */
     720             : 
     721             :                 /* for sure it cannot be already present */
     722       20652 :                 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             :                         os_abort();
     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 necessarily in this function call), */
     732             :                 /* it will have the inode restored, otherwise, it will get removed */
     733             : 
     734             :                 /* remove from the inode set */
     735       20652 :                 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       20652 :                 file->inode = 0;
     742             : 
     743             :                 /* mark as missing inode */
     744       20652 :                 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      179411 :         is_file_reported = 0;
     751      179411 :         is_original_file_size_different_than_zero = 0;
     752             : 
     753             :         /* then try finding it by name */
     754      179411 :         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      179411 :         is_file_already_present = file != 0;
     758             : 
     759      179411 :         if (is_file_already_present) {
     760             :                 /* if the file is without an inode */
     761      104908 :                 if (file_flag_has(file, FILE_IS_WITHOUT_INODE)) {
     762             :                         /* set it now */
     763       20621 :                         file->inode = st->st_ino;
     764             : 
     765             :                         /* insert in the set */
     766       20621 :                         tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
     767             : 
     768             :                         /* unmark as missing inode */
     769       20621 :                         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       84287 :                         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             :                                 os_abort();
     776             :                                 /* LCOV_EXCL_STOP */
     777             :                         }
     778             :                 }
     779             : 
     780             :                 /* for sure it cannot be already present */
     781      104908 :                 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             :                         os_abort();
     785             :                         /* LCOV_EXCL_STOP */
     786             :                 }
     787             : 
     788             :                 /* check if the file is not changed */
     789      104908 :                 if (file->size == st->st_size
     790      104314 :                         && file->mtime_sec == st->st_mtime
     791      100278 :                         && (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      100278 :                         file_flag_set(file, FILE_IS_PRESENT);
     800             : 
     801             :                         /* update the nano seconds mtime only if different */
     802             :                         /* to avoid unneeded updates */
     803      100278 :                         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      100278 :                         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      100268 :                                 ++scan->count_restore;
     820             : 
     821      100268 :                                 log_tag("scan:restore:%s:%s\n", disk->name, esc(sub));
     822      100268 :                                 if (is_diff) {
     823           0 :                                         printf("restore %s%s\n", disk->dir, sub);
     824             :                                 }
     825             : 
     826             :                                 /* remove from the inode set */
     827      100268 :                                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
     828             : 
     829             :                                 /* save the new inode */
     830      100268 :                                 file->inode = st->st_ino;
     831             : 
     832             :                                 /* reinsert in the inode set */
     833      100268 :                                 tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
     834             : 
     835             :                                 /* we have to save the new inode */
     836      100268 :                                 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          10 :                                 ++scan->count_equal;
     842             : 
     843          10 :                                 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      100278 :                         scan_file_keep(scan, file);
     850             : 
     851             :                         /* nothing more to do */
     852      100278 :                         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        4630 :                 is_original_file_size_different_than_zero = file->size != 0;
     859             : 
     860             :                 /* remove it, and continue to insert it again */
     861        4630 :                 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       79133 :         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       79133 :         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       79133 :         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       79133 :         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       79133 :         if (!state->opt.force_nocopy) {
     905       79133 :                 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      422996 :                 for (i = state->disklist; i != 0; i = i->next) {
     909      367496 :                         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      367496 :                         if (file->mtime_nsec != 0 && file->mtime_nsec != STAT_NSEC_INVALID)
     915      367388 :                                 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      367496 :                         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       23633 :                                 file_copy(other_file, file);
     923             : 
     924             :                                 /* revert old counter and use the copy one */
     925       23633 :                                 ++scan->count_copy;
     926             : 
     927       23633 :                                 log_tag("scan:copy:%s:%s:%s:%s\n", other_disk->name, esc(other_file->sub), disk->name, esc(file->sub));
     928       23633 :                                 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       23633 :                                 is_file_reported = 1;
     934             : 
     935             :                                 /* no need to continue the search */
     936       23633 :                                 break;
     937             :                         }
     938             :                 }
     939             :         }
     940             : 
     941             :         /* if not yet reported, do it now */
     942       79133 :         if (!is_file_reported) {
     943       55500 :                 if (is_file_already_present) {
     944        4630 :                         ++scan->count_change;
     945             : 
     946        4630 :                         log_tag("scan:update:%s:%s\n", disk->name, esc(sub));
     947        4630 :                         if (is_diff) {
     948         118 :                                 printf("update %s%s\n", disk->dir, sub);
     949             :                         }
     950             :                 } else {
     951       50870 :                         ++scan->count_insert;
     952             : 
     953       50870 :                         log_tag("scan:add:%s:%s\n", disk->name, esc(sub));
     954       50870 :                         if (is_diff) {
     955       17051 :                                 printf("add %s%s\n", disk->dir, sub);
     956             :                         }
     957             :                 }
     958             :         }
     959             : 
     960             :         /* insert the file in the delayed list */
     961       79133 :         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         239 : static void scan_emptydir(struct snapraid_scan* scan, const char* sub)
    1003             : {
    1004         239 :         struct snapraid_disk* disk = scan->disk;
    1005             :         struct snapraid_dir* dir;
    1006             : 
    1007             :         /* check if the dir already exists */
    1008         239 :         dir = tommy_hashdyn_search(&disk->dirset, dir_name_compare, sub, dir_name_hash(sub));
    1009         239 :         if (dir) {
    1010             :                 /* check if multiple files have the same name */
    1011         229 :                 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             :                         os_abort();
    1015             :                         /* LCOV_EXCL_STOP */
    1016             :                 }
    1017             : 
    1018             :                 /* mark as present */
    1019         229 :                 dir_flag_set(dir, FILE_IS_PRESENT);
    1020             : 
    1021             :                 /* nothing more to do */
    1022         468 :                 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      103366 : static int dd_ino_compare(const void* void_a, const void* void_b)
    1055             : {
    1056      103366 :         const struct dirent_sorted* a = void_a;
    1057      103366 :         const struct dirent_sorted* b = void_b;
    1058             : 
    1059      103366 :         if (a->d_ino < b->d_ino)
    1060       49430 :                 return -1;
    1061       53936 :         if (a->d_ino > b->d_ino)
    1062       53936 :                 return 1;
    1063             : 
    1064           0 :         return 0;
    1065             : }
    1066             : #endif
    1067             : 
    1068    10959090 : static int dd_name_compare(const void* void_a, const void* void_b)
    1069             : {
    1070    10959090 :         const struct dirent_sorted* a = void_a;
    1071    10959090 :         const struct dirent_sorted* b = void_b;
    1072             : 
    1073    10959090 :         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     1049496 : struct stat* dstat(const char* file, struct stat* st)
    1088             : {
    1089     1049496 :         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     1049496 :         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        1713 : static int scan_dir(struct snapraid_scan* scan, int level, int is_diff, const char* dir, const char* sub)
    1104             : {
    1105        1713 :         struct snapraid_state* state = scan->state;
    1106        1713 :         struct snapraid_disk* disk = scan->disk;
    1107        1713 :         int processed = 0;
    1108             :         DIR* d;
    1109             :         tommy_list list;
    1110             :         tommy_node* node;
    1111             : 
    1112        1713 :         tommy_list_init(&list);
    1113             : 
    1114        1713 :         d = opendir(dir);
    1115        1713 :         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     1088749 :                 errno = 0;
    1137     1088749 :                 dd = readdir(d);
    1138     1088749 :                 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     1088749 :                 if (dd == 0) {
    1146        1713 :                         break; /* finished */
    1147             :                 }
    1148             : 
    1149             :                 /* skip "." and ".." files */
    1150     1087036 :                 name = dd->d_name;
    1151     1087036 :                 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
    1152        6852 :                         continue;
    1153             : 
    1154     1083610 :                 pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
    1155     1083610 :                 pathprint(sub_next, sizeof(sub_next), "%s%s", sub, name);
    1156             : 
    1157             :                 /* check for not supported file names */
    1158     1083610 :                 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     1083610 :                 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     1083610 :                 if (filter_content(&state->contentlist, path_next) != 0) {
    1173           0 :                         msg_verbose("Excluding content '%s'\n", path_next);
    1174           0 :                         continue;
    1175             :                 }
    1176             : 
    1177     1083610 :                 name_len = strlen(dd->d_name);
    1178     1083610 :                 entry = malloc_nofail(sizeof(struct dirent_sorted) + name_len + 1);
    1179             : 
    1180             :                 /* copy the dir entry */
    1181             : #if HAVE_STRUCT_DIRENT_D_INO
    1182     1083610 :                 entry->d_ino = dd->d_ino;
    1183             : #endif
    1184             : #if HAVE_STRUCT_DIRENT_D_TYPE
    1185     1083610 :                 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     1083610 :                 memcpy(entry->d_name, dd->d_name, name_len + 1);
    1194             : 
    1195             :                 /* insert in the list */
    1196     1083610 :                 tommy_list_insert_tail(&list, &entry->node, entry);
    1197     1087036 :         }
    1198             : 
    1199        1713 :         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        1713 :         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        1677 :                 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        1713 :         node = list;
    1223     1087036 :         while (node != 0) {
    1224             :                 char path_next[PATH_MAX];
    1225             :                 char sub_next[PATH_MAX];
    1226             :                 char out[PATH_MAX];
    1227     1083610 :                 struct snapraid_filter* reason = 0;
    1228     1083610 :                 struct dirent_sorted* dd = node->data;
    1229     1083610 :                 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     1083610 :                 pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
    1237     1083610 :                 pathprint(sub_next, sizeof(sub_next), "%s%s", sub, name);
    1238             : 
    1239             :                 /* start with an unknown type */
    1240     1083610 :                 type = -1;
    1241     1083610 :                 st = 0;
    1242             : 
    1243             :                 /* if dirent has the type, use it */
    1244             : #if HAVE_STRUCT_DIRENT_D_TYPE
    1245     1083610 :                 switch (dd->d_type) {
    1246           0 :                 case DT_UNKNOWN : break;
    1247     1048310 :                 case DT_REG : type = 0; break;
    1248       34114 :                 case DT_LNK : type = 1; break;
    1249        1186 :                 case DT_DIR : type = 2; break;
    1250           0 :                 default : type = 3; break;
    1251             :                 }
    1252             : #endif
    1253             : 
    1254             :                 /* if type is still unknown */
    1255     1083610 :                 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     1083610 :                 if (type == 0) { /* REG */
    1286     1048310 :                         if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1287             : 
    1288             :                                 /* late stat, if not yet called */
    1289     1048310 :                                 if (!st)
    1290     1048310 :                                         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     1048310 :                                 scan_file(scan, is_diff, sub_next, st, FILEPHY_UNREAD_OFFSET);
    1306     1048310 :                                 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       35300 :                 } else if (type == 1) { /* LNK */
    1311       34114 :                         if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1312             :                                 char subnew[PATH_MAX];
    1313             :                                 int ret;
    1314             : 
    1315       34114 :                                 ret = readlink(path_next, subnew, sizeof(subnew));
    1316       34114 :                                 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       34114 :                                 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       34114 :                                 if (ret == 0)
    1329           0 :                                         log_fatal("WARNING! Empty symbolic link '%s'.\n", path_next);
    1330             : 
    1331             :                                 /* readlink doesn't put the final 0 */
    1332       34114 :                                 subnew[ret] = 0;
    1333             : 
    1334             :                                 /* process as a symbolic link */
    1335       34114 :                                 scan_link(scan, is_diff, sub_next, subnew, FILE_IS_SYMLINK);
    1336       34114 :                                 processed = 1;
    1337             :                         } else {
    1338           0 :                                 msg_verbose("Excluding link '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1339             :                         }
    1340        1186 :                 } else if (type == 2) { /* DIR */
    1341        1186 :                         if (filter_dir(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1342             : #ifndef _WIN32
    1343             :                                 /* late stat, if not yet called */
    1344        1186 :                                 if (!st)
    1345        1186 :                                         st = DSTAT(path_next, dd, &st_buf);
    1346             : 
    1347             :                                 /* in Unix don't follow mount points in different devices */
    1348             :                                 /* in Windows we are already skipping them reporting them as special files */
    1349        1186 :                                 if ((uint64_t)st->st_dev != disk->device) {
    1350           0 :                                         log_fatal("WARNING! Ignoring mount point '%s' because it appears to be in a different device\n", path_next);
    1351             :                                 } else
    1352             : #endif
    1353             :                                 {
    1354             :                                         char sub_dir[PATH_MAX];
    1355             : 
    1356             :                                         /* recurse */
    1357        1186 :                                         pathslash(path_next, sizeof(path_next));
    1358        1186 :                                         pathcpy(sub_dir, sizeof(sub_dir), sub_next);
    1359        1186 :                                         pathslash(sub_dir, sizeof(sub_dir));
    1360        1186 :                                         if (scan_dir(scan, level + 1, is_diff, path_next, sub_dir) == 0) {
    1361             :                                                 /* scan the directory as empty dir */
    1362         239 :                                                 scan_emptydir(scan, sub_next);
    1363             :                                         }
    1364             :                                         /* or we processed something internally, or we have added the empty dir */
    1365        1186 :                                         processed = 1;
    1366             :                                 }
    1367             :                         } else {
    1368           0 :                                 msg_verbose("Excluding directory '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1369             :                         }
    1370             :                 } else {
    1371           0 :                         if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
    1372             :                                 /* late stat, if not yet called */
    1373           0 :                                 if (!st)
    1374           0 :                                         st = DSTAT(path_next, dd, &st_buf);
    1375             : 
    1376           0 :                                 log_fatal("WARNING! Ignoring special '%s' file '%s'\n", stat_desc(st), path_next);
    1377             :                         } else {
    1378           0 :                                 msg_verbose("Excluding special file '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
    1379             :                         }
    1380             :                 }
    1381             : 
    1382             :                 /* next entry */
    1383     1083610 :                 node = node->next;
    1384             : 
    1385             :                 /* free the present one */
    1386     1083610 :                 free(dd);
    1387             :         }
    1388             : 
    1389        1713 :         return processed;
    1390             : }
    1391             : 
    1392          88 : static int state_diffscan(struct snapraid_state* state, int is_diff)
    1393             : {
    1394             :         tommy_node* i;
    1395             :         tommy_node* j;
    1396             :         tommy_list scanlist;
    1397             :         int done;
    1398             :         fptr* msg;
    1399             :         struct snapraid_scan total;
    1400             :         int no_difference;
    1401             : 
    1402          88 :         tommy_list_init(&scanlist);
    1403             : 
    1404          88 :         if (is_diff)
    1405           7 :                 msg_progress("Comparing...\n");
    1406             : 
    1407             :         /* first scan all the directory and find new and deleted files */
    1408         615 :         for (i = state->disklist; i != 0; i = i->next) {
    1409         527 :                 struct snapraid_disk* disk = i->data;
    1410             :                 struct snapraid_scan* scan;
    1411             :                 tommy_node* node;
    1412             :                 int ret;
    1413             :                 int has_persistent_inode;
    1414             : 
    1415         527 :                 scan = malloc_nofail(sizeof(struct snapraid_scan));
    1416         527 :                 scan->state = state;
    1417         527 :                 scan->disk = disk;
    1418         527 :                 scan->count_equal = 0;
    1419         527 :                 scan->count_move = 0;
    1420         527 :                 scan->count_copy = 0;
    1421         527 :                 scan->count_restore = 0;
    1422         527 :                 scan->count_change = 0;
    1423         527 :                 scan->count_remove = 0;
    1424         527 :                 scan->count_insert = 0;
    1425         527 :                 tommy_list_init(&scan->file_insert_list);
    1426         527 :                 tommy_list_init(&scan->link_insert_list);
    1427         527 :                 tommy_list_init(&scan->dir_insert_list);
    1428             : 
    1429         527 :                 tommy_list_insert_tail(&scanlist, &scan->node, scan);
    1430             : 
    1431         527 :                 if (!is_diff)
    1432         485 :                         msg_progress("Scanning disk %s...\n", disk->name);
    1433             : 
    1434             :                 /* check if the disk supports persistent inodes */
    1435         527 :                 ret = fsinfo(disk->dir, &has_persistent_inode, 0, 0);
    1436         527 :                 if (ret < 0) {
    1437             :                         /* LCOV_EXCL_START */
    1438             :                         log_fatal("Error accessing disk '%s' to get file-system info. %s.\n", disk->dir, strerror(errno));
    1439             :                         exit(EXIT_FAILURE);
    1440             :                         /* LCOV_EXCL_STOP */
    1441             :                 }
    1442         527 :                 if (!has_persistent_inode) {
    1443           0 :                         disk->has_volatile_inodes = 1;
    1444             :                 }
    1445             : 
    1446             :                 /* if inodes or UUID are not persistent/changed/unsupported */
    1447         527 :                 if (disk->has_volatile_inodes || disk->has_different_uuid || disk->has_unsupported_uuid) {
    1448             :                         /* remove all the inodes from the inode collection */
    1449             :                         /* if they are not persistent, all of them could be changed now */
    1450             :                         /* and we don't want to find false matching ones */
    1451             :                         /* see scan_file() for more details */
    1452          90 :                         node = disk->filelist;
    1453         190 :                         while (node) {
    1454          10 :                                 struct snapraid_file* file = node->data;
    1455             : 
    1456          10 :                                 node = node->next;
    1457             : 
    1458             :                                 /* remove from the inode set */
    1459          10 :                                 tommy_hashdyn_remove_existing(&disk->inodeset, &file->nodeset);
    1460             : 
    1461             :                                 /* clear the inode */
    1462          10 :                                 file->inode = 0;
    1463             : 
    1464             :                                 /* mark as missing inode */
    1465          10 :                                 file_flag_set(file, FILE_IS_WITHOUT_INODE);
    1466             :                         }
    1467             :                 }
    1468             : 
    1469         527 :                 scan_dir(scan, 0, is_diff, disk->dir, "");
    1470             :         }
    1471             : 
    1472             :         /* we split the search in two phases because to detect files */
    1473             :         /* moved from one disk to another we have to start deletion */
    1474             :         /* only when all disks have all the new files found */
    1475             : 
    1476             :         /* now process all the new and deleted files */
    1477         615 :         for (i = scanlist; i != 0; i = i->next) {
    1478         527 :                 struct snapraid_scan* scan = i->data;
    1479         527 :                 struct snapraid_disk* disk = scan->disk;
    1480             :                 tommy_node* node;
    1481             :                 unsigned phy_count;
    1482             :                 unsigned phy_dup;
    1483             :                 uint64_t phy_last;
    1484             :                 struct snapraid_file* phy_file_last;
    1485             : 
    1486             :                 /* check for removed files */
    1487         527 :                 node = disk->filelist;
    1488      974990 :                 while (node) {
    1489      973936 :                         struct snapraid_file* file = node->data;
    1490             : 
    1491             :                         /* next node */
    1492      973936 :                         node = node->next;
    1493             : 
    1494             :                         /* remove if not present */
    1495      973936 :                         if (!file_flag_has(file, FILE_IS_PRESENT)) {
    1496       29646 :                                 ++scan->count_remove;
    1497             : 
    1498       29646 :                                 log_tag("scan:remove:%s:%s\n", disk->name, esc(file->sub));
    1499       29646 :                                 if (is_diff) {
    1500          97 :                                         printf("remove %s%s\n", disk->dir, file->sub);
    1501             :                                 }
    1502             : 
    1503       29646 :                                 scan_file_remove(scan, file);
    1504             :                         }
    1505             :                 }
    1506             : 
    1507             :                 /* check for removed links */
    1508         527 :                 node = disk->linklist;
    1509       33847 :                 while (node) {
    1510       32793 :                         struct snapraid_link* slink = node->data;
    1511             : 
    1512             :                         /* next node */
    1513       32793 :                         node = node->next;
    1514             : 
    1515             :                         /* remove if not present */
    1516       32793 :                         if (!link_flag_has(slink, FILE_IS_PRESENT)) {
    1517         956 :                                 ++scan->count_remove;
    1518             : 
    1519         956 :                                 log_tag("scan:remove:%s:%s\n", disk->name, esc(slink->sub));
    1520         956 :                                 if (is_diff) {
    1521          10 :                                         printf("remove %s%s\n", disk->dir, slink->sub);
    1522             :                                 }
    1523             : 
    1524         956 :                                 scan_link_remove(scan, slink);
    1525             :                         }
    1526             :                 }
    1527             : 
    1528             :                 /* check for removed dirs */
    1529         527 :                 node = disk->dirlist;
    1530        1285 :                 while (node) {
    1531         231 :                         struct snapraid_dir* dir = node->data;
    1532             : 
    1533             :                         /* next node */
    1534         231 :                         node = node->next;
    1535             : 
    1536             :                         /* remove if not present */
    1537         231 :                         if (!dir_flag_has(dir, FILE_IS_PRESENT)) {
    1538           2 :                                 scan_emptydir_remove(scan, dir);
    1539             :                         }
    1540             :                 }
    1541             : 
    1542             :                 /* sort the files before inserting them */
    1543             :                 /* we use a stable sort to ensure that if the reported physical offset/inode */
    1544             :                 /* are always 0, we keep at least the directory order */
    1545         527 :                 switch (state->opt.force_order) {
    1546             :                 case SORT_PHYSICAL :
    1547          12 :                         tommy_list_sort(&scan->file_insert_list, file_physical_compare);
    1548          12 :                         break;
    1549             :                 case SORT_INODE :
    1550           0 :                         tommy_list_sort(&scan->file_insert_list, file_inode_compare);
    1551           0 :                         break;
    1552             :                 case SORT_ALPHA :
    1553         515 :                         tommy_list_sort(&scan->file_insert_list, file_path_compare);
    1554         515 :                         break;
    1555             :                 case SORT_DIR :
    1556             :                         /* already in order */
    1557           0 :                         break;
    1558             :                 }
    1559             : 
    1560             :                 /* insert all the new files, we insert them only after the deletion */
    1561             :                 /* to reuse the just freed space */
    1562             :                 /* also check if the physical offset reported are fakes or not */
    1563         527 :                 node = scan->file_insert_list;
    1564         527 :                 phy_count = 0;
    1565         527 :                 phy_dup = 0;
    1566         527 :                 phy_last = FILEPHY_UNREAD_OFFSET;
    1567         527 :                 phy_file_last = 0;
    1568      104894 :                 while (node) {
    1569      103840 :                         struct snapraid_file* file = node->data;
    1570             : 
    1571             :                         /* if the file is not empty, count duplicate physical offsets */
    1572      103840 :                         if (state->opt.force_order == SORT_PHYSICAL && file->size != 0) {
    1573       11418 :                                 if (phy_file_last != 0 && file->physical == phy_last
    1574             :                                         /* files without offset are expected to have duplicates */
    1575           0 :                                         && phy_last != FILEPHY_WITHOUT_OFFSET
    1576             :                                 ) {
    1577             :                                         /* if verbose, print the list of duplicates real offsets */
    1578             :                                         /* other cases are for offsets not supported, so we don't need to report them file by file */
    1579           0 :                                         if (phy_last >= FILEPHY_REAL_OFFSET) {
    1580           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);
    1581             :                                         }
    1582           0 :                                         ++phy_dup;
    1583             :                                 }
    1584       11418 :                                 phy_file_last = file;
    1585       11418 :                                 phy_last = file->physical;
    1586       11418 :                                 ++phy_count;
    1587             :                         }
    1588             : 
    1589             :                         /* next node */
    1590      103840 :                         node = node->next;
    1591             : 
    1592             :                         /* insert in the parity */
    1593      103840 :                         scan_file_allocate(scan, file);
    1594             :                 }
    1595             : 
    1596             :                 /* mark the disk without reliable physical offset if it has duplicates */
    1597             :                 /* here it should never happen because we already sorted out hardlinks */
    1598         527 :                 if (state->opt.force_order == SORT_PHYSICAL && phy_dup > 0) {
    1599           0 :                         disk->has_unreliable_physical = 1;
    1600             :                 }
    1601             : 
    1602             :                 /* insert all the new links */
    1603         527 :                 node = scan->link_insert_list;
    1604        3511 :                 while (node) {
    1605        2457 :                         struct snapraid_link* slink = node->data;
    1606             : 
    1607             :                         /* next node */
    1608        2457 :                         node = node->next;
    1609             : 
    1610             :                         /* insert it */
    1611        2457 :                         scan_link_insert(scan, slink);
    1612             :                 }
    1613             : 
    1614             :                 /* insert all the new dirs */
    1615         527 :                 node = scan->dir_insert_list;
    1616        1064 :                 while (node) {
    1617          10 :                         struct snapraid_dir* dir = node->data;
    1618             : 
    1619             :                         /* next node */
    1620          10 :                         node = node->next;
    1621             : 
    1622             :                         /* insert it */
    1623          10 :                         scan_emptydir_insert(scan, dir);
    1624             :                 }
    1625             :         }
    1626             : 
    1627             :         /* check for disks where all the previously existing files where removed */
    1628          88 :         if (!state->opt.force_empty) {
    1629          82 :                 int all_missing = 0;
    1630          82 :                 int all_rewritten = 0;
    1631          82 :                 done = 0;
    1632         573 :                 for (i = state->disklist, j = scanlist; i != 0; i = i->next, j = j->next) {
    1633         491 :                         struct snapraid_disk* disk = i->data;
    1634         491 :                         struct snapraid_scan* scan = j->data;
    1635             : 
    1636         491 :                         if (scan->count_equal == 0
    1637          71 :                                 && scan->count_move == 0
    1638          71 :                                 && scan->count_restore == 0
    1639          71 :                                 && (scan->count_remove != 0 || scan->count_change != 0)
    1640             :                         ) {
    1641           1 :                                 if (!done) {
    1642           1 :                                         done = 1;
    1643           1 :                                         log_fatal("WARNING! All the files previously present in disk '%s' at dir '%s'", disk->name, disk->dir);
    1644             :                                 } else {
    1645           0 :                                         log_fatal(", disk '%s' at dir '%s'", disk->name, disk->dir);
    1646             :                                 }
    1647             : 
    1648             :                                 /* detect the special condition of all files missing */
    1649           1 :                                 if (scan->count_change == 0)
    1650           1 :                                         all_missing = 1;
    1651             : 
    1652             :                                 /* detect the special condition of all files rewritten */
    1653           1 :                                 if (scan->count_remove == 0)
    1654           0 :                                         all_rewritten = 1;
    1655             :                         }
    1656             :                 }
    1657          82 :                 if (done) {
    1658           1 :                         log_fatal("\nare now missing or rewritten!\n");
    1659           1 :                         if (all_rewritten) {
    1660           0 :                                 log_fatal("This could happen when restoring a disk with a backup\n");
    1661           0 :                                 log_fatal("program that is not setting correctly the timestamps.\n");
    1662             :                         }
    1663           1 :                         if (all_missing) {
    1664           1 :                                 log_fatal("This could happen when some disks are not mounted\n");
    1665           1 :                                 log_fatal("in the expected directory.\n");
    1666             :                         }
    1667           1 :                         if (!is_diff) {
    1668           1 :                                 log_fatal("If you want to '%s' anyway, use 'snapraid --force-empty %s'.\n", state->command, state->command);
    1669           1 :                                 exit(EXIT_FAILURE);
    1670             :                         }
    1671             :                 }
    1672             :         }
    1673             : 
    1674             :         /* check for disks without the physical offset support */
    1675          87 :         if (state->opt.force_order == SORT_PHYSICAL) {
    1676           2 :                 done = 0;
    1677          14 :                 for (i = state->disklist; i != 0; i = i->next) {
    1678          12 :                         struct snapraid_disk* disk = i->data;
    1679             : 
    1680          12 :                         if (disk->has_unreliable_physical) {
    1681           0 :                                 if (!done) {
    1682           0 :                                         done = 1;
    1683           0 :                                         log_fatal("WARNING! Physical offsets not supported for disk '%s'", disk->name);
    1684             :                                 } else {
    1685           0 :                                         log_fatal(", '%s'", disk->name);
    1686             :                                 }
    1687             :                         }
    1688             :                 }
    1689           2 :                 if (done) {
    1690           0 :                         log_fatal(". Files order won't be optimal.\n");
    1691             :                 }
    1692             :         }
    1693             : 
    1694             :         /* check for disks without persistent inodes */
    1695          87 :         done = 0;
    1696         608 :         for (i = state->disklist; i != 0; i = i->next) {
    1697         521 :                 struct snapraid_disk* disk = i->data;
    1698             : 
    1699         521 :                 if (disk->has_volatile_inodes) {
    1700           0 :                         if (!done) {
    1701           0 :                                 done = 1;
    1702           0 :                                 log_fatal("WARNING! Inodes are not persistent for disks: '%s'", disk->name);
    1703             :                         } else {
    1704           0 :                                 log_fatal(", '%s'", disk->name);
    1705             :                         }
    1706             :                 }
    1707             :         }
    1708          87 :         if (done) {
    1709           0 :                 log_fatal(". Move operations won't be optimal.\n");
    1710             :         }
    1711             : 
    1712             :         /* check for disks with changed UUID */
    1713          87 :         done = 0;
    1714         608 :         for (i = state->disklist; i != 0; i = i->next) {
    1715         521 :                 struct snapraid_disk* disk = i->data;
    1716             : 
    1717             :                 /* don't print the message if the UUID changed because before */
    1718             :                 /* it was no set. */
    1719             :                 /* this is the normal condition for an empty disk because it */
    1720             :                 /* isn't stored */
    1721         521 :                 if (disk->has_different_uuid && !disk->had_empty_uuid) {
    1722           2 :                         if (!done) {
    1723           2 :                                 done = 1;
    1724           2 :                                 log_fatal("WARNING! UUID is changed for disks: '%s'", disk->name);
    1725             :                         } else {
    1726           0 :                                 log_fatal(", '%s'", disk->name);
    1727             :                         }
    1728             :                 }
    1729             :         }
    1730          87 :         if (done) {
    1731           2 :                 log_fatal(". Move operations won't be optimal.\n");
    1732             :         }
    1733             : 
    1734             :         /* check for disks with unsupported UUID */
    1735          87 :         done = 0;
    1736         608 :         for (i = state->disklist; i != 0; i = i->next) {
    1737         521 :                 struct snapraid_disk* disk = i->data;
    1738             : 
    1739         521 :                 if (disk->has_unsupported_uuid) {
    1740           0 :                         if (!done) {
    1741           0 :                                 done = 1;
    1742           0 :                                 log_fatal("WARNING! UUID is unsupported for disks: '%s'", disk->name);
    1743             :                         } else {
    1744           0 :                                 log_fatal(", '%s'", disk->name);
    1745             :                         }
    1746             :                 }
    1747             :         }
    1748          87 :         if (done) {
    1749           0 :                 log_fatal(". Move operations won't be optimal.\n");
    1750             : #if defined(_linux) && !HAVE_BLKID
    1751             :                 log_fatal("The 'blkid' library is not linked in SnapRAID!\n");
    1752             :                 log_fatal("Try rebuilding it after installing the libblkid-dev/devel package.\n");
    1753             : #endif
    1754             :         }
    1755             : 
    1756          87 :         total.count_equal = 0;
    1757          87 :         total.count_move = 0;
    1758          87 :         total.count_copy = 0;
    1759          87 :         total.count_restore = 0;
    1760          87 :         total.count_change = 0;
    1761          87 :         total.count_remove = 0;
    1762          87 :         total.count_insert = 0;
    1763             : 
    1764         608 :         for (i = scanlist; i != 0; i = i->next) {
    1765         521 :                 struct snapraid_scan* scan = i->data;
    1766         521 :                 total.count_equal += scan->count_equal;
    1767         521 :                 total.count_move += scan->count_move;
    1768         521 :                 total.count_copy += scan->count_copy;
    1769         521 :                 total.count_restore += scan->count_restore;
    1770         521 :                 total.count_change += scan->count_change;
    1771         521 :                 total.count_remove += scan->count_remove;
    1772         521 :                 total.count_insert += scan->count_insert;
    1773             :         }
    1774             : 
    1775          87 :         if (is_diff) {
    1776           7 :                 msg_status("\n");
    1777           7 :                 msg = msg_status;
    1778             :         } else {
    1779          80 :                 msg = msg_verbose;
    1780             :         }
    1781             : 
    1782          87 :         msg("%8u equal\n", total.count_equal);
    1783          87 :         msg("%8u moved\n", total.count_move);
    1784          87 :         msg("%8u copied\n", total.count_copy);
    1785          87 :         msg("%8u restored\n", total.count_restore);
    1786          87 :         msg("%8u updated\n", total.count_change);
    1787          87 :         msg("%8u removed\n", total.count_remove);
    1788          87 :         msg("%8u added\n", total.count_insert);
    1789             : 
    1790          87 :         log_tag("summary:equal:%u\n", total.count_equal);
    1791          87 :         log_tag("summary:moved:%u\n", total.count_move);
    1792          87 :         log_tag("summary:copied:%u\n", total.count_copy);
    1793          87 :         log_tag("summary:restored:%u\n", total.count_restore);
    1794          87 :         log_tag("summary:updated:%u\n", total.count_change);
    1795          87 :         log_tag("summary:removed:%u\n", total.count_remove);
    1796          87 :         log_tag("summary:added:%u\n", total.count_insert);
    1797             : 
    1798         260 :         no_difference = !total.count_move && !total.count_copy && !total.count_restore
    1799         144 :                 && !total.count_change && !total.count_remove && !total.count_insert;
    1800             : 
    1801          87 :         if (is_diff) {
    1802           7 :                 if (no_difference) {
    1803           1 :                         msg_status("No differences\n");
    1804             :                 } else {
    1805           6 :                         msg_status("There are differences!\n");
    1806             :                 }
    1807             :         }
    1808             : 
    1809          87 :         if (no_difference) {
    1810          14 :                 log_tag("summary:exit:equal\n");
    1811             :         } else {
    1812          73 :                 log_tag("summary:exit:diff\n");
    1813             :         }
    1814          87 :         log_flush();
    1815             : 
    1816          87 :         tommy_list_foreach(&scanlist, (tommy_foreach_func*)free);
    1817             : 
    1818             :         /* check the file-system on all disks */
    1819          87 :         state_fscheck(state, "after scan");
    1820             : 
    1821          87 :         if (is_diff) {
    1822             :                 /* check for file difference */
    1823           7 :                 if (!no_difference)
    1824           6 :                         return 1;
    1825             : 
    1826             :                 /* check also for incomplete "sync" */
    1827           1 :                 if (parity_is_invalid(state))
    1828           0 :                         return 1;
    1829             :         }
    1830             : 
    1831          81 :         return 0;
    1832             : }
    1833             : 
    1834           7 : int state_diff(struct snapraid_state* state)
    1835             : {
    1836           7 :         return state_diffscan(state, 1);
    1837             : }
    1838             : 
    1839          81 : void state_scan(struct snapraid_state* state)
    1840             : {
    1841          81 :         (void)state_diffscan(state, 0); /* ignore return value */
    1842          80 : }
    1843             : 

Generated by: LCOV version 1.10