LCOV - code coverage report
Current view: top level - cmdline - import.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 95 105 90.5 %
Date: 2016-01-16 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2013 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 "import.h"
      22             : 
      23             : /****************************************************************************/
      24             : /* import */
      25             : 
      26             : /**
      27             :  * Compare the hash of two import blocks.
      28             :  */
      29        4472 : int import_block_hash_compare(const void* void_arg, const void* void_data)
      30             : {
      31        4472 :         const unsigned char* arg = void_arg;
      32        4472 :         const struct snapraid_import_block* block = void_data;
      33             : 
      34        4472 :         return memcmp(arg, block->hash, HASH_SIZE);
      35             : }
      36             : 
      37           0 : int import_block_prevhash_compare(const void* void_arg, const void* void_data)
      38             : {
      39           0 :         const unsigned char* arg = void_arg;
      40           0 :         const struct snapraid_import_block* block = void_data;
      41             : 
      42           0 :         return memcmp(arg, block->prevhash, HASH_SIZE);
      43             : }
      44             : 
      45             : /**
      46             :  * Compute the hash of the hash for an import block.
      47             :  * We just use the first 32 bit of the hash itself.
      48             :  */
      49      657104 : static inline tommy_uint32_t import_block_hash(const unsigned char* hash)
      50             : {
      51             :         /* the hash data is not aligned, and we cannot access it with a direct cast */
      52      657104 :         return hash[0] | ((uint32_t)hash[1] << 8) | ((uint32_t)hash[2] << 16) | ((uint32_t)hash[3] << 24);
      53             : }
      54             : 
      55        1750 : static void import_file(struct snapraid_state* state, const char* path, uint64_t size)
      56             : {
      57             :         struct snapraid_import_file* file;
      58             :         block_off_t i;
      59             :         data_off_t offset;
      60             :         void* buffer;
      61             :         int ret;
      62             :         int f;
      63             :         int flags;
      64        1750 :         unsigned block_size = state->block_size;
      65             : 
      66        1750 :         file = malloc_nofail(sizeof(struct snapraid_import_file));
      67        1750 :         file->path = strdup_nofail(path);
      68        1750 :         file->size = size;
      69        1750 :         file->blockmax = (size + block_size - 1) / block_size;
      70        1750 :         file->blockimp = malloc_nofail(file->blockmax * sizeof(struct snapraid_import_block));
      71             : 
      72        1750 :         buffer = malloc_nofail(block_size);
      73             : 
      74             :         /* open for read */
      75             :         /* O_SEQUENTIAL: opening in sequential mode in Windows */
      76        1750 :         flags = O_RDONLY | O_BINARY;
      77        1750 :         if ((state->file_mode & MODE_SEQUENTIAL) != 0)
      78        1750 :                 flags |= O_SEQUENTIAL;
      79        1750 :         f = open(path, flags);
      80        1750 :         if (f == -1) {
      81             :                 /* LCOV_EXCL_START */
      82             :                 log_fatal("Error opening file '%s'. %s.\n", path, strerror(errno));
      83             :                 exit(EXIT_FAILURE);
      84             :                 /* LCOV_EXCL_STOP */
      85             :         }
      86             : 
      87             : #if HAVE_POSIX_FADVISE
      88        1750 :         if ((state->file_mode & MODE_SEQUENTIAL) != 0) {
      89             :                 /* advise sequential access */
      90        1750 :                 ret = posix_fadvise(f, 0, 0, POSIX_FADV_SEQUENTIAL);
      91        1750 :                 if (ret != 0) {
      92             :                         /* LCOV_EXCL_START */
      93             :                         log_fatal("Error advising file '%s'. %s.\n", path, strerror(ret));
      94             :                         exit(EXIT_FAILURE);
      95             :                         /* LCOV_EXCL_STOP */
      96             :                 }
      97             :         }
      98             : #endif
      99             : 
     100        1750 :         offset = 0;
     101        6222 :         for (i = 0; i < file->blockmax; ++i) {
     102        4472 :                 struct snapraid_import_block* block = &file->blockimp[i];
     103        4472 :                 unsigned read_size = block_size;
     104        4472 :                 if (read_size > size)
     105        1745 :                         read_size = size;
     106             : 
     107        4472 :                 ret = read(f, buffer, read_size);
     108        4472 :                 if (ret < 0 || (unsigned)ret != read_size) {
     109             :                         /* LCOV_EXCL_START */
     110             :                         log_fatal("Error reading file '%s'. %s.\n", path, strerror(errno));
     111             :                         exit(EXIT_FAILURE);
     112             :                         /* LCOV_EXCL_STOP */
     113             :                 }
     114             : 
     115        4472 :                 block->file = file;
     116        4472 :                 block->offset = offset;
     117        4472 :                 block->size = read_size;
     118             : 
     119        4472 :                 memhash(state->hash, state->hashseed, block->hash, buffer, read_size);
     120        4472 :                 tommy_hashdyn_insert(&state->importset, &block->nodeset, block, import_block_hash(block->hash));
     121             : 
     122             :                 /* if we are in a rehash state */
     123        4472 :                 if (state->prevhash != HASH_UNDEFINED) {
     124             :                         /* compute also the previous hash */
     125           0 :                         memhash(state->prevhash, state->prevhashseed, block->prevhash, buffer, read_size);
     126           0 :                         tommy_hashdyn_insert(&state->previmportset, &block->prevnodeset, block, import_block_hash(block->prevhash));
     127             :                 }
     128             : 
     129        4472 :                 offset += read_size;
     130        4472 :                 size -= read_size;
     131             :         }
     132             : 
     133        1750 :         ret = close(f);
     134        1750 :         if (ret != 0) {
     135             :                 /* LCOV_EXCL_START */
     136             :                 log_fatal("Error closing file '%s'. %s.\n", path, strerror(errno));
     137             :                 exit(EXIT_FAILURE);
     138             :                 /* LCOV_EXCL_STOP */
     139             :         }
     140             : 
     141        1750 :         tommy_list_insert_tail(&state->importlist, &file->nodelist, file);
     142             : 
     143        1750 :         free(buffer);
     144        1750 : }
     145             : 
     146        1750 : void import_file_free(struct snapraid_import_file* file)
     147             : {
     148        1750 :         free(file->path);
     149        1750 :         free(file->blockimp);
     150        1750 :         free(file);
     151        1750 : }
     152             : 
     153      652632 : int state_import_fetch(struct snapraid_state* state, int rehash, struct snapraid_block* missing_block, unsigned char* buffer)
     154             : {
     155             :         struct snapraid_import_block* block;
     156             :         int ret;
     157             :         int f;
     158      652632 :         const unsigned char* hash = missing_block->hash;
     159      652632 :         unsigned block_size = state->block_size;
     160             :         unsigned read_size;
     161             :         unsigned char buffer_hash[HASH_SIZE];
     162             :         const char* path;
     163             : 
     164      652632 :         if (rehash) {
     165           0 :                 block = tommy_hashdyn_search(&state->previmportset, import_block_prevhash_compare, hash, import_block_hash(hash));
     166             :         } else {
     167      652632 :                 block = tommy_hashdyn_search(&state->importset, import_block_hash_compare, hash, import_block_hash(hash));
     168             :         }
     169      652632 :         if (!block)
     170      648160 :                 return -1;
     171             : 
     172        4472 :         path = block->file->path;
     173        4472 :         read_size = block->size;
     174             : 
     175        4472 :         f = open(path, O_RDONLY | O_BINARY);
     176        4472 :         if (f == -1) {
     177             :                 /* LCOV_EXCL_START */
     178             :                 if (errno == ENOENT) {
     179             :                         log_fatal("DANGER! file '%s' disappeared.\n", path);
     180             :                         log_fatal("If you moved it, please rerun the same command.\n");
     181             :                 } else {
     182             :                         log_fatal("Error opening file '%s'. %s.\n", path, strerror(errno));
     183             :                 }
     184             :                 exit(EXIT_FAILURE);
     185             :                 /* LCOV_EXCL_STOP */
     186             :         }
     187             : 
     188        4472 :         if (lseek(f, block->offset, SEEK_SET) != block->offset) {
     189             :                 /* LCOV_EXCL_START */
     190             :                 log_fatal("Error seeking file '%s'. %s.\n", path, strerror(errno));
     191             :                 exit(EXIT_FAILURE);
     192             :                 /* LCOV_EXCL_STOP */
     193             :         }
     194             : 
     195        4472 :         ret = read(f, buffer, read_size);
     196        4472 :         if (ret < 0 || (unsigned)ret != read_size) {
     197             :                 /* LCOV_EXCL_START */
     198             :                 log_fatal("Error reading file '%s'. %s.\n", path, strerror(errno));
     199             :                 exit(EXIT_FAILURE);
     200             :                 /* LCOV_EXCL_STOP */
     201             :         }
     202             : 
     203        4472 :         ret = close(f);
     204        4472 :         if (ret != 0) {
     205             :                 /* LCOV_EXCL_START */
     206             :                 log_fatal("Error closing file '%s'. %s.\n", path, strerror(errno));
     207             :                 exit(EXIT_FAILURE);
     208             :                 /* LCOV_EXCL_STOP */
     209             :         }
     210             : 
     211        4472 :         if (read_size != block_size) {
     212             :                 /* fill the remaining with 0 */
     213        1745 :                 memset(buffer + read_size, 0, block_size - read_size);
     214             :         }
     215             : 
     216             :         /* recheck the hash */
     217        4472 :         if (rehash)
     218           0 :                 memhash(state->prevhash, state->prevhashseed, buffer_hash, buffer, read_size);
     219             :         else
     220        4472 :                 memhash(state->hash, state->hashseed, buffer_hash, buffer, read_size);
     221             : 
     222        4472 :         if (memcmp(buffer_hash, hash, HASH_SIZE) != 0) {
     223             :                 /* LCOV_EXCL_START */
     224             :                 log_fatal("Error in data reading file '%s'.\n", path);
     225             :                 log_fatal("Please don't change imported files while running.\n");
     226             :                 exit(EXIT_FAILURE);
     227             :                 /* LCOV_EXCL_STOP */
     228             :         }
     229             : 
     230        4472 :         return 0;
     231             : }
     232             : 
     233           1 : static void import_dir(struct snapraid_state* state, const char* dir)
     234             : {
     235             :         DIR* d;
     236             : 
     237           1 :         d = opendir(dir);
     238           1 :         if (!d) {
     239             :                 /* LCOV_EXCL_START */
     240             :                 log_fatal("Error opening directory '%s'. %s.\n", dir, strerror(errno));
     241             :                 exit(EXIT_FAILURE);
     242             :                 /* LCOV_EXCL_STOP */
     243             :         }
     244             : 
     245             :         while (1) {
     246             :                 char path_next[PATH_MAX];
     247             :                 struct stat st;
     248             :                 const char* name;
     249             :                 struct dirent* dd;
     250             : 
     251             :                 /* clear errno to detect erroneous conditions */
     252        1812 :                 errno = 0;
     253        1812 :                 dd = readdir(d);
     254        1812 :                 if (dd == 0 && errno != 0) {
     255             :                         /* LCOV_EXCL_START */
     256             :                         log_fatal("Error reading directory '%s'. %s.\n", dir, strerror(errno));
     257             :                         exit(EXIT_FAILURE);
     258             :                         /* LCOV_EXCL_STOP */
     259             :                 }
     260        1812 :                 if (dd == 0) {
     261           1 :                         break; /* finished */
     262             :                 }
     263             : 
     264             :                 /* skip "." and ".." files */
     265        1811 :                 name = dd->d_name;
     266        1811 :                 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
     267           2 :                         continue;
     268             : 
     269        1809 :                 pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
     270             : 
     271             : #if HAVE_STRUCT_DIRENT_D_STAT
     272             :                 /* convert dirent to lstat result */
     273             :                 dirent_lstat(dd, &st);
     274             : 
     275             :                 /* if the st_mode field is missing, takes care to fill it using normal lstat() */
     276             :                 /* at now this can happen only in Windows (with HAVE_STRUCT_DIRENT_D_STAT defined), */
     277             :                 /* because we use a directory reading method that doesn't read info about ReparsePoint. */
     278             :                 /* Note that here we cannot call here lstat_sync(), because we don't know what kind */
     279             :                 /* of file is it, and lstat_sync() doesn't always work */
     280             :                 if (st.st_mode == 0) {
     281             :                         if (lstat(path_next, &st) != 0) {
     282             :                                 /* LCOV_EXCL_START */
     283             :                                 log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
     284             :                                 exit(EXIT_FAILURE);
     285             :                                 /* LCOV_EXCL_STOP */
     286             :                         }
     287             :                 }
     288             : #else
     289             :                 /* get lstat info about the file */
     290        1809 :                 if (lstat(path_next, &st) != 0) {
     291             :                         /* LCOV_EXCL_START */
     292             :                         log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
     293             :                         exit(EXIT_FAILURE);
     294             :                         /* LCOV_EXCL_STOP */
     295             :                 }
     296             : #endif
     297             : 
     298        1809 :                 if (S_ISREG(st.st_mode)) {
     299        1750 :                         import_file(state, path_next, st.st_size);
     300          59 :                 } else if (S_ISDIR(st.st_mode)) {
     301           0 :                         pathslash(path_next, sizeof(path_next));
     302           0 :                         import_dir(state, path_next);
     303             :                 }
     304        1811 :         }
     305             : 
     306           1 :         if (closedir(d) != 0) {
     307             :                 /* LCOV_EXCL_START */
     308             :                 log_fatal("Error closing directory '%s'. %s.\n", dir, strerror(errno));
     309             :                 exit(EXIT_FAILURE);
     310             :                 /* LCOV_EXCL_STOP */
     311             :         }
     312           1 : }
     313             : 
     314           1 : void state_import(struct snapraid_state* state, const char* dir)
     315             : {
     316             :         char path[PATH_MAX];
     317             : 
     318           1 :         msg_progress("Importing...\n");
     319             : 
     320             :         /* add the final slash */
     321           1 :         pathimport(path, sizeof(path), dir);
     322           1 :         pathslash(path, sizeof(path));
     323             : 
     324           1 :         import_dir(state, path);
     325           1 : }
     326             : 

Generated by: LCOV version 1.10