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
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
/* 
 * $Id: Roxen.pmod,v 1.7 1999/05/16 15:36:46 grubba Exp $ 
 * 
 * Various helper functions. 
 * 
 * Henrik Grubbström 1999-05-03 
 */ 
 
/* 
 * TODO: 
 * 
 * o Quota: Fix support for the index file. 
 * 
 */ 
 
#ifdef QUOTA_DEBUG 
#define QD_WRITE(X) werror(X) 
#else /* !QUOTA_DEBUG */ 
#define QD_WRITE(X) 
#endif /* QUOTA_DEBUG */ 
 
class QuotaDB 
{ 
#if constant(create_thread) 
  object(Thread.Mutex) lock = Thread.Mutex(); 
#define LOCK()                mixed key__; catch { _key = lock->lock(); } 
#define UNLOCK()    do { if (key__) destruct(key__); } while(0) 
#else /* !constant(create_thread) */ 
#define LOCK() 
#define UNLOCK() 
#endif /* constant(create_thread) */ 
 
  constant READ_BUF_SIZE = 256; 
  constant CACHE_SIZE_LIMIT = 512; 
 
  string base; 
 
  object catalog_file; 
  object data_file; 
 
  mapping(string:int) new_entries_cache = ([]); 
  mapping(string:object) active_objects = ([]); 
 
  array(int) index; 
  array(string) index_acc; 
  int acc_scale; 
 
  int next_offset; 
 
  static class QuotaEntry 
  { 
    string name; 
    int data_offset; 
 
    static int usage; 
    static int quota; 
 
    static void store() 
    { 
      LOCK(); 
 
      QD_WRITE(sprintf("QuotaEntry::store(): Usage for %O is now %O(%O)\n", 
                       name, usage, quota)); 
 
      data_file->seek(data_offset); 
      data_file->write(sprintf("%4c", usage)); 
 
      UNLOCK(); 
    } 
 
    static void read() 
    { 
      LOCK(); 
 
      data_file->seek(data_offset); 
      string s = data_file->read(4); 
 
      usage = 0; 
      sscanf(s, "%4c", usage); 
 
      QD_WRITE(sprintf("QuotaEntry::read(): Usage for %O is %O(%O)\n", 
                       name, usage, quota)); 
 
      UNLOCK(); 
    } 
 
    void create(string n, int d_o, int q) 
    { 
      QD_WRITE(sprintf("QuotaEntry(%O, %O, %O)\n", n, d_o, q)); 
 
      name = n; 
      data_offset = d_o; 
      quota = q; 
 
      read(); 
    } 
 
    int check_quota(string uri, int amount) 
    { 
      QD_WRITE(sprintf("QuotaEntry::check_quota(%O, %O): usage:%d(%d)\n", 
                       uri, amount, usage, quota)); 
 
      if (!quota) { 
        // No quota at all. 
        return 0; 
      } 
 
      if (amount == 0x7fffffff) { 
        // Workaround for FTP. 
        return 1; 
      } 
 
      return(usage + amount <= quota); 
    } 
 
    int allocate(string uri, int amount) 
    { 
      QD_WRITE(sprintf("QuotaEntry::allocate(%O, %O): usage:%d => %d(%d)\n", 
                       uri, amount, usage, usage + amount, quota)); 
 
      usage += amount; 
 
      store(); 
 
      return(usage <= quota); 
    } 
 
    int deallocate(string uri, int amount) 
    { 
      return(allocate(uri, -amount)); 
    } 
 
    int get_usage(string uri) 
    { 
      return usage; 
    } 
 
    void set_usage(string uri, int amount) 
    { 
      usage = amount; 
 
      store(); 
    } 
 
#if !constant(set_weak_flag) 
    static int refs; 
 
    void add_ref() 
    { 
      refs++; 
    } 
 
    void free_ref() 
    { 
      if (!(--refs)) { 
        destruct(); 
      } 
    } 
  } 
 
  static class QuotaProxy 
  { 
    static object(QuotaEntry) master; 
 
    function(string, int:int) check_quota; 
    function(string, int:int) allocate; 
    function(string, int:int) deallocate; 
    function(string, int:void) set_usage; 
    function(string:int) get_usage; 
 
