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

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

Generated by: LCOV version 1.10