AmendHub

Download

jcs

/

amend

/

bile.h

 

(View History)

jcs   bile: Set a useful error code when opening fails due to a bad map Latest amendment: 194 on 2026-10-04

1 /*
2 * Copyright (c) 2022 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef __BILE_H__
18 #define __BILE_H__
19
20 #include "util.h"
21
22 /*
23 * File format:
24 * [ bile header - BILE_HEADER_LEN ]
25 * [ BILE_MAGIC - BILE_MAGIC_LEN ]
26 * [ map pointer object ]
27 * [ pointer position - long ]
28 * [ pointer size - long ]
29 * [ pointer type (_BL>) - long ]
30 * [ pointer id - long ]
31 * [ previous map pointer object ]
32 * [ pointer position - long ]
33 * [ pointer size - long ]
34 * [ pointer type (_BL>) - long ]
35 * [ pointer id - long ]
36 * [ padding for future use ]
37 * [ object[0] start (map points to this as its position) ]
38 * [ object[0] position - long ]
39 * [ object[0] size - long ]
40 * [ object[0] type - long ]
41 * [ object[0] id - long ]
42 * [ object[1] start ]
43 * [ .. ]
44 * [ map object - (pointed to by map pointer position) ]
45 * [ map position - long ]
46 * [ map size - long ]
47 * [ map type (_BLM) - long ]
48 * [ map id - long ]
49 * [ map contents ]
50 */
51 #define BILE_MAGIC "BILE2"
52 #define BILE_MAGIC_LEN 5
53 #define BILE_TYPE_MAP '_BLM'
54 #define BILE_TYPE_MAPPTR '_BL>'
55 #define BILE_TYPE_PURGE '_BLP'
56 #define BILE_TYPE_HIGHESTID '_BLH'
57
58 struct bile_object {
59 unsigned long pos;
60 unsigned long size;
61 OSType type;
62 unsigned long id;
63 };
64 #define BILE_OBJECT_SIZE (sizeof(struct bile_object))
65 #define BILE_HEADER_LEN 256
66
67 /* allocate filesystem space in chunks of this */
68 #define BILE_ALLOCATE_SIZE 8192
69
70 #ifndef BILE1_MAGIC
71 #define BILE1_MAGIC "BILE1"
72 #endif
73 #ifndef BILE1_MAGIC_LEN
74 #define BILE1_MAGIC_LEN 5
75 #endif
76
77 #define BILE_ERR_NEED_UPGRADE_1 -4000
78 #define BILE_ERR_BOGUS_OBJECT -4001
79 #define BILE_ERR_BAD_MAP -4002
80
81 struct bile_highest_id {
82 OSType type;
83 unsigned long highest_id;
84 };
85
86 struct bile {
87 struct bile_object map_ptr;
88 struct bile_object old_map_ptr;
89 short vrefnum, frefnum, last_error;
90 Str255 filename;
91 size_t file_size;
92 struct bile_object *map; /* array of bile_objects */
93 size_t nobjects;
94 char magic[5];
95 bool in_transaction;
96 };
97
98 struct bile_object_field {
99 size_t struct_off;
100 ssize_t size;
101 size_t object_len_off;
102 };
103
104 short bile_error(struct bile *bile);
105
106 struct bile * bile_create(const Str255 filename, short vrefnum,
107 const OSType creator, const OSType type);
108 struct bile * bile_open(const Str255 filename, short vrefnum);
109 struct bile * bile_open_recover_map(const Str255 filename,
110 short vrefnum);
111 short bile_flush(struct bile *bile, bool and_vol);
112 void bile_close(struct bile *bile);
113 void bile_begin(struct bile *bile);
114 short bile_commit(struct bile *bile);
115
116 struct bile_object * bile_find(struct bile *bile, const OSType type,
117 const unsigned long id);
118 size_t bile_count_by_type(struct bile *bile,
119 const OSType type);
120 size_t bile_sorted_ids_by_type(struct bile *bile,
121 const OSType type, unsigned long **ret);
122 struct bile_object * bile_get_nth_of_type(struct bile *bile,
123 const unsigned long index, const OSType type);
124 unsigned long bile_next_id(struct bile *bile, const OSType type);
125 short bile_delete(struct bile *bile, const OSType type,
126 const unsigned long id);
127 size_t bile_read_object(struct bile *bile,
128 const struct bile_object *o, void *data,
129 const size_t len);
130 size_t bile_read(struct bile *bile, const OSType type,
131 const unsigned long id, void *data,
132 const size_t len);
133 size_t bile_read_alloc(struct bile *bile,
134 const OSType type, const unsigned long id,
135 void *data_ptr);
136 size_t bile_write(struct bile *bile, OSType type,
137 const unsigned long id, const void *data,
138 const size_t len);
139 short bile_verify(struct bile *bile);
140
141 short bile_marshall_object(struct bile *bile,
142 const struct bile_object_field *fields,
143 const size_t nfields, void *object,
144 void *ret_ptr, size_t *ret_size, char *note);
145 short bile_unmarshall_object(struct bile *bile,
146 const struct bile_object_field *fields,
147 const size_t nfields, const void *data,
148 const size_t data_size, void *object,
149 const size_t object_size, bool deep, char *note);
150
151 #endif