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

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2014 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 "handle.h"
      24             : 
      25           1 : void state_nano(struct snapraid_state* state)
      26             : {
      27             :         tommy_node* i;
      28             : 
      29           1 :         msg_progress("Setting nanosecond timestamps...\n");
      30             : 
      31             :         /* for all disks */
      32           7 :         for (i = state->disklist; i != 0; i = i->next) {
      33           6 :                 struct snapraid_disk* disk = i->data;
      34             :                 tommy_node* j;
      35             : 
      36             :                 /* for all files */
      37        5723 :                 for (j = disk->filelist; j != 0; j = j->next) {
      38        5717 :                         struct snapraid_file* file = j->data;
      39             : 
      40             :                         /* if the file has a zero nanosecond timestamp */
      41             :                         /* note that symbolic links are not in the file list */
      42             :                         /* and then are not processed */
      43        5717 :                         if (file->mtime_nsec == 0) {
      44             :                                 char path[PATH_MAX];
      45             :                                 struct stat st;
      46             :                                 int f;
      47             :                                 int ret;
      48             :                                 int nsec;
      49             : 
      50          16 :                                 pathprint(path, sizeof(path), "%s%s", disk->dir, file->sub);
      51             : 
      52             :                                 /* set a new nanosecond timestamp different than 0 */
      53             :                                 do {
      54             :                                         uint32_t nano;
      55             : 
      56             :                                         /* get a random nanosecond value */
      57          16 :                                         if (randomize(&nano, sizeof(nano)) != 0) {
      58             :                                                 /* LCOV_EXCL_START */
      59             :                                                 log_fatal("Failed to get random values.\n");
      60             :                                                 exit(EXIT_FAILURE);
      61             :                                                 /* LCOV_EXCL_STOP */
      62             :                                         }
      63             : 
      64          16 :                                         nsec = nano % 1000000000;
      65          16 :                                 } while (nsec == 0);
      66             : 
      67             :                                 /* open it */
      68             :                                 /* O_NOFOLLOW: do not follow links to ensure to open the real file */
      69          16 :                                 f = open(path, O_RDONLY | O_BINARY | O_NOFOLLOW);
      70          16 :                                 if (f == -1) {
      71             :                                         /* LCOV_EXCL_START */
      72             :                                         log_fatal("Error opening file '%s'. %s.\n", path, strerror(errno));
      73             :                                         continue;
      74             :                                         /* LCOV_EXCL_STOP */
      75             :                                 }
      76             : 
      77             :                                 /* get the present timestamp, that may be different than the one */
      78             :                                 /* in the content file */
      79          16 :                                 ret = fstat(f, &st);
      80          16 :                                 if (ret == -1) {
      81             :                                         /* LCOV_EXCL_START */
      82             :                                         close(f);
      83             :                                         log_fatal("Error accessing file '%s'. %s.\n", path, strerror(errno));
      84             :                                         continue;
      85             :                                         /* LCOV_EXCL_STOP */
      86             :                                 }
      87             : 
      88             :                                 /* set the tweaked modification time, with new nano seconds */
      89          16 :                                 ret = fmtime(f, st.st_mtime, nsec);
      90          16 :                                 if (ret != 0) {
      91             :                                         /* LCOV_EXCL_START */
      92             :                                         close(f);
      93             :                                         log_fatal("Error timing file '%s'. %s.\n", path, strerror(errno));
      94             :                                         continue;
      95             :                                         /* LCOV_EXCL_STOP */
      96             :                                 }
      97             : 
      98             :                                 /* uses fstat again to get the present timestamp */
      99             :                                 /* this is needed because the value read */
     100             :                                 /* may be different than the written one */
     101          16 :                                 ret = fstat(f, &st);
     102          16 :                                 if (ret == -1) {
     103             :                                         /* LCOV_EXCL_START */
     104             :                                         close(f);
     105             :                                         log_fatal("Error accessing file '%s'. %s.\n", path, strerror(errno));
     106             :                                         continue;
     107             :                                         /* LCOV_EXCL_STOP */
     108             :                                 }
     109             : 
     110             :                                 /* close it */
     111          16 :                                 ret = close(f);
     112          16 :                                 if (ret != 0) {
     113             :                                         /* LCOV_EXCL_START */
     114             :                                         log_fatal("Error closing file '%s'. %s.\n", path, strerror(errno));
     115             :                                         continue;
     116             :                                         /* LCOV_EXCL_STOP */
     117             :                                 }
     118             : 
     119             :                                 /* set the same nanosecond value in the content file */
     120             :                                 /* note that if the seconds value is already matching */
     121             :                                 /* the file won't be synched because the content file will */
     122             :                                 /* contain the new updated timestamp */
     123          16 :                                 file->mtime_nsec = STAT_NSEC(&st);
     124             : 
     125             :                                 /* state changed, we need to update it */
     126          16 :                                 state->need_write = 1;
     127             : 
     128          16 :                                 log_tag("nano:%s:%s\n", disk->name, esc(file->sub));
     129          16 :                                 msg_info("nano %s%s\n", disk->dir, file->sub);
     130             :                         }
     131             :                 }
     132             :         }
     133           1 : }
     134             : 

Generated by: LCOV version 1.10