Download
jcs
/subtext
/bile.h
(View History)
jcs bile: Cope with malloc/realloc failures gracefully when possible | Latest amendment: 333 on 2023-03-02 |
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_NO_MEMORY -4002 |
80 | |
81 | #define BILE_DELETE_FLAG_ZERO (1 << 0) |
82 | #define BILE_DELETE_FLAG_PURGE (1 << 1) |
83 | |
84 | struct bile_highest_id { |
85 | OSType type; |
86 | unsigned long highest_id; |
87 | }; |
88 | |
89 | struct bile { |
90 | struct bile_object map_ptr; |
91 | struct bile_object old_map_ptr; |
92 | short vrefnum, frefnum, last_error; |
93 | Str255 filename; |
94 | size_t file_size; |
95 | struct bile_object *map; /* array of bile_objects */ |
96 | size_t nobjects; |
97 | bool dirty_map; |
98 | char magic[5]; |
99 | }; |
100 | |
101 | struct bile_object_field { |
102 | size_t struct_off; |
103 | ssize_t size; |
104 | size_t object_len_off; |
105 | }; |
106 | |
107 | short bile_error(struct bile *bile); |
108 | |
109 | struct bile * bile_create(const Str255 filename, short vrefnum, |
110 | const OSType creator, const OSType type); |
111 | struct bile * bile_open(const Str255 filename, short vrefnum); |
112 | struct bile * bile_open_recover_map(const Str255 filename, |
113 | short vrefnum); |
114 | short bile_write_map(struct bile *bile); |
115 | short bile_flush(struct bile *bile, bool and_vol); |
116 | void bile_close(struct bile *bile); |
117 | |
118 | struct bile_object * bile_find(struct bile *bile, const OSType type, |
119 | const unsigned long id); |
120 | size_t bile_count_by_type(struct bile *bile, |
121 | const OSType type); |
122 | size_t bile_ids_by_type(struct bile *bile, |
123 | const OSType type, unsigned long **ret); |
124 | size_t bile_sorted_ids_by_type(struct bile *bile, |
125 | const OSType type, unsigned long **ret); |
126 | struct bile_object * bile_get_nth_of_type(struct bile *bile, |
127 | const unsigned long index, const OSType type); |
128 | unsigned long bile_next_id(struct bile *bile, const OSType type); |
129 | short bile_delete(struct bile *bile, const OSType type, |
130 | const unsigned long id, const unsigned short flags); |
131 | size_t bile_read_object(struct bile *bile, |
132 | const struct bile_object *o, void *data, |
133 | const size_t len); |
134 | size_t bile_read(struct bile *bile, const OSType type, |
135 | const unsigned long id, void *data, |
136 | const size_t len); |
137 | size_t bile_read_alloc(struct bile *bile, |
138 | const OSType type, const unsigned long id, |
139 | void *data_ptr); |
140 | size_t bile_resize(struct bile *bile, const OSType type, |
141 | const unsigned long id, size_t new_size); |
142 | size_t bile_write(struct bile *bile, OSType type, |
143 | const unsigned long id, const void *data, |
144 | const size_t len); |
145 | short bile_verify(struct bile *bile); |
146 | |
147 | short bile_marshall_object(struct bile *bile, |
148 | const struct bile_object_field *fields, |
149 | const size_t nfields, void *object, |
150 | void *ret_ptr, size_t *ret_size); |
151 | short bile_unmarshall_object(struct bile *bile, |
152 | const struct bile_object_field *fields, |
153 | const size_t nfields, const void *data, |
154 | const size_t data_size, void *object, |
155 | const size_t object_size, bool deep); |
156 | |
157 | #endif |