1 #include "filelist.hpp"
4 #include "filelist.hpp"
5 #include "directory.hpp"
9 filelist::filelist(const std::string
& _backingfile
, const std::string
& _directory
)
10 : backingfile(_backingfile
), directory(_directory
)
18 std::set
<std::string
> filelist::enumerate()
20 auto contents
= readfile();
21 check_stale(contents
);
23 std::set
<std::string
> ret
;
24 for(auto i
: contents
)
30 void filelist::add(const std::string
& filename
)
32 auto contents
= readfile();
33 int64_t ts
= directory::mtime(directory
+ "/" + filename
);
34 contents
[filename
] = ts
;
38 void filelist::remove(const std::string
& filename
)
40 auto contents
= readfile();
41 //FIXME: Do something with this?
42 //int64_t ts = directory::mtime(directory + "/" + filename);
43 contents
.erase(filename
);
47 void filelist::rename(const std::string
& oldname
, const std::string
& newname
)
49 auto contents
= readfile();
50 contents
[newname
] = contents
[oldname
];
51 contents
.erase(oldname
);
55 std::map
<std::string
, int64_t> filelist::readfile()
57 std::map
<std::string
, int64_t> contents
;
58 std::ifstream
strm(backingfile
);
60 while(std::getline(strm
, line
)) {
61 regex_results r
= regex("([0-9]+) (.*)", line
);
64 try { ts
= parse_value
<int64_t>(r
[1]); } catch(...) { continue; }
70 void filelist::check_stale(std::map
<std::string
, int64_t>& data
)
73 int64_t ts
= directory::mtime(directory
+ "/" + i
.first
);
74 //If file timestamp does not match, mark the file as stale.
80 void filelist::writeback(const std::map
<std::string
, int64_t>& data
)
82 std::string backingtmp
= backingfile
+ ".new";
83 std::ofstream
strm(backingtmp
);
88 strm
<< i
.second
<< " " << i
.first
<< std::endl
;
90 directory::rename_overwrite(backingtmp
.c_str(), backingfile
.c_str());