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

Generated by: LCOV version 1.0