LCOV - code coverage report
Current view: top level - cmdline - scan.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 557 624 89.3 %
Date: 2016-01-16 Functions: 23 23 100.0 %

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

Generated by: LCOV version 1.10