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
= file_get_mtime(directory
+ "/" + filename
);
34 contents
[filename
] = ts
;
38 void filelist::remove(const std::string
& filename
)
40 auto contents
= readfile();
41 int64_t ts
= file_get_mtime(directory
+ "/" + filename
);
42 contents
.erase(filename
);
46 void filelist::rename(const std::string
& oldname
, const std::string
& newname
)
48 auto contents
= readfile();
49 contents
[newname
] = contents
[oldname
];
50 contents
.erase(oldname
);
54 std::map
<std::string
, int64_t> filelist::readfile()
56 std::map
<std::string
, int64_t> contents
;
57 std::ifstream
strm(backingfile
);
59 while(std::getline(strm
, line
)) {
60 regex_results r
= regex("([0-9]+) (.*)", line
);
63 try { ts
= parse_value
<int64_t>(r
[1]); } catch(...) { continue; }
69 void filelist::check_stale(std::map
<std::string
, int64_t>& data
)
72 int64_t ts
= file_get_mtime(directory
+ "/" + i
.first
);
73 //If file timestamp does not match, mark the file as stale.
79 void filelist::writeback(const std::map
<std::string
, int64_t>& data
)
81 std::string backingtmp
= backingfile
+ ".new";
82 std::ofstream
strm(backingtmp
);
87 strm
<< i
.second
<< " " << i
.first
<< std::endl
;
89 zip::rename_overwrite(backingtmp
.c_str(), backingfile
.c_str());