forked from cloudyourcar/ringfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringfs.h
182 lines (159 loc) · 4.91 KB
/
ringfs.h
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
/*
* Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
* This program is free software. It comes without any warranty, to the extent
* permitted by applicable law. You can redistribute it and/or modify it under
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
* published by Sam Hocevar. See the COPYING file for more details.
*/
#ifndef RINGFS_H
#define RINGFS_H
/**
* @defgroup ringfs_api RingFS API
* @{
*/
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
/**
* Flash memory+parition descriptor.
*/
struct ringfs_flash_partition
{
int sector_size; /**< Sector size, in bytes. */
int sector_offset; /**< Partition offset, in sectors. */
int sector_count; /**< Partition size, in sectors. */
/**
* Erase a sector.
* @param address Any address inside the sector.
* @returns Zero on success, -1 on failure.
*/
int (*sector_erase)(struct ringfs_flash_partition *flash, int address);
/**
* Program flash memory bits by toggling them from 1 to 0.
* @param address Start address, in bytes.
* @param data Data to program.
* @param size Size of data.
* @returns size on success, -1 on failure.
*/
ssize_t (*program)(struct ringfs_flash_partition *flash, int address, const void *data, size_t size);
/**
* Read flash memory.
* @param address Start address, in bytes.
* @param data Buffer to store read data.
* @param size Size of data.
* @returns size on success, -1 on failure.
*/
ssize_t (*read)(struct ringfs_flash_partition *flash, int address, void *data, size_t size);
};
/** @private */
struct ringfs_loc {
int sector;
int slot;
};
/**
* RingFS instance. Should be initialized with ringfs_init() befure use.
* Structure fields should not be accessed directly.
* */
struct ringfs {
/* Constant values, set once at ringfs_init(). */
struct ringfs_flash_partition *flash;
uint32_t version;
int object_size;
/* Cached values. */
int slots_per_sector;
/* Read/write pointers. Modified as needed. */
struct ringfs_loc read;
struct ringfs_loc write;
struct ringfs_loc cursor;
};
/**
* Initialize a RingFS instance. Must be called before the instance can be used
* with the other ringfs_* functions.
*
* @param fs RingFS instance to be initialized.
* @param flash Flash memory interface. Must be implemented externally.
* @param version Object version. Should be incremented whenever the object's
* semantics or size change in a backwards-incompatible way.
* @param object_size Size of one stored object, in bytes.
* @returns Zero on success, -1 on failure.
*/
int ringfs_init(struct ringfs *fs, struct ringfs_flash_partition *flash, uint32_t version, int object_size);
/**
* Format the flash memory.
*
* @param fs Initialized RingFS instance.
* @returns Zero on success, -1 on failure.
*/
int ringfs_format(struct ringfs *fs);
/**
* Scan the flash memory for a valid filesystem.
*
* @param fs Initialized RingFS instance.
* @returns Zero on success, -1 on failure.
*/
int ringfs_scan(struct ringfs *fs);
/**
* Calculate maximum RingFS capacity.
*
* @param fs Initialized RingFS instance.
* @returns Maximum capacity on success, -1 on failure.
*/
int ringfs_capacity(struct ringfs *fs);
/**
* Calculate approximate object count.
* Runs in O(1).
*
* @param fs Initialized RingFS instance.
* @returns Estimated object count on success, -1 on failure.
*/
int ringfs_count_estimate(struct ringfs *fs);
/**
* Calculate exact object count.
* Runs in O(n).
*
* @param fs Initialized RingFS instance.
* @returns Exact object count on success, -1 on failure.
*/
int ringfs_count_exact(struct ringfs *fs);
/**
* Append an object at the end of the ring. Deletes oldest objects as needed.
*
* @param fs Initialized RingFS instance.
* @param object Object to be stored.
* @returns Zero on success, -1 on failure.
*/
int ringfs_append(struct ringfs *fs, const void *object);
/**
* Fetch next object from the ring, oldest-first. Advances read cursor.
*
* @param fs Initialized RingFS instance.
* @param object Buffer to store retrieved object.
* @returns Zero on success, -1 on failure.
*/
int ringfs_fetch(struct ringfs *fs, void *object);
/**
* Discard all fetched objects up to the read cursor.
*
* @param fs Initialized RingFS instance.
* @returns Zero on success, -1 on failure.
*/
int ringfs_discard(struct ringfs *fs);
int ringfs_item_discard(struct ringfs *fs);
/**
* Rewind the read cursor back to the oldest object.
*
* @param fs Initialized RingFS instance.
* @returns Zero on success, -1 on failure.
*/
int ringfs_rewind(struct ringfs *fs);
/**
* Dump filesystem metadata. For debugging purposes.
* @param stream File stream to write to.
* @param fs Initialized RingFS instance.
*/
void ringfs_dump(FILE *stream, struct ringfs *fs);
/**
* @}
*/
#endif
/* vim: set ts=4 sw=4 et: */