216825cde063ced933871f5687490c052144cb14
[idzebra-moved-to-github.git] / bfile / commit.c
1 /*
2  * Copyright (C) 1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: commit.c,v $
7  * Revision 1.1  1995-11-30 08:33:13  adam
8  * Started work on commit facility.
9  *
10  */
11
12 #include <assert.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <alexutil.h>
16
17 #include <mfile.h>
18 #include "cfile.h"
19
20 void cf_commit (CFile cf)
21 {
22     int i, r, bucket_no;
23     int hash_bytes;
24     struct CFile_ph_bucket *p;
25
26     if (cf->bucket_in_memory)
27     {
28         logf (LOG_FATAL, "Cannot commit potential dirty cache");
29         exit (1);
30     }
31     p = xmalloc (sizeof(*p));
32     hash_bytes = cf->head.hash_size * sizeof(int);
33     bucket_no = (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
34     if (lseek (cf->hash_fd, bucket_no * HASH_BSIZE, SEEK_SET) < 0)
35     {
36         logf (LOG_FATAL|LOG_ERRNO, "seek commit");
37         exit (1);
38     }
39     for (; bucket_no < cf->head.next_bucket; bucket_no++)
40     {
41         r = read (cf->hash_fd, p, HASH_BSIZE);
42         if (r != HASH_BSIZE)
43         {
44             logf (LOG_FATAL, "read commit hash");
45             exit (1);
46         }
47         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
48         {
49             if (lseek (cf->block_fd, p->vno[i]*cf->head.block_size,
50                 SEEK_SET) < 0)
51             {
52                 logf (LOG_FATAL, "lseek commit block");
53                 exit (1);
54             }
55             r = read (cf->block_fd, cf->iobuf, cf->head.block_size);
56             if (r != cf->head.block_size)
57             {
58                 logf (LOG_FATAL, "read commit block");
59                 exit (1);
60             }
61             mf_write (cf->mf, p->no[i], 0, 0, cf->iobuf);
62         }
63     }
64     xfree (p);
65 }
66