Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Add NameShortener. Juggle function arguments around a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuerig committed Jun 1, 2011
1 parent 2622e3e commit e8cce4a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
71 changes: 45 additions & 26 deletions src/DirectoryMetadata.cpp
Expand Up @@ -20,15 +20,23 @@ 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(new DirectoryMetadata(etc));
Ptr dm(boost::make_shared<DirectoryMetadata>(etc));
vector<Entry>& entries(dm->entries_);

for ( fs::directory_iterator it(path); it != fs::directory_iterator(); ++it ) {
for ( fs::directory_iterator it(path), end; it != end; ++it ) {
const string name = it->path().filename().string();

struct stat st;
Expand All @@ -38,19 +46,32 @@ DirectoryMetadata::fromFilesystem(

Entry entry = {
longName : name,
shortName : name,
shortName : shortener(name),
uid : st.st_uid,
gid : st.st_gid,
mode : st.st_mode,
mtime : st.st_mtime,
};

dm->entries_.push_back(entry);
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,
Expand All @@ -59,7 +80,7 @@ DirectoryMetadata::fromMetadataFile(
) {
fs::path path(directoryPath);
path /= filename;
Ptr dm(new DirectoryMetadata(etc));
Ptr dm(boost::make_shared<DirectoryMetadata>(etc));

ifstream ifs(path.string().c_str());

Expand All @@ -69,12 +90,13 @@ DirectoryMetadata::fromMetadataFile(
return dm;
}


DirectoryMetadata::Ptr
DirectoryMetadata::fromMetadataFile(
const string& directoryPath,
const string& filename
) {
EtcPtr etc(new Etc);
EtcPtr etc(boost::make_shared<Etc>());
return fromMetadataFile(directoryPath, etc, filename);
}

Expand All @@ -85,22 +107,17 @@ DirectoryMetadata::fromMetadataFile(
return fromMetadataFile(directoryPath, metadataFilename);
}

DirectoryMetadata::DirectoryMetadata(boost::shared_ptr<Etc> etc)
: etc_(etc)
{
}

void
DirectoryMetadata::write() const
{
DirectoryMetadata::write() const {
fs::path path(metadataFilename);
ofstream os(path.string().c_str());
write(os);
}


void
DirectoryMetadata::write(ostream& os) const
{
DirectoryMetadata::write(ostream& os) const {
boost::archive::xml_oarchive oa(os);
oa << ser::make_nvp("entries", entries_);
}
Expand All @@ -118,6 +135,7 @@ inline void serialize(
boost::serialization::split_free(ar, e, file_version);
}


template<class Archive>
void save(
Archive& ar,
Expand All @@ -126,14 +144,15 @@ void save(
) {
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);
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,
Expand All @@ -142,19 +161,19 @@ void load(
) {
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("longname", e.longName);
ar >> ser::make_nvp("shortname", e.shortName);

string owner;
ar & ser::make_nvp("owner", owner);
ar >> ser::make_nvp("owner", owner);
e.uid = etc.lookupUserId(owner);

string group;
ar & ser::make_nvp("group", 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);
ar >> ser::make_nvp("mode", e.mode);
ar >> ser::make_nvp("mtime", e.mtime);
}

} // namespace serialization
Expand Down
17 changes: 13 additions & 4 deletions src/DirectoryMetadata.h
Expand Up @@ -5,8 +5,9 @@
#include <ostream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>

class Etc;

Expand All @@ -15,6 +16,11 @@ class DirectoryMetadata : public boost::noncopyable {
typedef boost::shared_ptr<DirectoryMetadata> Ptr;
typedef boost::shared_ptr<Etc> EtcPtr;

struct NameShortener {
virtual std::string operator()(const std::string& longName) = 0;
virtual ~NameShortener() {};
};

struct Entry {
std::string longName;
std::string shortName;
Expand All @@ -28,13 +34,18 @@ class DirectoryMetadata : public boost::noncopyable {

static const std::string metadataFilename;

DirectoryMetadata(const EtcPtr etc);
// The ctor ought to be private, but that won't work with make_shared

static Ptr fromFilesystem(
const std::string& directoryPath,
NameShortener& shortener,
const EtcPtr etc
);

static Ptr fromFilesystem(
const std::string& directoryPath
const std::string& directoryPath,
NameShortener& shortener
);

static Ptr fromMetadataFile(
Expand Down Expand Up @@ -62,8 +73,6 @@ class DirectoryMetadata : public boost::noncopyable {
}

private:
DirectoryMetadata(const boost::shared_ptr<Etc> etc);

std::vector<Entry> entries_;
const boost::shared_ptr<Etc> etc_;
};
Expand Down
28 changes: 20 additions & 8 deletions src/dirmeta.cpp
@@ -1,23 +1,35 @@

#include "DirectoryMetadata.h"
#include <exception>
#include <iostream>

using namespace std;
using namespace boost;

class IdentityShortener : public DirectoryMetadata::NameShortener
{
public:
virtual string operator()(const string& longName) {
return std::string(longName, 0, 50);
}
};

int
main(
int argc,
char* argv[]
) {
// DirectoryMetadata::Ptr md = DirectoryMetadata::fromFilesystem(".");
DirectoryMetadata::Ptr md = argc > 1 ?
DirectoryMetadata::fromMetadataFile(".", argv[1]) :
DirectoryMetadata::fromMetadataFile(".");
DirectoryMetadata::Ptr md;

if ( argc > 1 && (string(argv[1]) == string("-f")) ) {
md = argc > 2 ?
DirectoryMetadata::fromMetadataFile(".", argv[2]) :
DirectoryMetadata::fromMetadataFile(".");
} else {
IdentityShortener shortener;
md = DirectoryMetadata::fromFilesystem(".", shortener);
}

// for (DirectoryMetadata::EntryIter it = md->cbegin(); it != md->cend(); ++it) {
// cout << it->shortName << "\t" << it->longName << "\t" << it->uid << "\t" << oct << it->mode << dec << endl;
// }

md->write(cout);

return 0;
Expand Down

0 comments on commit e8cce4a

Please sign in to comment.