WvStreams
unifstreegen.cc
1 #include "uniconfgen.h"
2 #include "unimountgen.h"
3 #include "wvmoniker.h"
4 #include "wvlinkerhack.h"
5 #include "wvlog.h"
6 
7 #include "unifiltergen.h"
8 
10 {
11  WvString dir;
12  UniMountGen *mount;
13  IUniConfGen *treegen;
14  WvLog log;
15 
16 public:
17  UniAutoMountGen(WvStringParm _dir)
18  : UniFilterGen(mount = new UniMountGen), dir(_dir),
19  log(WvString("AutoMount '%s'", dir), WvLog::Info)
20  {
21  log("Starting.\n");
22  mount->mount("/", WvString("readonly:fs:%s", dir), true);
23  treegen = mount->whichmount("/", NULL);
24  }
25 
26  virtual ~UniAutoMountGen()
27  {
28  log("Stopping.\n");
29  }
30 
31  virtual bool keymap(const UniConfKey &key, UniConfKey &mapped_key)
32  {
33  automount(key);
34  return UniFilterGen::keymap(key, mapped_key);
35  }
36 
37  void automount(const UniConfKey &key)
38  {
39  IUniConfGen *parent = mount->whichmount(key, NULL);
40  if (parent && parent != treegen && parent->haschildren("/"))
41  return; // don't bother; already mounted a parent
42 
43  log("Automount for '%s'\n", key);
44 
45  for (int count = key.numsegments(); count >= 0; count--)
46  {
47  UniConfKey k(key.first(count));
48  if (mount->ismountpoint(k))
49  {
50  log("Key '%s' already mounted.\n", k);
51  return; // already mounted
52  }
53 
54  WvString filename("%s/%s", dir, k);
55  log("Filename is '%s'\n", filename);
56  mount->mount(k, WvString("ini:%s", filename), true);
57  log("Key '%s' newly mounted.\n", k);
58  return; // newly mounted
59  }
60 
61  // just plain not found
62  log("Key '%s' not found.\n", key);
63  }
64 
65  virtual Iter *recursiveiterator(const UniConfKey &key)
66  {
67  // don't try to optimize this like UniMountGen does, because we're
68  // going to mount things *as* we iterate through them, not sooner.
69  // Use the default UniConfGen implementation, which just recursively
70  // calls iterator().
72  }
73 };
74 
75 
76 WV_LINK(UniFsTreeGen);
77 
78 
79 static IUniConfGen *creator(WvStringParm s, IObject *)
80 {
81  return new UniAutoMountGen(s);
82 }
83 
84 WvMoniker<IUniConfGen> UniFsTreeGenMoniker("fstree", creator);
85 
86 
UniMountGen::ismountpoint
virtual bool ismountpoint(const UniConfKey &key)
Determines if a key is a mountpoint.
Definition: unimountgen.cc:290
UniAutoMountGen::keymap
virtual bool keymap(const UniConfKey &key, UniConfKey &mapped_key)
A mapping function for filters that remap one keyspace onto another.
Definition: unifstreegen.cc:31
UniMountGen::mount
virtual IUniConfGen * mount(const UniConfKey &key, WvStringParm moniker, bool refresh)
Mounts a generator at a key using a moniker.
Definition: unimountgen.cc:176
UniFilterGen::keymap
virtual bool keymap(const UniConfKey &unmapped_key, UniConfKey &mapped_key)
A mapping function for filters that remap one keyspace onto another.
Definition: unifiltergen.cc:37
UniConfGen::recursiveiterator
virtual Iter * recursiveiterator(const UniConfKey &key)
Like iterator(), but the returned iterator is recursive, that is, it will return children of the imme...
Definition: uniconfgen.cc:260
UniMountGen::whichmount
virtual IUniConfGen * whichmount(const UniConfKey &key, UniConfKey *mountpoint)
Finds the generator that owns a key.
Definition: unimountgen.cc:271
IUniConfGen::haschildren
virtual bool haschildren(const UniConfKey &key)=0
Returns true if a key has children.
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
IUniConfGen
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:39
WvLog
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition: wvlog.h:56
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
WvMoniker
A type-safe version of WvMonikerBase that lets you provide create functions for object types other th...
Definition: wvmoniker.h:61
IObject
Definition: IObject.h:65
UniConfKey::first
UniConfKey first(int n=1) const
Returns the path formed by the n first segments of this path.
Definition: uniconfkey.h:314
UniAutoMountGen::recursiveiterator
virtual Iter * recursiveiterator(const UniConfKey &key)
Like iterator(), but the returned iterator is recursive, that is, it will return children of the imme...
Definition: unifstreegen.cc:65
UniMountGen
The UniMountTree implementation realized as a UniConfGen.
Definition: unimountgen.h:17
UniConfKey::numsegments
int numsegments() const
Returns the number of segments in this path.
Definition: uniconfkey.h:287
UniFilterGen
A UniConfGen that delegates all requests to an inner generator.
Definition: unifiltergen.h:17
UniAutoMountGen
Definition: unifstreegen.cc:9
UniConfGen::Iter
An abstract iterator over keys and values in a generator.
Definition: uniconfgen.h:323