monotone

monotone Mtn Source Tree

Root/unix/inodeprint.cc

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status