1 // Copyright (C) 2005 nathaniel smith <njs@pobox.com>
2 //
3 // This program is made available under the GNU GPL version 2.0 or
4 // greater. See the accompanying file COPYING for details.
5 //
6 // This program is distributed WITHOUT ANY WARRANTY; without even the
7 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE.
9 
10 #include "../base.hh"
11 #include <sys/stat.h>
12 #include <time.h>
13 
14 #include "../platform.hh"
15 
should_abort(time_t now,time_t then)16 inline bool should_abort(time_t now, time_t then)
17 {
18   if (now < 0 || then < 0)
19     return false;
20   double difference = difftime(now, then);
21   return (difference >= -3 && difference <= 3);
22 }
23 
is_future(time_t now,time_t then)24 inline bool is_future(time_t now, time_t then)
25 {
26   if (now < 0 || then < 0)
27     return false;
28   return difftime(now, then) > 0;
29 }
30 
inodeprint_file(std::string const & file,inodeprint_calculator & calc)31 bool inodeprint_file(std::string const & file, inodeprint_calculator & calc)
32 {
33   struct stat st;
34   if (stat(file.c_str(), &st) < 0)
35     return false;
36 
37   time_t now;
38   time(&now);
39 
40   calc.note_nowish(should_abort(now, st.st_ctime));
41   calc.add_item(st.st_ctime);
42   calc.note_future(is_future(now, st.st_ctime));
43 
44   // aah, portability.
45 #ifdef HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC
46   calc.add_item(st.st_ctim.tv_nsec);
47 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC_TV_NSEC)
48   calc.add_item(st.st_ctimespec.tv_nsec);
49 #elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
50   calc.add_item(st.st_ctimensec);
51 #else
52   calc.add_item((long)0);
53 #endif
54 
55   calc.note_nowish(should_abort(now, st.st_mtime));
56   calc.add_item(st.st_mtime);
57   calc.note_future(is_future(now, st.st_mtime));
58 
59 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
60   calc.add_item(st.st_mtim.tv_nsec);
61 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
62   calc.add_item(st.st_mtimespec.tv_nsec);
63 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
64   calc.add_item(st.st_mtimensec);
65 #else
66   calc.add_item((long)0);
67 #endif
68 
69   calc.add_item(st.st_mode);
70   calc.add_item(st.st_ino);
71   calc.add_item(st.st_dev);
72   calc.add_item(st.st_uid);
73   calc.add_item(st.st_gid);
74   calc.add_item(st.st_size);
75 
76   return true;
77 }
78 
79 // Local Variables:
80 // mode: C++
81 // fill-column: 76
82 // c-file-style: "gnu"
83 // indent-tabs-mode: nil
84 // End:
85 // vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
86