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 <sys/stat.h>
7#include <time.h>
8
9#include "platform.hh"
10
11inline bool should_abort(time_t now, time_t then)
12{
13 if (now < 0 || then < 0)
14 return false;
15 double difference = difftime(now, then);
16 return (difference >= -3 && difference <= 3);
17}
18
19inline bool is_future(time_t now, time_t then)
20{
21 if (now < 0 || then < 0)
22 return false;
23 return difftime(now, then) > 0;
24}
25
26bool inodeprint_file(std::string const & file, inodeprint_calculator & calc)
27{
28 struct stat st;
29 if (stat(file.c_str(), &st) < 0)
30 return false;
31
32 time_t now;
33 time(&now);
34
35 calc.note_nowish(should_abort(now, st.st_ctime));
36 calc.add_item(st.st_ctime);
37 calc.note_future(is_future(now, st.st_ctime));
38
39 // aah, portability.
40#ifdef HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC
41 calc.add_item(st.st_ctim.tv_nsec);
42#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC_TV_NSEC)
43 calc.add_item(st.st_ctimespec.tv_nsec);
44#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
45 calc.add_item(st.st_ctimensec);
46#else
47 calc.add_item((long)0);
48#endif
49
50 calc.note_nowish(should_abort(now, st.st_mtime));
51 calc.add_item(st.st_mtime);
52 calc.note_future(is_future(now, st.st_mtime));
53
54#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
55 calc.add_item(st.st_mtim.tv_nsec);
56#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
57 calc.add_item(st.st_mtimespec.tv_nsec);
58#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
59 calc.add_item(st.st_mtimensec);
60#else
61 calc.add_item((long)0);
62#endif
63
64 calc.add_item(st.st_mode);
65 calc.add_item(st.st_ino);
66 calc.add_item(st.st_dev);
67 calc.add_item(st.st_uid);
68 calc.add_item(st.st_gid);
69 calc.add_item(st.st_size);
70
71 return true;
72}
73
74// Local Variables:
75// mode: C++
76// fill-column: 76
77// c-file-style: "gnu"
78// indent-tabs-mode: nil
79// End:
80// 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