Strictly speaking, WvPam is not a stream. So move wvpam.cc to utils.
[wvapps.git] / funfs / funfscim.cc
blob7e65a8d3585d50d60ca4d4743fc713792482184d
1 #include "funfscim.h"
2 #include "strutils.h"
5 CacheInfo::CacheInfo(CacheInfoMgr *_cim, WvStringParm _path, bool _root)
6 : path(_path), root(_root), uptodate(0), famid(0), child_count(0), cim(_cim)
8 cim->fi_by_path.add(this, root);
12 CacheInfo::~CacheInfo()
14 if (!root && uptodate & FILEMASK)
15 cim->get_fileinfo(getdirname(path))->remove_child();
17 if (famid)
18 cim->fam._unmonitor(famid);
20 cim->fi_by_path.remove(this);
24 void CacheInfo::mark_uptodate(uint8 _uptodate)
26 if (root || _uptodate & DIRMASK && uptodate & ~DIRMASK)
28 if (famid)
29 fprintf(stderr, "monitoring listing for %s\n", path.cstr());
30 famid = famid ? famid : cim->fam._monitordir(&path);
33 if (_uptodate & FILEMASK && uptodate & ~FILEMASK)
35 fprintf(stderr, "monitoring attributes for %s\n", path.cstr());
36 if (!root)
37 cim->get_fileinfo(getdirname(path))->add_child();
38 else
39 famid = famid ? famid : cim->fam._monitordir(&path);
43 uptodate |= _uptodate;
47 bool CacheInfo::is_uptodate(uint8 _uptodate)
49 return uptodate & _uptodate;
53 void CacheInfo::invalidate(bool thisfile)
55 if (thisfile)
57 if (!root && uptodate & FILEMASK)
59 cim->get_fileinfo(getdirname(path))->remove_child();
60 fprintf(stderr, "invalidating attributes for %s\n", path.cstr());
63 uptodate &= ~FILEMASK;
65 // means something *under* us changed
66 else
68 if (child_count == 0)
70 if (famid)
72 cim->fam._unmonitor(famid);
73 fprintf(stderr, "invalidating listing for %s\n", path.cstr());
75 famid = 0;
78 uptodate &= ~DIRMASK;
81 if (uptodate == 0 && child_count == 0 && !root)
82 delete this;
86 void CacheInfo::add_child()
88 child_count++;
90 if (!famid)
91 famid = cim->fam._monitordir(&path);
95 void CacheInfo::remove_child()
97 child_count--;
99 if (child_count == 0 && uptodate == 0)
101 if (root)
103 if (famid)
104 cim->fam._unmonitor(famid);
106 else
107 delete this;
112 CacheInfoMgr::CacheInfoMgr()
113 : fam(WvFamCallback(this, &CacheInfoMgr::fam_callback)), fdmap(20)
115 new CacheInfo(this, "/", true);
119 CacheInfo *CacheInfoMgr::get_fileinfo(WvStringParm path)
121 CacheInfo *fi = fi_by_path[path];
123 return fi ? fi : new CacheInfo(this, path);
127 void CacheInfoMgr::cim_do_open(WvStringParm path, int fd)
129 fdmap.add(fd, path);
133 void CacheInfoMgr::cim_do_close(int fd)
135 fdmap.remove(fd);
139 void CacheInfoMgr::cim_mark_uptodate(WvStringParm path, JobType jobtype)
141 CacheInfo *fi = get_fileinfo(path);
143 switch (jobtype)
145 case JT_GETDIR:
146 fi->mark_uptodate(UTD_LIST);
147 break;
148 case JT_GETATTR:
149 fi->mark_uptodate(UTD_ATTR);
150 break;
151 case JT_READLINK:
152 fi->mark_uptodate(UTD_LINK);
153 break;
154 default:
155 assert(! "cim_mark_uptodate called with unknown jobtype");
161 void CacheInfoMgr::fam_callback(WvStringParm file, WvFamEvent event, bool fromdir)
163 CacheInfo *fi = fi_by_path[file];
165 if (!fi)
166 return;
168 CacheInfo *parent = fi_by_path[getdirname(file)];
170 fi->invalidate(true);
171 if (parent)
172 parent->invalidate(false);