This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirectoryMetadata.cpp
182 lines (141 loc) · 4.31 KB
/
DirectoryMetadata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "DirectoryMetadata.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/filesystem.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>
#include "Etc.h"
#include "util.h"
using namespace std;
namespace fs = boost::filesystem;
namespace ser = boost::serialization;
namespace sys = boost::system;
const string DirectoryMetadata::metadataFilename = ".preserfs";
DirectoryMetadata::DirectoryMetadata(const EtcPtr etc)
: etc_(etc)
{
}
DirectoryMetadata::Ptr
DirectoryMetadata::fromFilesystem(
const string& directoryPath,
NameShortener& shortener,
const boost::shared_ptr<Etc> etc
) {
fs::path path(directoryPath);
Ptr dm(boost::make_shared<DirectoryMetadata>(etc));
vector<Entry>& entries(dm->entries_);
for ( fs::directory_iterator it(path), end; it != end; ++it ) {
const string entryPath( it->path().string() );
struct stat st;
if ( lstat(entryPath.c_str(), &st) < 0 ) {
util::throw_errno("lstat", entryPath);
}
const string entryName( it->path().filename().string() );
Entry entry = {
longName : entryName,
shortName : shortener(entryName),
uid : st.st_uid,
gid : st.st_gid,
mode : st.st_mode,
mtime : st.st_mtime,
};
entries.push_back(entry);
}
entries.swap(entries); // trim excessive capacity
return dm;
}
DirectoryMetadata::Ptr
DirectoryMetadata::fromFilesystem(
const string& directoryPath,
NameShortener& shortener
) {
EtcPtr etc(boost::make_shared<Etc>());
return fromFilesystem(directoryPath, shortener, etc);
}
DirectoryMetadata::Ptr
DirectoryMetadata::fromMetadataFile(
const string& directoryPath,
const EtcPtr etc,
const string& filename
) {
fs::path path(directoryPath);
path /= filename;
Ptr dm(boost::make_shared<DirectoryMetadata>(etc));
ifstream ifs(path.string().c_str());
boost::archive::xml_iarchive ia(ifs);
ia >> ser::make_nvp("entries", dm->entries_);
return dm;
}
DirectoryMetadata::Ptr
DirectoryMetadata::fromMetadataFile(
const string& directoryPath,
const string& filename
) {
EtcPtr etc(boost::make_shared<Etc>());
return fromMetadataFile(directoryPath, etc, filename);
}
DirectoryMetadata::Ptr
DirectoryMetadata::fromMetadataFile(
const string& directoryPath
) {
return fromMetadataFile(directoryPath, metadataFilename);
}
void
DirectoryMetadata::write() const {
fs::path path(metadataFilename);
ofstream os(path.string().c_str());
write(os);
}
void
DirectoryMetadata::write(ostream& os) const {
boost::archive::xml_oarchive oa(os);
oa << ser::make_nvp("entries", entries_);
}
namespace boost {
namespace serialization {
template<class Archive>
inline void serialize(
Archive& ar,
DirectoryMetadata::Entry& e,
const unsigned int file_version
) {
boost::serialization::split_free(ar, e, file_version);
}
template<class Archive>
void save(
Archive& ar,
const DirectoryMetadata::Entry& e,
const unsigned int /*version*/
) {
Etc etc; // TODO how to get hold of the instance in DirectoryMetadata from here?
ar << ser::make_nvp("longname", e.longName);
ar << ser::make_nvp("shortname", e.shortName);
ar << ser::make_nvp("owner", etc.lookupUsername(e.uid));
ar << ser::make_nvp("group", etc.lookupGroupname(e.gid));
ar << ser::make_nvp("mode", e.mode);
ar << ser::make_nvp("mtime", e.mtime);
}
template<class Archive>
void load(
Archive& ar,
DirectoryMetadata::Entry& e,
const unsigned int /*version*/
) {
Etc etc; // TODO how to get hold of the instance in DirectoryMetadata from here?
ar >> ser::make_nvp("longname", e.longName);
ar >> ser::make_nvp("shortname", e.shortName);
string owner;
ar >> ser::make_nvp("owner", owner);
e.uid = etc.lookupUserId(owner);
string group;
ar >> ser::make_nvp("group", group);
e.gid = etc.lookupGroupId(group);
ar >> ser::make_nvp("mode", e.mode);
ar >> ser::make_nvp("mtime", e.mtime);
}
} // namespace serialization
} // namespace boost