    void create(object(QuotaEntry) m) 
    { 
      master = m; 
      master->add_ref(); 
      check_quota = master->check_quota; 
      allocate = master->allocate; 
      deallocate = master->deallocate; 
      set_usage = master->set_usage; 
      get_usage = master->get_usage; 
    } 
 
    void destroy() 
    { 
      master->free_ref(); 
    } 
#endif /* !constant(set_weak_flag) */ 
  } 
 
  static object read_entry(int offset, int|void quota) 
  { 
    QD_WRITE(sprintf("QuotaDB::read_entry(%O, %O)\n", offset, quota)); 
 
    catalog_file->seek(offset); 
 
    string data = catalog_file->read(READ_BUF_SIZE); 
 
    if (data == "") { 
      QD_WRITE(sprintf("QuotaDB::read_entry(%O, %O): At EOF\n", 
                       offset, quota)); 
 
      return 0; 
    } 
 
    int len; 
    int data_offset; 
    string key; 
 
    sscanf(data[..7], "%4c%4c", len, data_offset); 
    if (len > sizeof(data)) { 
      key = data[8..] + catalog_file->read(len - sizeof(data)); 
 
      len -= 8; 
 
      if (sizeof(key) != len) { 
        error(sprintf("Failed to read catalog entry at offset %d.\n" 
                      "len: %d, sizeof(key):%d\n", 
                      offset, len, sizeof(key))); 
      } 
    } else { 
      key = data[8..len-1]; 
      catalog_file->seek(offset + 8 + sizeof(key)); 
    } 
 
    return QuotaEntry(key, data_offset, quota); 
  } 
 
  static object open(string fname, int|void create_new) 
  { 
    object f = Stdio.File(); 
    string mode = create_new?"rwc":"rw"; 
 
    if (!f->open(fname, mode)) { 
      error(sprintf("Failed to open quota file %O.\n", fname)); 
    } 
    if (f->try_lock && !f->try_lock()) { 
      error(sprintf("Failed to lock quota file %O.\n", fname)); 
    } 
    return(f); 
  } 
 
  static void init_index_acc() 
  { 
    /* Set up the index accellerator. 
     * sizeof(index_acc) ~ sqrt(sizeof(index)) 
     */ 
    acc_scale = 1; 
    if (sizeof(index)) { 
      int i = sizeof(index)/2; 
 
      while (i) { 
        i /= 4; 
        acc_scale <<= 1; 
      } 
    } 
    index_acc = allocate(sizeof(index)/acc_scale); 
 
    QD_WRITE(sprintf("QuotaDB()::init_index_acc(): " 
                     "sizeof(index):%d, sizeof(index_acc):%d acc_scale:%d\n", 
                     sizeof(index), sizeof(index_acc), acc_scale)); 
  } 
 
  void rebuild_index() 
  { 
    // FIXME: Actually make an index file. 
    array(string) new_keys = sort(indices(new_entries_cache)); 
 
    int prev; 
    array(int) new_index = ({}); 
 
    foreach(new_keys, string key) { 
      int lo; 
      int hi = sizeof(index_acc); 
      if (hi) { 
        do { 
          // Loop invariants: 
          //   hi is an element > key. 
          //   lo-1 is an element < key. 
 
          int probe = (lo + hi)/2; 
 
          if (!index_acc[probe]) { 
            object e = read_entry(index[probe * acc_scale]); 
 
            index_acc[probe] = e->name; 
          } 
          if (index_acc[probe] < key) { 
            lo = probe + 1; 
          } else if (index_acc[probe] > key) { 
            hi = probe; 
          } else { 
            /* Found */ 
            // Shouldn't happen... 
            break; 
          } 
        } while(lo < hi); 
 
        if (lo < hi) { 
          // Found... 
          // Shouldn't happen, but... 
          // Skip to the next key... 
          continue; 
        } 
        hi *= acc_scale; 
        lo = hi - acc_scale; 
 
        do { 
          // Same loop invariants as above. 
 
          int probe = (lo + hi)/2; 
          object e = read_entry(index[probe]); 
          if (e->name < key) { 
            lo = probe + 1; 
          } else if (e->name > key) { 
            hi = probe; 
          } else { 
            /* Found */ 
            // Shouldn't happen... 
            break; 
          } 
        } while (lo < hi); 
        if (lo < hi) { 
          // Found... 
          // Shouldn't happen, but... 
          // Skip to the next key... 
          continue; 
        } 
        new_index += index[prev..hi-1] + ({ new_entries_cache[key] }); 
        prev = hi; 
      } else { 
        new_index += ({ new_entries_cache[key] }); 
      } 
    } 
 
    LOCK(); 
 
    object index_file = open(base + ".index.new", 1); 
    string to_write = sprintf("%@4c", new_index); 
    if (index_file->write(to_write) != sizeof(to_write)) { 
      index_file->close(); 
      rm(base + ".index.new"); 
    } else { 
      mv(base + ".index.new", base + ".index"); 
    } 
 
    index = new_index; 
    init_index_acc(); 
 
    UNLOCK(); 
 
    foreach(new_keys, string key) { 
      m_delete(new_entries_cache, key); 
    } 
  } 
 
