summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/package.cc
blob: f8e5f9c53af0d28b496355b54d69fef3f5b3fb73 (plain) (blame)
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/**
 * @file
 * @brief A container for chunks that make up the save file.
**/

/*
Guarantees:
* A crash at any moment may not cause corruption -- the save will return to
  the exact state it had at the last commit().

Notes:
* Unless DO_FSYNC is defined, crashes that put down the operating system
  may break the consistency guarantee.
* Incomplete writes don't have any effects, but don't break commits or reads
  (which both use the last complete write).
* Readers always get the last complete (but not necessarily committed) write
  (ie, READ_UNCOMMITTED) at the time they started; it is safe to continue
  reading even if the chunk has been changed since.
*/

#include "AppHdr.h"

#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sstream>

#include "package.h"
#include "endianness.h"
#include "errors.h"
#include "syscalls.h"

#if !defined(DGAMELAUNCH) && !defined(__ANDROID__) && !defined(DEBUG_DIAGNOSTICS)
#define DO_FSYNC
#endif

// debugging defines
#undef  FSCK_VERBOSE
#undef  COSTLY_ASSERTS
#undef  DEBUG_PACKAGE

static plen_t htole(plen_t x)
{
    if (sizeof(plen_t) == 4)
        return htole32(x);
    if (sizeof(plen_t) == 8)
        return htole64(x);
    die("unsupported plen_t size");
}

#ifdef DEBUG_PACKAGE
#define dprintf(...) printf(__VA_ARGS__)
#else
#define dprintf(...) do {} while (0)
#endif

#define PACKAGE_VERSION 1
#define PACKAGE_MAGIC   0x53534344 /* "DCSS" */

struct file_header
{
    uint32_t magic;
    uint8_t version;
    char padding[3];
    plen_t start;
};

struct block_header
{
    plen_t len;
    plen_t next;
};

typedef map<string, plen_t> directory_t;
typedef pair<plen_t, plen_t> bm_p;
typedef map<plen_t, bm_p> bm_t;
typedef map<plen_t, plen_t> fb_t;

