Improve platform indepdendence of directory blobs

This commit is contained in:
Sebastian Messmer 2016-02-09 12:37:30 +01:00
parent 68c10afbda
commit 88e6164d35
2 changed files with 45 additions and 26 deletions

View File

@ -17,28 +17,47 @@ namespace cryfs {
key.ToBinary(dest+offset); key.ToBinary(dest+offset);
offset += key.BINARY_LENGTH; offset += key.BINARY_LENGTH;
*reinterpret_cast<uid_t*>(dest+offset) = uid; *reinterpret_cast<uint32_t*>(dest+offset) = uid;
offset += sizeof(uid_t); offset += sizeof(uint32_t);
*reinterpret_cast<gid_t*>(dest+offset) = gid; *reinterpret_cast<uint32_t*>(dest+offset) = gid;
offset += sizeof(gid_t); offset += sizeof(uint32_t);
*reinterpret_cast<mode_t*>(dest+offset) = mode; *reinterpret_cast<uint32_t*>(dest+offset) = mode;
offset += sizeof(mode_t); offset += sizeof(uint32_t);
static_assert(sizeof(timespec) == 16, "Ensure platform independence of the serialization"); offset += _serializeTimeValue(dest + offset, lastAccessTime);
*reinterpret_cast<timespec*>(dest+offset) = lastAccessTime; offset += _serializeTimeValue(dest + offset, lastModificationTime);
offset += sizeof(timespec); offset += _serializeTimeValue(dest + offset, lastMetadataChangeTime);
*reinterpret_cast<timespec*>(dest+offset) = lastModificationTime;
offset += sizeof(timespec);
*reinterpret_cast<timespec*>(dest+offset) = lastMetadataChangeTime;
offset += sizeof(timespec);
ASSERT(offset == serializedSize(), "Didn't write correct number of elements"); ASSERT(offset == serializedSize(), "Didn't write correct number of elements");
} }
unsigned int DirEntry::_serializeTimeValue(uint8_t *dest, timespec value) {
unsigned int offset = 0;
*reinterpret_cast<uint64_t*>(dest+offset) = value.tv_sec;
offset += sizeof(uint64_t);
*reinterpret_cast<uint32_t*>(dest+offset) = value.tv_nsec;
offset += sizeof(uint32_t);
ASSERT(offset == _serializedTimeValueSize(), "serialized to wrong size");
return offset;
}
size_t DirEntry::_serializedTimeValueSize() {
return sizeof(uint64_t) + sizeof(uint32_t);
}
timespec DirEntry::_deserializeTimeValue(const char **pos) {
timespec value;
value.tv_sec = *(uint64_t*)(*pos);
*pos += sizeof(uint64_t);
value.tv_nsec = *(uint32_t*)(*pos);
*pos += sizeof(uint32_t);
return value;
}
size_t DirEntry::serializedSize() const { size_t DirEntry::serializedSize() const {
return 1 + (name.size() + 1) + key.BINARY_LENGTH + sizeof(uid_t) + sizeof(gid_t) + sizeof(mode_t) + 3*sizeof(timespec); return 1 + (name.size() + 1) + key.BINARY_LENGTH + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t) + 3*_serializedTimeValueSize();
} }
const char *DirEntry::deserializeAndAddToVector(const char *pos, vector<DirEntry> *result) { const char *DirEntry::deserializeAndAddToVector(const char *pos, vector<DirEntry> *result) {
@ -54,21 +73,18 @@ namespace cryfs {
Key key = Key::FromBinary(pos); Key key = Key::FromBinary(pos);
pos += Key::BINARY_LENGTH; pos += Key::BINARY_LENGTH;
uid_t uid = *(uid_t*)pos; uid_t uid = *(uint32_t*)pos;
pos += sizeof(uid_t); pos += sizeof(uint32_t);
gid_t gid = *(gid_t*)pos; gid_t gid = *(uint32_t*)pos;
pos += sizeof(gid_t); pos += sizeof(uint32_t);
mode_t mode = *(mode_t*)pos; mode_t mode = *(uint32_t*)pos;
pos += sizeof(mode_t); pos += sizeof(uint32_t);
timespec lastAccessTime = *(timespec*)pos; timespec lastAccessTime = _deserializeTimeValue(&pos);
pos += sizeof(timespec); timespec lastModificationTime = _deserializeTimeValue(&pos);
timespec lastModificationTime = *(timespec*)pos; timespec lastMetadataChangeTime = _deserializeTimeValue(&pos);
pos += sizeof(timespec);
timespec lastMetadataChangeTime = *(timespec*)pos;
pos += sizeof(timespec);
result->emplace_back(type, name, key, mode, uid, gid, lastAccessTime, lastModificationTime, lastMetadataChangeTime); result->emplace_back(type, name, key, mode, uid, gid, lastAccessTime, lastModificationTime, lastMetadataChangeTime);
return pos; return pos;

View File

@ -34,6 +34,9 @@ namespace cryfs {
void serialize(uint8_t* dest) const; void serialize(uint8_t* dest) const;
size_t serializedSize() const; size_t serializedSize() const;
static const char *deserializeAndAddToVector(const char *pos, std::vector<DirEntry> *result); static const char *deserializeAndAddToVector(const char *pos, std::vector<DirEntry> *result);
static size_t _serializedTimeValueSize();
static unsigned int _serializeTimeValue(uint8_t *dest, timespec value);
static timespec _deserializeTimeValue(const char **pos);
fspp::Dir::EntryType type; fspp::Dir::EntryType type;
std::string name; std::string name;