  static object low_lookup(string key, int quota) 
  { 
    QD_WRITE(sprintf("QuotaDB::low_lookup(%O, %O)\n", key, quota)); 
 
    int cat_offset; 
 
    if (!zero_type(cat_offset = new_entries_cache[key])) { 
      QD_WRITE(sprintf("QuotaDB::low_lookup(%O, %O): " 
                       "Found in new entries cache.\n", key, quota)); 
      return read_entry(cat_offset, quota); 
    } 
 
    /* Try the index file. */ 
 
    /* First use the accellerated index. */ 
    int lo; 
    int hi = sizeof(index_acc); 
    if (hi) { 
      do { 
        // Loop invariants: 
        //   hi is an element that is > key. 
        //   lo-1 is an element that is < key. 
        int probe = (lo + hi)/2; 
 
        QD_WRITE(sprintf("QuotaDB:low_lookup(%O): " 
                         "In acc: lo:%d, probe:%d, hi:%d\n", 
                         key, lo, probe, hi)); 
 
        if (!index_acc[probe]) { 
          object e = read_entry(index[probe * acc_scale], quota); 
 
          index_acc[probe] = e->name; 
 
          if (key == e->name) { 
            /* Found in e */ 
            QD_WRITE(sprintf("QuotaDB:low_lookup(%O): In acc: Found at %d\n", 
                             key, probe * acc_scale)); 
            return e; 
          } 
        } 
        if (index_acc[probe] < key) { 
          lo = probe + 1; 
        } else if (index_acc[probe] > key) { 
          hi = probe; 
        } else { 
          /* Found */ 
          QD_WRITE(sprintf("QuotaDB:low_lookup(%O): In acc: Found at %d\n", 
                           key, probe * acc_scale)); 
          return read_entry(index[probe * acc_scale], quota); 
        } 
      } while(lo < hi); 
      // At this point hi is the first element that is > key. 
      // Not in the accellerated index. 
 
      if (hi) { 
        // Go to disk 
 
        hi *= acc_scale; 
        lo = hi - acc_scale; 
        do { 
          // Same loop invariant as above. 
 
          int probe = (lo + hi)/2; 
 
          QD_WRITE(sprintf("QuotaDB:low_lookup(%O): lo:%d, probe:%d, hi:%d\n", 
                           key, lo, probe, hi)); 
 
          object e = read_entry(index[probe], quota); 
         
          if (e->name < key) { 
            lo = probe + 1; 
          } else if (e->name > key) { 
            hi = probe; 
          } else { 
            /* Found */ 
            QD_WRITE(sprintf("QuotaDB:low_lookup(%O): Found at %d\n", 
                             key, probe)); 
            return e; 
          } 
        } while (lo < hi); 
      } 
    } 
 
    QD_WRITE(sprintf("QuotaDB::low_lookup(%O): Not found\n", key)); 
 
    return 0; 
  } 
 
  object lookup(string key, int quota) 
  { 
    QD_WRITE(sprintf("QuotaDB::lookup(%O, %O)\n", key, quota)); 
 
    LOCK(); 
 
    object res; 
 
