summaryrefslogtreecommitdiff
path: root/camel/camel-block-file.h
blob: 7e8531c0deb3a3c686edfbe331de6e2fd50eb751 (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
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 */

#if !defined (__CAMEL_H_INSIDE__) && !defined (CAMEL_COMPILATION)
#error "Only <camel/camel.h> can be included directly."
#endif

#ifndef CAMEL_BLOCK_FILE_H
#define CAMEL_BLOCK_FILE_H

#include <stdio.h>
#include <sys/types.h>

#include <glib-object.h>

/* Standard GObject macros */
#define CAMEL_TYPE_BLOCK_FILE \
	(camel_block_file_get_type ())
#define CAMEL_BLOCK_FILE(obj) \
	(G_TYPE_CHECK_INSTANCE_CAST \
	((obj), CAMEL_TYPE_BLOCK_FILE, CamelBlockFile))
#define CAMEL_BLOCK_FILE_CLASS(cls) \
	(G_TYPE_CHECK_CLASS_CAST \
	((cls), CAMEL_TYPE_BLOCK_FILE, CamelBlockFileClass))
#define CAMEL_IS_BLOCK_FILE(obj) \
	(G_TYPE_CHECK_INSTANCE_TYPE \
	((obj), CAMEL_TYPE_BLOCK_FILE))
#define CAMEL_IS_BLOCK_FILE_CLASS(cls) \
	(G_TYPE_CHECK_CLASS_TYPE \
	((cls), CAMEL_TYPE_BLOCK_FILE))
#define CAMEL_BLOCK_FILE_GET_CLASS(obj) \
	(G_TYPE_INSTANCE_GET_CLASS \
	((obj), CAMEL_TYPE_BLOCK_FILE, CamelBlockFileClass))

#define CAMEL_TYPE_KEY_FILE \
	(camel_key_file_get_type ())
#define CAMEL_KEY_FILE(obj) \
	(G_TYPE_CHECK_INSTANCE_CAST \
	((obj), CAMEL_TYPE_KEY_FILE, CamelKeyFile))
#define CAMEL_KEY_FILE_CLASS(cls) \
	(G_TYPE_CHECK_CLASS_CAST \
	((cls), CAMEL_TYPE_KEY_FILE, CamelKeyFileClass))
#define CAMEL_IS_KEY_FILE(obj) \
	(G_TYPE_CHECK_INSTANCE_TYPE \
	((obj), CAMEL_TYPE_KEY_FILE))
#define CAMEL_IS_KEY_FILE_CLASS(cls) \
	(G_TYPE_CHECK_CLASS_TYPE \
	((cls), CAMEL_TYPE_KEY_FILE))
#define CAMEL_KEY_FILE_GET_CLASS(obj) \
	(G_TYPE_INSTANCE_GET_CLASS \
	((obj), CAMEL_TYPE_KEY_FILE, CamelKeyFileClass))

G_BEGIN_DECLS

typedef guint32 camel_block_t;	/* block offset, absolute, bottom BLOCK_SIZE_BITS always 0 */
typedef guint32 camel_key_t;	/* this is a bitfield of (block offset:BLOCK_SIZE_BITS) */

typedef struct _CamelBlockRoot CamelBlockRoot;
typedef struct _CamelBlock CamelBlock;
typedef struct _CamelBlockFile CamelBlockFile;
typedef struct _CamelBlockFileClass CamelBlockFileClass;
typedef struct _CamelBlockFilePrivate CamelBlockFilePrivate;

typedef enum {
	CAMEL_BLOCK_FILE_SYNC = 1 << 0
} CamelBlockFileFlags;

#define CAMEL_BLOCK_SIZE (1024)
#define CAMEL_BLOCK_SIZE_BITS (10) /* # bits to contain block_size bytes */

typedef enum {
	CAMEL_BLOCK_DIRTY = 1 << 0,
	CAMEL_BLOCK_DETACHED = 1 << 1
} CamelBlockFlags;

struct _CamelBlockRoot {
	gchar version[8];	/* version number */

	guint32 flags;		/* flags for file */
	guint32 block_size;	/* block size of this file */
	camel_block_t free;	/* free block list */
	camel_block_t last;	/* pointer to end of blocks */

	/* subclasses tack on, but no more than CAMEL_BLOCK_SIZE! */
};

/* LRU cache of blocks */
struct _CamelBlock {
	camel_block_t id;
	CamelBlockFlags flags;
	guint32 refcount;
	guint32 align00;

	guchar data[CAMEL_BLOCK_SIZE];
};

struct _CamelBlockFile {
	GObject parent;
	CamelBlockFilePrivate *priv;

	gchar version[8];
	gchar *path;
	CamelBlockFileFlags flags;

	gint fd;
	gsize block_size;

	CamelBlockRoot *root;
	CamelBlock *root_block;

	/* make private? */
	gint block_cache_limit;
	gint block_cache_count;
	GQueue block_cache;
	GHashTable *blocks;
};

struct _CamelBlockFileClass {
	GObjectClass parent_class;

	gint (*validate_root)(CamelBlockFile *);
	gint (*init_root)(CamelBlockFile *);
};

GType		camel_block_file_get_type	(void);
CamelBlockFile *camel_block_file_new		(const gchar *path,
						 gint flags,
						 const gchar version[8],
						 gsize block_size);
gint		camel_block_file_rename		(CamelBlockFile *bs,
						 const gchar *path);
gint		camel_block_file_delete		(CamelBlockFile *kf);
CamelBlock *	camel_block_file_new_block	(CamelBlockFile *bs);
gint		camel_block_file_free_block	(CamelBlockFile *bs,
						 camel_block_t id);
CamelBlock *	camel_block_file_get_block	(CamelBlockFile *bs,
						 camel_block_t id);
void		camel_block_file_detach_block	(CamelBlockFile *bs,
						 CamelBlock *bl);
void		camel_block_file_attach_block	(CamelBlockFile *bs,
						 CamelBlock *bl);
void		camel_block_file_touch_block	(CamelBlockFile *bs,
						 CamelBlock *bl);
void		camel_block_file_unref_block	(CamelBlockFile *bs,
						 CamelBlock *bl);
gint		camel_block_file_sync_block	(CamelBlockFile *bs,
						 CamelBlock *bl);
gint		camel_block_file_sync		(CamelBlockFile *bs);

/* ********************************************************************** */

typedef struct _CamelKeyFile CamelKeyFile;
typedef struct _CamelKeyFileClass CamelKeyFileClass;
typedef struct _CamelKeyFilePrivate CamelKeyFilePrivate;

struct _CamelKeyFile {
	GObject parent;
	CamelKeyFilePrivate *priv;

	FILE *fp;
	gchar *path;
	gint flags;
	goffset last;
};

struct _CamelKeyFileClass {
	GObjectClass parent_class;
};

GType      camel_key_file_get_type (void);

CamelKeyFile * camel_key_file_new (const gchar *path, gint flags, const gchar version[8]);
gint	       camel_key_file_rename (CamelKeyFile *kf, const gchar *path);
gint	       camel_key_file_delete (CamelKeyFile *kf);

gint            camel_key_file_write (CamelKeyFile *kf, camel_block_t *parent, gsize len, camel_key_t *records);
gint            camel_key_file_read (CamelKeyFile *kf, camel_block_t *start, gsize *len, camel_key_t **records);

G_END_DECLS

#endif /* CAMEL_BLOCK_FILE_H */