-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxfiles.c
201 lines (166 loc) · 4.7 KB
/
xfiles.c
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
197
198
199
200
201
/*
* CMUCS AFStools
* dumpscan - routines for scanning and manipulating AFS volume dumps
*
* Copyright (c) 1998, 2001 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or [email protected]
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/* xfiles.c - General support routines for xfiles */
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include "xfiles.h"
#include "xf_errs.h"
#define SKIP_SIZE 65536
afs_uint32 xfread(XFILE *X, void *buf, afs_uint32 count)
{
afs_uint32 code;
u_int64 tmp64;
code = (X->do_read)(X, buf, count);
if (code) return code;
add64_32(tmp64, X->filepos, count);
cp64(X->filepos, tmp64);
if (X->passthru) return xfwrite(X->passthru, buf, count);
return 0;
}
afs_uint32 xfwrite(XFILE *X, void *buf, afs_uint32 count)
{
afs_uint32 code;
u_int64 tmp64;
if (!X->is_writable) return ERROR_XFILE_RDONLY;
code = (X->do_write)(X, buf, count);
if (code) return code;
add64_32(tmp64, X->filepos, count);
cp64(X->filepos, tmp64);
return 0;
}
afs_uint32 xftell(XFILE *X, u_int64 *offset)
{
if (X->do_tell) return (X->do_tell)(X, offset);
cp64(*offset, X->filepos);
return 0;
}
afs_uint32 xfseek(XFILE *X, u_int64 *offset)
{
afs_uint32 code;
if (!X->do_seek) return ERROR_XFILE_NOSEEK;
code = (X->do_seek)(X, offset);
if (code) return code;
cp64(X->filepos, *offset);
return 0;
}
afs_uint32 xfskip(XFILE *X, afs_uint32 count)
{
afs_uint32 code;
u_int64 tmp64;
/* Use the skip method, if there is one */
if (X->do_skip && !X->passthru) {
mk64(tmp64, 0, count);
code = (X->do_skip)(X, &tmp64);
if (code) return code;
add64_32(tmp64, X->filepos, count);
cp64(X->filepos, tmp64);
return 0;
}
/* Simulate using absolute seek, if available */
if (X->do_seek && !X->passthru) {
if (code = xftell(X, &tmp64)) return code;
add64_32(X->filepos, tmp64, count);
cp64(tmp64, X->filepos);
return xfseek(X, &tmp64);
}
/* Do it the hard/slow way - read all the data to be skipped.
* This is done if no other method is available, or if we are
* supposed to be copying all the data to another XFILE
*/
{
char buf[SKIP_SIZE];
afs_uint32 n;
while (count) {
n = (count > SKIP_SIZE) ? SKIP_SIZE : count;
if (code = xfread(X, buf, n)) return code;
count -= n;
}
return 0;
}
}
afs_uint32 xfskip64(XFILE *X, u_int64 *count)
{
afs_uint32 code;
u_int64 tmp64;
/* Use the skip method, if there is one */
if (X->do_skip && !X->passthru) {
code = (X->do_skip)(X, count);
if (code) return code;
add64_64(tmp64, X->filepos, *count);
cp64(X->filepos, tmp64);
return 0;
}
/* Simulate using absolute seek, if available */
if (X->do_seek && !X->passthru) {
if (code = xftell(X, &tmp64)) return code;
add64_64(X->filepos, tmp64, *count);
cp64(tmp64, X->filepos);
return xfseek(X, &tmp64);
}
/* Do it the hard/slow way - read all the data to be skipped.
* This is done if no other method is available, or if we are
* supposed to be copying all the data to another XFILE
*/
{
char buf[SKIP_SIZE];
afs_uint32 n;
u_int64 remaining, zero;
mk64(zero, 0, 0);
cp64(remaining, *count);
while (gt64(remaining, zero)) {
mk64(tmp64, 0, SKIP_SIZE);
if (gt64(remaining, tmp64)) n = SKIP_SIZE;
else n = get64(remaining);
if (code = xfread(X, buf, n)) return code;
sub64_32(tmp64, remaining, n);
cp64(remaining, tmp64);
}
return 0;
}
}
afs_uint32 xfpass(XFILE *X, XFILE *Y)
{
if (X->passthru) return ERROR_XFILE_ISPASS;
if (!Y->is_writable) return ERROR_XFILE_RDONLY;
X->passthru = Y;
return 0;
}
afs_uint32 xfunpass(XFILE *X)
{
if (!X->passthru) return ERROR_XFILE_NOPASS;
X->passthru = 0;
return 0;
}
afs_uint32 xfclose(XFILE *X)
{
int code = 0;
if (X->do_close) code = (X->do_close)(X);
memset(X, 0, sizeof(*X));
return code;
}