    if (res = active_objects[key]) { 
      QD_WRITE(sprintf("QuotaDB::lookup(%O, %O): User in active objects.\n", 
                       key, quota)); 
 
#if constant(set_weak_flag) 
      return res; 
#else /* !constant(set_weak_flag) */ 
      return QuotaProxy(res); 
#endif /* constant(set_weak_flag) */ 
    } 
    if (res = low_lookup(key, quota)) { 
      active_objects[key] = res; 
 
#if constant(set_weak_flag) 
      return res; 
#else /* !constant(set_weak_flag) */ 
      return QuotaProxy(res); 
#endif /* constant(set_weak_flag) */ 
    } 
 
    QD_WRITE(sprintf("QuotaDB::lookup(%O, %O): New user.\n", key, quota)); 
 
    // Search to EOF. 
    data_file->seek(-1); 
    data_file->read(1); 
 
    catalog_file->seek(next_offset); 
 
    // We should now be at EOF. 
 
    int data_offset = data_file->tell(); 
 
    // Initialize. 
    if (data_file->write(sprintf("%4c", 0)) != 4) { 
      error(sprintf("write() failed for quota data file!\n")); 
    } 
    string entry = sprintf("%4c%4c%s", sizeof(key)+8, data_offset, key); 
 
    if (catalog_file->write(entry) != sizeof(entry)) { 
      error(sprintf("write() failed for quota catalog file!\n")); 
    } 
 
    new_entries_cache[key] = next_offset; 
    next_offset = catalog_file->tell(); 
 
    if (sizeof(new_entries_cache) > CACHE_SIZE_LIMIT) { 
      rebuild_index(); 
    } 
 
    // low_lookup will always succeed at this point. 
    return low_lookup(key, quota); 
  } 
 
  void create(string base_name, int|void create_new) 
  { 
    base = base_name; 
 
    catalog_file = open(base_name + ".cat", create_new); 
    data_file = open(base_name + ".data", create_new); 
    object index_file = open(base_name + ".index", 1); 
 
#if constant(set_weak_flag) 
    set_weak_flag(active_objects, 1); 
#endif /* constant(set_weak_flag) */ 
 
    /* Initialize the new_entries table. */ 
    array index_st = index_file->stat(); 
    if (!index_st || !sizeof(index_st)) { 
      error(sprintf("stat() failed for quota index file!\n")); 
    } 
    array data_st = data_file->stat(); 
    if (!data_st || !sizeof(data_st)) { 
      error(sprintf("stat() failed for quota data file!\n")); 
    } 
    if (index_st[1] < 0) { 
      error("quota index file isn't a regular file!\n"); 
    } 
    if (data_st[1] < 0) { 
      error("quota data file isn't a regular file!\n"); 
    } 
    if (data_st[1] < index_st[1]) { 
      error("quota data file is shorter than the index file!\n"); 
    } 
    if (index_st[1] & 3) { 
      error("quota index file has odd length!\n"); 
    } 
    if (data_st[1] & 3) { 
      error("quota data file has odd length!\n"); 
    } 
 
    /* Read the index, and find the last entry in the catalog file. 
     */ 
    int i; 
    array(string) index_str = index_file->read()/4; 
    index = allocate(sizeof(index_str)); 
 
    if (sizeof(index_str) && (sizeof(index_str[-1]) != 4)) { 
      error("Truncated read of the index file!\n"); 
    } 
 
    foreach(index_str, string offset_str) { 
      int offset; 
      sscanf(offset_str, "%4c", offset); 
      index[i++] = offset; 
      if (offset > next_offset) { 
        next_offset = offset; 
      } 
    } 
 
    init_index_acc(); 
 
    if (sizeof(index)) { 
      /* Skip past the last entry in the catalog file */ 
      mixed entry = read_entry(next_offset); 
      next_offset = catalog_file->tell(); 
    } 
 
    if (index_st[1] < data_st[1]) { 
      /* Put everything else in the new_entries_cache */ 
      while (mixed entry = read_entry(next_offset)) { 
        new_entries_cache[entry->name] = next_offset; 
        next_offset = catalog_file->tell(); 
      } 
 
      /* Clean up the index. */ 
      rebuild_index(); 
    } 
  } 
}