Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-block-file.h
1 /*
2  * Copyright (C) 2001 Ximian Inc.
3  *
4  * Authors: Michael Zucchi <notzed@ximian.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef _CAMEL_BLOCK_FILE_H
22 #define _CAMEL_BLOCK_FILE_H
23
24 #include <camel/camel-object.h>
25 #include <glib.h>
26 #include <libedataserver/e-msgport.h>
27 #include <stdio.h>
28
29 G_BEGIN_DECLS
30
31 typedef guint32 camel_block_t;  /* block offset, absolute, bottom BLOCK_SIZE_BITS always 0 */
32 typedef guint32 camel_key_t;    /* this is a bitfield of (block offset:BLOCK_SIZE_BITS) */
33
34 typedef struct _CamelBlockRoot CamelBlockRoot;
35 typedef struct _CamelBlock CamelBlock;
36 typedef struct _CamelBlockFile CamelBlockFile;
37 typedef struct _CamelBlockFileClass CamelBlockFileClass;
38
39 #define CAMEL_BLOCK_FILE_SYNC (1<<0)
40
41 #define CAMEL_BLOCK_SIZE (1024)
42 #define CAMEL_BLOCK_SIZE_BITS (10) /* # bits to contain block_size bytes */
43
44 #define CAMEL_BLOCK_DIRTY (1<<0)
45 #define CAMEL_BLOCK_DETACHED (1<<1)
46
47 struct _CamelBlockRoot {
48         char version[8];        /* version number */
49
50         guint32 flags;          /* flags for file */
51         guint32 block_size;     /* block size of this file */
52         camel_block_t free;     /* free block list */
53         camel_block_t last;     /* pointer to end of blocks */
54
55         /* subclasses tack on, but no more than CAMEL_BLOCK_SIZE! */
56 };
57
58 /* LRU cache of blocks */
59 struct _CamelBlock {
60         struct _CamelBlock *next;
61         struct _CamelBlock *prev;
62
63         camel_block_t id;
64         guint32 flags;
65         guint32 refcount;
66         guint32 align00;
67
68         unsigned char data[CAMEL_BLOCK_SIZE];
69 };
70
71 struct _CamelBlockFile {
72         CamelObject parent;
73
74         struct _CamelBlockFilePrivate *priv;
75
76         char version[8];
77         char *path;
78         int flags;
79
80         int fd;
81         size_t block_size;
82
83         CamelBlockRoot *root;
84         CamelBlock *root_block;
85
86         /* make private? */
87         int block_cache_limit;
88         int block_cache_count;
89         EDList block_cache;
90         GHashTable *blocks;
91 };
92
93 struct _CamelBlockFileClass {
94         CamelObjectClass parent;
95
96         int (*validate_root)(CamelBlockFile *);
97         int (*init_root)(CamelBlockFile *);
98 };
99
100 CamelType camel_block_file_get_type(void);
101
102 CamelBlockFile *camel_block_file_new(const char *path, int flags, const char version[8], size_t block_size);
103 int camel_block_file_rename(CamelBlockFile *bs, const char *path);
104 int camel_block_file_delete(CamelBlockFile *kf);
105
106 CamelBlock *camel_block_file_new_block(CamelBlockFile *bs);
107 int camel_block_file_free_block(CamelBlockFile *bs, camel_block_t id);
108 CamelBlock *camel_block_file_get_block(CamelBlockFile *bs, camel_block_t id);
109 void camel_block_file_detach_block(CamelBlockFile *bs, CamelBlock *bl);
110 void camel_block_file_attach_block(CamelBlockFile *bs, CamelBlock *bl);
111 void camel_block_file_touch_block(CamelBlockFile *bs, CamelBlock *bl);
112 void camel_block_file_unref_block(CamelBlockFile *bs, CamelBlock *bl);
113 int camel_block_file_sync_block(CamelBlockFile *bs, CamelBlock *bl);
114 int camel_block_file_sync(CamelBlockFile *bs);
115
116 /* ********************************************************************** */
117
118 typedef struct _CamelKeyFile CamelKeyFile;
119 typedef struct _CamelKeyFileClass CamelKeyFileClass;
120
121 struct _CamelKeyFile {
122         CamelObject parent;
123
124         struct _CamelKeyFilePrivate *priv;
125
126         FILE *fp;
127         char *path;
128         int flags;
129         off_t last;
130 };
131
132 struct _CamelKeyFileClass {
133         CamelObjectClass parent;
134 };
135
136 CamelType      camel_key_file_get_type(void);
137
138 CamelKeyFile * camel_key_file_new(const char *path, int flags, const char version[8]);
139 int            camel_key_file_rename(CamelKeyFile *kf, const char *path);
140 int            camel_key_file_delete(CamelKeyFile *kf);
141
142 int            camel_key_file_write(CamelKeyFile *kf, camel_block_t *parent, size_t len, camel_key_t *records);
143 int            camel_key_file_read(CamelKeyFile *kf, camel_block_t *start, size_t *len, camel_key_t **records);
144
145 G_END_DECLS
146
147 #endif /* ! _CAMEL_BLOCK_FILE_H */