LCOV - code coverage report
Current view: top level - cmdline - import.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 103 105 98.1 %
Date: 2015-05-12 Functions: 8 8 100.0 %

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

Generated by: LCOV version 1.10