package::package(const char* file, bool writeable, bool empty)
  : n_users(0), dirty(false), aborted(false), tmp(false)
{
    dprintf("package: initializing file=\"%s\" rw=%d\n", file, writeable);
    ASSERT(writeable || !empty);
    filename = file;
    rw = writeable;

    if (empty)
    {
        fd = open_u(file, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
        if (fd == -1)
            sysfail("can't create save file (%s)", file);

        if (!lock_file(fd, true))
        {
            close(fd);
            sysfail("failed to lock newly created save (%s)", file);
        }

        dirty = true;
        file_len = sizeof(file_header);
    }
    else
    {
        fd = open_u(file, (writeable? O_RDWR : O_RDONLY) | O_BINARY, 0666);
        if (fd == -1)
            sysfail("can't open save file (%s)", file);

        try
        {
            if (!lock_file(fd, writeable))
                fail("Another game is already in progress using this save!");

            load();
        }
        catch (exception &e)
        {
            close(fd);
            throw;
        }
    }
}

package::package()
  : rw(true), n_users(0), dirty(false), aborted(false), tmp(true)
{
    dprintf("package: initializing tmp file\n");
    filename = "[tmp]";

    char file[7] = "XXXXXX";
    fd = mkstemp(file);
    if (fd == -1)
        sysfail("can't create temporary save file");

    ::unlink(file); // FIXME: won't work on Windows

    if (!lock_file(fd, true))
    {
        close(fd);
        sysfail("failed to lock newly created save (%s)", file);
    }

    dirty = true;
    file_len = sizeof(file_header);
}

void package::load()
{
    file_header head;
    ssize_t res = ::read(fd, &head, sizeof(file_header));
    if (res < 0)
        sysfail("error reading the save file (%s)", filename.c_str());
    if (!res || !(head.magic || head.version || head.padding[0]
                  || head.padding[1] || head.padding[2] || head.start))
    {
        corrupted("The save file (%s) is empty!", filename.c_str());
    }
    if (res != sizeof(file_header))
        corrupted("save file (%s) corrupted -- header truncated", filename.c_str());

    if (htole(head.magic) != PACKAGE_MAGIC)
    {
        corrupted("save file (%s) corrupted -- not a DCSS save file",
             filename.c_str());
    }
    off_t len = lseek(fd, 0, SEEK_END);
    if (len == -1)
        sysfail("save file (%s) is not seekable", filename.c_str());
    file_len = len;
    read_directory(htole(head.start), head.version);

    if (rw)
        load_traces();
}

void package::load_traces()
{
    ASSERT(!dirty);
    ASSERT(!n_users);
    if (directory.empty() || !block_map.empty())
        return;

    free_blocks[sizeof(file_header)] = file_len - sizeof(file_header);

    for (directory_t::iterator ch = directory.begin();
        ch != directory.end(); ++ch)
    {
        trace_chunk(ch->second);
    }
#ifdef COSTLY_ASSERTS
    // any inconsitency in the save is guaranteed to be already found
    // by this time -- this checks only for internal bugs
    fsck();
#endif
}

package::~package()
{
    dprintf("package: finalizing\n");
    ASSERT(!n_users || CrawlIsCrashing); // not merely aborted, there are
        // live pointers to us.  With normal stack unwinding, destructors
        // will make sure this never happens and this assert is good for
        // catching missing manual deletes.  The C++ exit handler is the
        // only place that can be legitimately call things in wrong order.

    if (rw && !aborted)
    {
        commit();
        if (ftruncate(fd, file_len))
            sysfail("failed to update save file");
    }

    // all errors here should be cached write errors
    if (fd != -1)
        if (close(fd) && !aborted)
        {
            sysfail(rw ? "write error while saving"
                       : "can't close the save I've just read???");
        }
    dprintf("package: closed\n");
}

void package::commit()
{
    ASSERT(rw);
    if (!dirty)
        return;
    ASSERT(!aborted);

#ifdef COSTLY_ASSERTS
    fsck();
#endif

    file_header head;
    head.magic = htole(PACKAGE_MAGIC);
    head.version = PACKAGE_VERSION;
    memset(&head.padding, 0, sizeof(head.padding));
    head.start = htole(write_directory());
#ifdef DO_FSYNC
    // We need a barrier before updating the link to point at the new directory.
    if (!tmp && fdatasync(fd))
        sysfail("flush error while saving");
#endif
    seek(0);
    if (write(fd, &head, sizeof(head)) != sizeof(head))
        sysfail("write error while saving");
#ifdef DO_FSYNC
    if (!tmp && fdatasync(fd))
        sysfail("flush error while saving");
#endif

    new_chunks.clear();
    collect_blocks();
    dirty = false;

#ifdef COSTLY_ASSERTS
    fsck();
#endif
}

void package::seek(plen_t to)
{
    ASSERT(!aborted);

    if (to > file_len)
        corrupted("save file corrupted -- invalid offset");
    if (lseek(fd, to, SEEK_SET) != (off_t)to)
        sysfail("failed to seek inside the save file");
}

chunk_writer* package::writer(const string name)
{
    return new chunk_writer(this, name);
}

chunk_reader* package::reader(const string name)
{
    directory_t::iterator ch = directory.find(name);
    if (ch == directory.end())
        return 0;
    return new chunk_reader(this, ch->second);
}

plen_t package::extend_block(plen_t at, plen_t size, plen_t by)
{
    // the header is not counted into the block's size, yet takes space
    size += sizeof(block_header);
    if (at + size == file_len)
    {
        // at the end of file
        file_len += by;
        return by;
    }

    fb_t::iterator bl = free_blocks.find(at + size);
    if (bl == free_blocks.end())
        return 0;

    plen_t free = bl->second;
    dprintf("reusing %u of %u at %u\n", by, free, bl->first);
    if (free <= by)
    {
        // we consume the entire block
        by = free;
        free_blocks.erase(bl);
        return by;
    }

    free_blocks.erase(bl);
    free_blocks[at + size + by] = free - by;

    return by;
}

plen_t package::alloc_block(plen_t &size)
{
    fb_t::iterator bl, best_big, best_small;
    plen_t bb_size = (plen_t)-1, bs_size = 0;
    for (bl = free_blocks.begin(); bl!=free_blocks.end(); ++bl)
    {
        if (bl->second < bb_size && bl->second >= size + sizeof(block_header))
            best_big = bl, bb_size = bl->second;
        // don't reuse very small blocks unless they're big enough
        else if (bl->second >= 16 && bl->second > bs_size)
            best_small = bl, bs_size = bl->second;
    }
    if (bb_size != (plen_t)-1)
        bl = best_big;
    else if (bs_size != 0)
        bl = best_small;
    else
    {
        plen_t at = file_len;

        file_len += sizeof(block_header) + size;
        return at;
    }

    plen_t at = bl->first;
    plen_t free = bl->second;
    dprintf("found a block for reuse at %u size %u\n", at, free);
    free_blocks.erase(bl);
    free -= sizeof(block_header);
    if (size > free)
        size = free;
    if ((free -= size))
        free_blocks[at + sizeof(block_header) + size] = free;

    return at;
}

void package::finish_chunk(const string name, plen_t at)
{
    free_chunk(name);
    directory[name] = at;
    new_chunks.insert(at);
    dirty = true;
}

void package::free_chunk(const string name)
{
    directory_t::iterator ci = directory.find(name);
    if (ci == directory.end())
        return;

    dprintf("freeing chunk(%s)\n", name.c_str());
    if (new_chunks.count(ci->second))
        free_block_chain(ci->second);
    else // can't free committed blocks yet
        unlinked_blocks.push_back(ci->second);

    dirty = true;
}

void package::delete_chunk(const string name)
{
    free_chunk(name);
    directory.erase(name);
}

plen_t package::write_directory()
{
    delete_chunk("");

    stringstream dir;
    for (directory_t::iterator i = directory.begin();
         i != directory.end(); ++i)
    {
        uint8_t name_len = i->first.length();
        dir.write((const char*)&name_len, sizeof(name_len));
        dir.write(&i->first[0], i->first.length());
        plen_t start = htole(i->second);
        dir.write((const char*)&start, sizeof(plen_t));
    }

    ASSERT(dir.str().size());
    dprintf("writing directory (%u bytes)\n", (unsigned int)dir.str().size());
    {
        chunk_writer dch(this, "");
        dch.write(&dir.str()[0], dir.str().size());
    }

    return directory[""];
}

void package::collect_blocks()
{
    for (ssize_t i = unlinked_blocks.size() - 1; i >= 0; --i)
    {
        plen_t at = unlinked_blocks[i];
        // Blocks may be re-added onto the list if they're in use.
        if (i != (ssize_t)unlinked_blocks.size() - 1)
            unlinked_blocks[i] = unlinked_blocks[unlinked_blocks.size() - 1];
        unlinked_blocks.pop_back();
        free_block_chain(at);
    }
}

void package::free_block_chain(plen_t at)
{
    if (reader_count.count(at))
    {
        dprintf("deleting an in-use chain at %d\n", at);
        unlinked_blocks.push_back(at);
        return;
    }

    dprintf("freeing an unlinked chain at %d\n", at);
    while (at)
    {
        bm_t::iterator bl = block_map.find(at);
        ASSERT(bl != block_map.end());
        dprintf("+- at %d size=%d+header\n", at, bl->second.first);
        free_block(at, bl->second.first + sizeof(block_header));
        at = bl->second.second;
        block_map.erase(bl);
    }
}

void package::free_block(plen_t at, plen_t size)
{
    ASSERT(at >= sizeof(file_header));
    ASSERT(at + size <= file_len);

    fb_t::iterator neigh;

    neigh = free_blocks.lower_bound(at);
    if (neigh != free_blocks.begin())
    {
        --neigh;
        ASSERT(neigh->first + neigh->second <= at);
        if (neigh->first + neigh->second == at)
        {
            // combine with the left neighbour
            at = neigh->first;
            size += neigh->second;
            free_blocks.erase(neigh);
        }
    }

    neigh = free_blocks.lower_bound(at);
    if (neigh != free_blocks.end())
    {
        ASSERT(neigh->first >= at + size);
        if (neigh->first == at + size)
        {
            // combine with the right neighbour
            size += neigh->second;
            free_blocks.erase(neigh);
        }
    }

    if (at + size == file_len)
        file_len -= size;
    else
        free_blocks[at] = size;
}

void package::fsck()
{
    fb_t  save_free_blocks = free_blocks;
    plen_t save_file_len = file_len;

#ifdef FSCK_VERBOSE
    printf("Fsck starting.  %u chunks, %u used blocks, %u free ones, file size %u\n",
           (unsigned int)directory.size(), (unsigned int)block_map.size(),
           (unsigned int)free_blocks.size(), file_len);

    for (fb_t::const_iterator bl = free_blocks.begin(); bl != free_blocks.end();
        ++bl)
    {
        printf("<at %u size %u>\n", bl->first, bl->second);
    }
#endif
    for (bm_t::const_iterator bl = block_map.begin(); bl != block_map.end();
        ++bl)
    {
#ifdef FSCK_VERBOSE
        printf("[at %u size %u+header]\n", bl->first, bl->second.first);
#endif
        free_block(bl->first, bl->second.first + sizeof(block_header));
    }
    // after freeing everything, the file should be empty
    ASSERT(free_blocks.empty());
    ASSERT(file_len == sizeof(file_header));

    free_blocks = save_free_blocks;
    file_len = save_file_len;
}

struct dir_entry0
{
    char name[sizeof(plen_t)];
    plen_t start;
};

void package::read_directory(plen_t start, uint8_t version)
{
    ASSERT(directory.empty());
    directory[""] = start;

    dprintf("package: reading directory\n");
    chunk_reader rd(this, start);

    switch (version)
    {
    case 0:
        dir_entry0 ch0;
        while (plen_t res = rd.read(&ch0, sizeof(dir_entry0)))
        {
            if (res != sizeof(dir_entry0))
                corrupted("save file corrupted -- truncated directory");
            string chname(ch0.name, 4);
            chname.resize(strlen(chname.c_str()));
            directory[chname] = htole(ch0.start);
            dprintf("* %s\n", chname.c_str());
        }
        break;
    case 1:
        uint8_t name_len;
        plen_t bstart;
        while (plen_t res = rd.read(&name_len, sizeof(name_len)))
        {
            if (res != sizeof(name_len))
                corrupted("save file corrupted -- truncated directory");
            string chname;
            chname.resize(name_len);
            if (rd.read(&chname[0], name_len) != name_len)
                corrupted("save file corrupted -- truncated directory");
            if (rd.read(&bstart, sizeof(bstart)) != sizeof(bstart))
                corrupted("save file corrupted -- truncated directory");
            directory[chname] = htole(bstart);
            dprintf("* %s\n", chname.c_str());
        }
        break;
    default:
        corrupted("save file (%s) uses an unknown format %u", filename.c_str(),
             version);
    }
}

bool package::has_chunk(const string name)
{
    return !name.empty() && directory.count(name);
}

vector<string> package::list_chunks()
{
    vector<string> list;
    list.reserve(directory.size());
    for (directory_t::iterator i = directory.begin();
         i != directory.end(); ++i)
    {
        if (!i->first.empty())
            list.push_back(i->first);
    }
    return list;
}

void package::trace_chunk(plen_t start)
{
    while (start)
    {
        block_header bl;
        seek(start);
        ssize_t res = ::read(fd, &bl, sizeof(block_header));
        if (res < 0)
            sysfail("error reading the save file");
        if (res != sizeof(block_header))
            corrupted("save file corrupted -- block past eof");

        plen_t len  = htole(bl.len);
        plen_t next = htole(bl.next);
        plen_t end  = start + len + sizeof(block_header);
        dprintf("{at %u size %u+header}\n", start, len);

        fb_t::iterator sp = free_blocks.upper_bound(start);
        if (sp == free_blocks.begin())
            corrupted("save file corrupted -- overlapping blocks");
        --sp;
        plen_t sp_start = sp->first;
        plen_t sp_size  = sp->second;
        if (sp_start > start || sp_start + sp_size < end)
            corrupted("save file corrupted -- overlapping blocks");
        free_blocks.erase(sp);
        if (sp_start < start)
            free_blocks[sp_start] = start - sp_start;
        if (sp_start + sp_size > end)
            free_blocks[end] = sp_start + sp_size - end;

        block_map[start] = bm_p(len, next);
        start = next;
    }
}

void package::abort()
{
    // Disable any further operations, allow a shutdown.  All errors past
    // this point are ignored (assuming we already failed).  All writes since
    // the last commit() are lost.
    aborted = true;
}

void package::unlink()
{
    abort();
    close(fd);
    fd = -1;
    ::unlink_u(filename.c_str());
}

// the amount of free space not at the end of file
plen_t package::get_slack()
{
    load_traces();

    plen_t slack = 0;
    for (fb_t::iterator bl = free_blocks.begin(); bl!=free_blocks.end(); ++bl)
        slack += bl->second;
    return slack;
}

plen_t package::get_chunk_fragmentation(const string name)
{
    load_traces();
    ASSERT(directory.count(name)); // not has_chunk(), "" is valid
    plen_t frags = 0;
    plen_t at = directory[name];
    while (at)
    {
        bm_t::iterator bl = block_map.find(at);
        ASSERT(bl != block_map.end());
        frags ++;
        at = bl->second.second;
    }
    return frags;
}

plen_t package::get_chunk_compressed_length(const string name)
{
    load_traces();
    ASSERT(directory.count(name)); // not has_chunk(), "" is valid
    plen_t len = 0;
    plen_t at = directory[name];
    while (at)
    {
        bm_t::iterator bl = block_map.find(at);
        ASSERT(bl != block_map.end());
        len += bl->second.first;
        at = bl->second.second;
    }
    return len;
}

chunk_writer::chunk_writer(package *parent, const string _name)
    : first_block(0), cur_block(0), block_len(0)
{
    ASSERT(parent);
    ASSERT(!parent->aborted);

    // If you need more, please change {read,write}_directory().
    ASSERT(MAX_CHUNK_NAME_LENGTH < 256);
    ASSERT(_name.length() < MAX_CHUNK_NAME_LENGTH);

    dprintf("chunk_writer(%s): starting\n", _name.c_str());
    pkg = parent;
    pkg->n_users++;
    name = _name;

#ifdef USE_ZLIB
    zs.data_type = Z_BINARY;
    zs.zalloc    = 0;
    zs.zfree     = 0;
    zs.opaque    = Z_NULL;
    if (deflateInit(&zs, Z_DEFAULT_COMPRESSION))
        fail("save file compression failed during init: %s", zs.msg);
#define ZB_SIZE 32768
    zs.next_out  = z_buffer = (Bytef*)malloc(ZB_SIZE);
    zs.avail_out = ZB_SIZE;
#endif
}

chunk_writer::~chunk_writer()
{
    dprintf("chunk_writer(%s): closing\n", name.c_str());

    ASSERT(pkg->n_users > 0);
    pkg->n_users--;
    if (pkg->aborted)
    {
#ifdef USE_ZLIB
        // ignore errors, they're not relevant anymore
        deflateEnd(&zs);
        free(z_buffer);
#endif
        return;
    }

#ifdef USE_ZLIB
    zs.avail_in = 0;
    int res;
    do
    {
        res = deflate(&zs, Z_FINISH);
        if (res != Z_STREAM_END && res != Z_OK && res != Z_BUF_ERROR)
            fail("save file compression failed: %s", zs.msg);
        raw_write(z_buffer, zs.next_out - z_buffer);
        zs.next_out = z_buffer;
        zs.avail_out = ZB_SIZE;
    } while (res != Z_STREAM_END);
    if (deflateEnd(&zs) != Z_OK)
        fail("save file compression failed during clean-up: %s", zs.msg);
    free(z_buffer);
#endif
    if (cur_block)
        finish_block(0);
    pkg->finish_chunk(name, first_block);
}

void chunk_writer::raw_write(const void *data, plen_t len)
{
    while (len > 0)
    {
        plen_t space = pkg->extend_block(cur_block, block_len, len);
        if (!space)
        {
            plen_t next_block = pkg->alloc_block(space = len);
            ASSERT(space > 0);
            if (cur_block)
                finish_block(next_block);
            cur_block = next_block;
            if (!first_block)
                first_block = next_block;
            block_len = 0;
        }

        pkg->seek(cur_block + block_len + sizeof(block_header));
        if (::write(pkg->fd, data, space) != (ssize_t)space)
            sysfail("write error while saving");
        data = (char*)data + space;
        block_len += space;
        len -= space;
    }
}

void chunk_writer::finish_block(plen_t next)
{
    block_header head;
    head.len = htole(block_len);
    head.next = htole(next);

    pkg->seek(cur_block);
    if (::write(pkg->fd, &head, sizeof(head)) != sizeof(head))
        sysfail("write error while saving");

    pkg->block_map[cur_block] = bm_p(block_len, next);
}

void chunk_writer::write(const void *data, plen_t len)
{
    ASSERT(data);
    ASSERT(!pkg->aborted);

#ifdef USE_ZLIB
    zs.next_in  = (Bytef*)data;
    zs.avail_in = len;
    while (zs.avail_in)
    {
        if (!zs.avail_out)
        {
            raw_write(z_buffer, zs.next_out - z_buffer);
            zs.next_out  = z_buffer;
            zs.avail_out = ZB_SIZE;
        }
        // we don't allow Z_BUF_ERROR, so it's fatal for us
        if (deflate(&zs, Z_NO_FLUSH) != Z_OK)
            fail("save file compression failed: %s", zs.msg);
    }
#else
    raw_write(data, len);
#endif
}

void chunk_reader::init(plen_t start)
{
    ASSERT(!pkg->aborted);
    pkg->n_users++;
    pkg->reader_count[start]++;
    first_block = next_block = start;
    block_left = 0;

#ifdef USE_ZLIB
    if (!start)
        corrupted("save file corrupted -- zlib header missing");

    zs.zalloc    = 0;
    zs.zfree     = 0;
    zs.opaque    = Z_NULL;
    zs.next_in   = Z_NULL;
    zs.avail_in  = 0;
    if (inflateInit(&zs))
        fail("save file decompression failed during init: %s", zs.msg);
    eof = false;
#endif
}

chunk_reader::chunk_reader(package *parent, plen_t start)
{
    ASSERT(parent);
    dprintf("chunk_reader[%u]: starting\n", start);
    pkg = parent;
    init(start);
}

chunk_reader::chunk_reader(package *parent, const string _name)
{
    ASSERT(parent);
    if (!parent->has_chunk(_name))
        corrupted("save file corrupted -- chunk \"%s\" missing", _name.c_str());
    dprintf("chunk_reader(%s): starting\n", _name.c_str());
    pkg = parent;
    init(parent->directory[_name]);
}

chunk_reader::~chunk_reader()
{
    dprintf("chunk_reader: closing\n");

#ifdef USE_ZLIB
    if (inflateEnd(&zs) != Z_OK)
        fail("save file decompression failed during clean-up: %s", zs.msg);
#endif
    ASSERT(pkg->reader_count[first_block] > 0);
    if (!--pkg->reader_count[first_block])
        pkg->reader_count.erase(first_block);
    ASSERT(pkg->n_users > 0);
    pkg->n_users--;
}

plen_t chunk_reader::raw_read(void *data, plen_t len)
{
    void *buf = data;
    while (len)
    {
        if (!block_left)
        {
            if (!next_block)
                return (char*)buf - (char*)data;

            block_header bl;
            pkg->seek(next_block);
            ssize_t res = ::read(pkg->fd, &bl, sizeof(block_header));
            if (res < 0)
                sysfail("error reading the save file");
            if (res != sizeof(block_header))
                corrupted("save file corrupted -- block past eof");

            off = next_block + sizeof(block_header);
            block_left = htole(bl.len);
            next_block = htole(bl.next);
            // This reeks of on-disk corruption (zeroed data).
            if (!block_left)
                corrupted("save file corrupted -- empty block");
        }
        else
            pkg->seek(off);

        plen_t s = len;
        if (s > block_left)
            s = block_left;
        ssize_t res = ::read(pkg->fd, buf, s);
        if (res < 0)
            sysfail("error reading the save file");
        if ((plen_t)res != s)
            corrupted("save file corrupted -- block past eof");

        buf = (char*)buf + s;
        off += s;
        len -= s;
        block_left -= s;
    }

    return (char*)buf - (char*)data;
}

plen_t chunk_reader::read(void *data, plen_t len)
{
    ASSERT(data);
    if (pkg->aborted)
        return 0;

#ifdef USE_ZLIB
    if (!len)
        return 0;
    if (eof)
        return 0;

    zs.next_out  = (Bytef*)data;
    zs.avail_out = len;
    while (zs.avail_out)
    {
        if (!zs.avail_in)
        {
            zs.next_in  = z_buffer;
            zs.avail_in = raw_read(z_buffer, sizeof(z_buffer));
            if (!zs.avail_in)
                corrupted("save file corrupted -- block truncated");
        }
        int res = inflate(&zs, Z_NO_FLUSH);
        if (res == Z_STREAM_END)
        {
            eof = true;
            return zs.next_out - (Bytef*)data;
        }
        if (res != Z_OK)
            corrupted("save file decompression failed: %s", zs.msg);
    }
    return zs.next_out - (Bytef*)data;
#else
    return raw_read(data, len);
#endif
}

void chunk_reader::read_all(vector<char> &data)
{
#define SPACE 1024
    plen_t s, at;
    do
    {
        at = data.size();
        data.resize(at + SPACE);
        s = read(&data[at], SPACE);
    } while (s == SPACE);
    data.resize(at + s);
#undef SPACE
}