3 * dumpscan - routines for scanning and manipulating AFS volume dumps
5 * Copyright (c) 1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
29 /* xf_files.c - XFILE routines for accessing UNIX files */
31 #include <sys/types.h>
43 #define O_MODE_MASK (O_RDONLY | O_WRONLY | O_RDWR)
46 /* do_read for stdio xfiles */
48 xf_FILE_do_read(XFILE * X, void *buf, afs_uint32 count)
52 /* XXX: handle short and interrupted reads */
53 if (fread(buf, count, 1, F) != 1)
54 return ferror(F) ? errno : ERROR_XFILE_EOF;
59 /* do_write for stdio xfiles */
61 xf_FILE_do_write(XFILE * X, void *buf, afs_uint32 count)
65 /* XXX: handle interrupted writes */
66 if (fwrite(buf, count, 1, F) != 1)
72 /* do_tell for stdio xfiles */
74 xf_FILE_do_tell(XFILE * X, dt_uint64 * offset)
82 set64(*offset, where);
87 /* do_seek for stdio xfiles */
89 xf_FILE_do_seek(XFILE * X, dt_uint64 * offset)
92 off_t where = get64(*offset);
94 if (fseek(F, where, SEEK_SET) == -1)
100 /* do_skip for stdio xfiles */
102 xf_FILE_do_skip(XFILE * X, afs_uint32 count)
106 if (fseek(F, count, SEEK_CUR) == -1)
112 /* do_close for stdio xfiles */
114 xf_FILE_do_close(XFILE * X)
125 /* Prepare a stdio XFILE */
127 prepare(XFILE * X, FILE * F, int xflag)
131 memset(X, 0, sizeof(*X));
132 X->do_read = xf_FILE_do_read;
133 X->do_write = xf_FILE_do_write;
134 X->do_tell = xf_FILE_do_tell;
135 X->do_close = xf_FILE_do_close;
140 if (!fstat(fileno(F), &st)
141 && ((st.st_mode & S_IFMT) == S_IFREG
142 || (st.st_mode & S_IFMT) == S_IFBLK)) {
144 X->do_seek = xf_FILE_do_seek;
145 X->do_skip = xf_FILE_do_skip;
150 /* Open an XFILE by path */
152 xfopen_path(XFILE * X, int flag, char *path, int mode)
158 xflag = flag & O_MODE_MASK;
159 if (xflag == O_WRONLY)
160 return ERROR_XFILE_WRONLY;
162 if ((fd = open(path, flag, mode)) < 0)
164 if (!(F = fdopen(fd, (xflag == O_RDONLY) ? "r" : "r+"))) {
170 prepare(X, F, xflag);
175 /* Open an XFILE by FILE * */
177 xfopen_FILE(XFILE * X, int flag, FILE * F)
180 if (flag == O_WRONLY)
181 return ERROR_XFILE_WRONLY;
187 /* Open an XFILE by file descriptor */
189 xfopen_fd(XFILE * X, int flag, int fd)
194 if (flag == O_WRONLY)
195 return ERROR_XFILE_WRONLY;
196 if (!(F = fdopen(fd, (flag == O_RDONLY) ? "r" : "r+")))
203 /* open-by-name support for filenames */
205 xfon_path(XFILE * X, int flag, char *name)
207 return xfopen_path(X, flag, name, 0644);
211 /* open-by-name support for file descriptors */
213 xfon_fd(XFILE * X, int flag, char *name)
216 return xfopen_fd(X, flag, fd);
220 /* open-by-name support for standard I/O */
222 xfon_stdio(XFILE * X, int flag)
225 if (flag == O_WRONLY)
227 return xfopen_FILE(X, flag, (flag == O_RDONLY) ? stdin : stdout);