2 * Copyright (c) 2001 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * The interface for the on disk storage of data when arla is down
38 #include "arla_local.h"
42 * Save the fcache inforation in `fn', call `func' until `func' return
43 * a none zero value. A negative value is an errno, and a positive is
44 * a signal to terminate the loop (success). `ptr' is passed to
49 state_store_fcache(const char *fn
, store_fcache_fn func
, void *ptr
)
51 int ret
, fd
, save_errno
;
52 struct fcache_store st
;
54 char file
[MAXPATHLEN
];
56 snprintf(file
, sizeof(file
), "%s.new", fn
);
58 fd
= open (file
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
61 u1
= FCACHE_MAGIC_COOKIE
;
64 if (write (fd
, &u1
, sizeof(u1
)) != sizeof(u1
)
65 || write (fd
, &u2
, sizeof(u2
)) != sizeof(u2
)) {
74 memset(&st
, 0, sizeof(st
));
76 ret
= (*func
)(&st
, ptr
);
77 if (ret
< 0) { /* errors */
80 } else if (ret
== STORE_SKIP
) {
82 } else if (ret
== STORE_NEXT
) {
83 if (write(fd
, &st
, sizeof(st
)) != sizeof(st
)) {
88 } else if (ret
== STORE_DONE
) {
96 if (rename (file
, fn
))
103 state_recover_fcache(const char *file
, store_fcache_fn func
, void *ptr
)
106 struct fcache_store st
;
109 fd
= open (file
, O_RDONLY
| O_BINARY
, 0);
112 if (read (fd
, &u1
, sizeof(u1
)) != sizeof(u1
)
113 || read (fd
, &u2
, sizeof(u2
)) != sizeof(u2
)) {
117 if (u1
!= FCACHE_MAGIC_COOKIE
) {
119 arla_warnx (ADEBFCACHE
, "dump file not recognized, ignoring");
124 if (u2
!= FCACHE_VERSION
) {
126 arla_warnx (ADEBFCACHE
, "unknown dump file version number %u", u2
);
132 while (read (fd
, &st
, sizeof(st
)) == sizeof(st
)) {
134 st
.cell
[sizeof(st
.cell
)-1] = '\0';
135 st
.parentcell
[sizeof(st
.parentcell
)-1] = '\0';
137 if ((*func
)(&st
, ptr
)) {
148 * Save the volcache inforation in `fn', call `func' until `func' return
149 * a none zero value. A negative value is an errno, and a positive is
150 * a signal to terminate the loop (success). `ptr' is passed to
155 state_store_volcache(const char *fn
, store_volcache_fn func
, void *ptr
)
157 int ret
, fd
, save_errno
;
158 struct volcache_store st
;
160 char file
[MAXPATHLEN
];
162 snprintf(file
, sizeof(file
), "%s.new", fn
);
164 fd
= open (file
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
167 u1
= VOLCACHE_MAGIC_COOKIE
;
168 u2
= VOLCACHE_VERSION
;
170 if (write (fd
, &u1
, sizeof(u1
)) != sizeof(u1
)
171 || write (fd
, &u2
, sizeof(u2
)) != sizeof(u2
)) {
180 memset(&st
, 0, sizeof(st
));
182 ret
= (*func
)(&st
, ptr
);
183 if (ret
< 0) { /* errors */
186 } else if (ret
== STORE_SKIP
) {
188 } else if (ret
== STORE_NEXT
) {
189 if (write(fd
, &st
, sizeof(st
)) != sizeof(st
)) {
194 } else if (ret
== STORE_DONE
) {
202 if (rename (file
, fn
))
209 state_recover_volcache(const char *file
, store_volcache_fn func
, void *ptr
)
212 struct volcache_store st
;
215 fd
= open (file
, O_RDONLY
| O_BINARY
, 0);
218 if (read (fd
, &u1
, sizeof(u1
)) != sizeof(u1
)
219 || read (fd
, &u2
, sizeof(u2
)) != sizeof(u2
)) {
223 if (u1
!= VOLCACHE_MAGIC_COOKIE
) {
225 arla_warnx (ADEBVOLCACHE
, "dump file not recognized, ignoring");
230 if (u2
!= VOLCACHE_VERSION
) {
232 arla_warnx (ADEBVOLCACHE
, "unknown dump file version number %u", u2
);
238 while (read (fd
, &st
, sizeof(st
)) == sizeof(st
)) {
240 st
.cell
[sizeof(st
.cell
)-1] = '\0';
242 if ((*func
)(&st
, ptr
)) {