summaryrefslogtreecommitdiff
path: root/src/hdb/filemap.h
blob: d6af7eb7c6a872f87a88658f5e020a83131e6cbd (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef FILEMAPH
#define FILEMAPH

#include <mt/mstring.h>
#include <mt/lock.h>
#include <mt/mlock.h>
#include <mt/mlock.h>
#include <netinet/in.h>

//////////////////////////////////////////////
//
// NOTE that filemap is MT unsafe:
// it needs to be called from dbmap
//
// The exception is when filelocking;
// that's MT safe per filemap, and hence
// should not be locked further
//

#define HEADLOCK(fm) headlock_t headlock(fm)

//////////////////////////////////////////////////////

// network byteorder
class byte4 {
public:
  byte4() {} // must be uninitialized
  explicit byte4(int h): n(htonl(h)) {}
  void set(int h) { n=htonl(h); }
  void add(int h) { n=htonl(ntohl(n)+h); }
  int get() const { return ntohl(n); }
private:
  long n;
};

//////////////////////////////////////////////////////

class filemap {
public:
  typedef char char32[32];
  static const int maxtabs=32;
  
  ////////////////////////////////////////////////////

  struct head_t {
    char32 ident;
    byte4 lock;
    byte4 minor;
    byte4 major;
    byte4 fatal;
    byte4 filesize;
    byte4 filetop;
    byte4 tables;
    byte4 index;
    char32 compname;
    char32 tabs[maxtabs];
    byte4 frame;

    head_t();
    head_t(const head_t* hd,int mem);

    void setcomp(int idx, const sref& cp);

    sref get(int col) const { return sref(tabs[col]); }
    void set(int col,const sref& s);
    void insert(int col,const sref& s);
    void remove(int col);
  };
  
  head_t* head() { return hd; }
  const head_t* head() const { return hd; }

  ///////////////////////////////////////////////////////

  struct rec_t {
    byte4 mem;
    byte4 caps;
  };

  rec_t* at(int off) {
    for(chunk_t* p=mem;p;p=p->next) if(p->own(off)) return p->at(off); return 0;
  }
  const rec_t* at(int off) const {
    for(const chunk_t* p=mem;p;p=p->next) if(p->own(off)) return p->at(off);
    return 0;
  }

  int recs() const { return hd->frame.get(); }
  int size(int off) const {
    if(off<hd->filetop.get()) {
      for(const chunk_t* p=mem;p;p=p->next)
        if(p->own(off)) return p->at(off)->mem.get();
    }
    return 0;
  }

  ///////////////////////////////////////////////////////

  void remap();

  rec_t* add(int n);
  void rem(rec_t* rec);
  
  ///////////////////////////////////////////////////////

  filemap(filemap* p);
  filemap(filemap* p,const mstring& path,int update=0,int prot=0600);
  ~filemap();

  filemap* next;

  ///////////////////////////////////////////////////////

  void open(const mstring& path,int update=0,int prot=0600) throw(merror_t);
  void save(const mstring& path,int prot=0600) throw(merror_t);
  void check() throw(merror_t);
  void close();
  void sync();

  const mstring& Pathname() const { return pathname; }
  time_t Lastmod() const;

  int reference() const { return refs; }
  int writable() const { return wok; }

  void pushref() { refs++; }
  void popref() { refs--; }

  ///////////////////////////////////////////////////////

  void lock();
  void unlock();

  ///////////////////////////////////////////////////////

protected:
  struct chunk_t {
    chunk_t* next;
    long begin;
    long end;
    char* map;

    int size() const { return end-begin; }
    
    chunk_t(chunk_t* p,int fd,int wok,int off,int size);
    ~chunk_t();
    
    int own(int off) const { return off>=begin&&off<end; }
    void sync();

    rec_t* at(int off) { return (rec_t*)(map+(off-begin)); }
    const rec_t* at(int off) const { return (rec_t*)(map+(off-begin)); }

    int fill(int off);
  };
private:
  int fd,wok,refs;
  mstring pathname;
  head_t* hd;
  chunk_t* mem;
  mutex_t mutex;
  nodelock_t* nodelock;
private:
  filemap(const filemap&) {}
  void operator=(const filemap&) {}
};

//////////////////////////////////////////////////////

struct headlock_t {
  filemap* fm;
  headlock_t(filemap* f) : fm(f) { fm->lock(); }
  ~headlock_t() { if(fm) fm->unlock(); }
};

#endif