2 * Copyright (c) 2002 Maxim Sobolev
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #define CONF_BUF (512)
34 #define SEEK_BUF (512)
38 char **filesv
; /* Filenames */
39 char **descsv
; /* Descriptions */
40 int filesc
; /* Number of parts */
41 int curfile
; /* Current file number */
42 int curfd
; /* Current file descriptor */
43 off_t tot_pos
; /* Offset from the beginning of the sequence */
44 off_t file_pos
; /* Offset from the beginning of the slice */
47 static int split_openfile(struct split_file
*sf
);
48 static int splitfs_open(const char *path
, struct open_file
*f
);
49 static int splitfs_close(struct open_file
*f
);
50 static int splitfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
);
51 static off_t
splitfs_seek(struct open_file
*f
, off_t offset
, int where
);
52 static int splitfs_stat(struct open_file
*f
, struct stat
*sb
);
54 struct fs_ops splitfs_fsops
= {
66 split_file_destroy(struct split_file
*sf
)
71 for (i
= 0; i
< sf
->filesc
; i
++) {
82 split_openfile(struct split_file
*sf
)
87 sf
->curfd
= open(sf
->filesv
[sf
->curfile
], O_RDONLY
);
90 if ((sf
->curfd
== -1) && (errno
!= ENOENT
))
94 printf("\nInsert disk labelled %s and press any key...",
95 sf
->descsv
[sf
->curfile
]);
104 splitfs_open(const char *fname
, struct open_file
*f
)
106 char *buf
, *confname
, *cp
;
108 struct split_file
*sf
;
111 /* Have to be in "just read it" mode */
112 if (f
->f_flags
!= F_READ
)
115 /* If the name already ends in `.split', ignore it */
116 if ((cp
= strrchr(fname
, '.')) && (!strcmp(cp
, ".split")))
119 /* Construct new name */
120 confname
= malloc(strlen(fname
) + 7);
121 sprintf(confname
, "%s.split", fname
);
123 /* Try to open the configuration file */
124 conffd
= open(confname
, O_RDONLY
);
129 if (fstat(conffd
, &sb
) < 0) {
130 printf("splitfs_open: stat failed\n");
134 if (!S_ISREG(sb
.st_mode
)) {
135 printf("splitfs_open: not a file\n");
137 return(EISDIR
); /* best guess */
140 /* Allocate a split_file structure, populate it from the config file */
141 sf
= malloc(sizeof(struct split_file
));
142 bzero(sf
, sizeof(struct split_file
));
143 buf
= malloc(CONF_BUF
);
144 while (fgetstr(buf
, CONF_BUF
, conffd
) > 0) {
146 while ((*cp
!= '\0') && (isspace(*cp
) == 0))
152 while ((*cp
!= '\0') && (isspace(*cp
) != 0))
157 sf
->filesv
= realloc(sf
->filesv
, sizeof(*(sf
->filesv
)) * sf
->filesc
);
158 sf
->descsv
= realloc(sf
->descsv
, sizeof(*(sf
->descsv
)) * sf
->filesc
);
159 sf
->filesv
[sf
->filesc
- 1] = strdup(buf
);
160 sf
->descsv
[sf
->filesc
- 1] = strdup(cp
);
165 if (sf
->filesc
== 0) {
166 split_file_destroy(sf
);
169 errno
= split_openfile(sf
);
171 split_file_destroy(sf
);
175 /* Looks OK, we'll take it */
181 splitfs_close(struct open_file
*f
)
184 struct split_file
*sf
;
186 sf
= (struct split_file
*)f
->f_fsdata
;
188 split_file_destroy(sf
);
193 splitfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
197 struct split_file
*sf
;
199 sf
= (struct split_file
*)f
->f_fsdata
;
202 nread
= read(sf
->curfd
, buf
, size
- totread
);
208 sf
->tot_pos
+= nread
;
209 sf
->file_pos
+= nread
;
211 buf
= (char *)buf
+ nread
;
213 if (totread
< size
) { /* EOF */
214 if (sf
->curfile
== (sf
->filesc
- 1)) /* Last slice */
217 /* Close previous slice */
218 if (close(sf
->curfd
) != 0)
222 errno
= split_openfile(sf
);
226 } while (totread
< size
);
229 *resid
= size
- totread
;
235 splitfs_seek(struct open_file
*f
, off_t offset
, int where
)
239 off_t new_pos
, seek_by
;
240 struct split_file
*sf
;
242 sf
= (struct split_file
*)f
->f_fsdata
;
247 seek_by
-= sf
->tot_pos
;
252 panic("splitfs_seek: SEEK_END not supported");
261 * Seek forward - implemented using splitfs_read(), because otherwise we'll be
262 * unable to detect that we have crossed slice boundary and hence
263 * unable to do a long seek crossing that boundary.
267 tmp
= malloc(SEEK_BUF
);
274 for (; seek_by
> 0; seek_by
-= nread
) {
276 errno
= splitfs_read(f
, tmp
, min(seek_by
, SEEK_BUF
), &resid
);
277 nread
= min(seek_by
, SEEK_BUF
) - resid
;
278 if ((errno
!= 0) || (nread
== 0))
288 /* Seek backward or seek past the boundary of the last slice */
289 if (sf
->file_pos
+ seek_by
< 0)
290 panic("splitfs_seek: can't seek past the beginning of the slice");
291 new_pos
= lseek(sf
->curfd
, seek_by
, SEEK_CUR
);
296 sf
->tot_pos
+= new_pos
- sf
->file_pos
;
297 sf
->file_pos
= new_pos
;
300 return (sf
->tot_pos
);
304 splitfs_stat(struct open_file
*f
, struct stat
*sb
)
307 struct split_file
*sf
= (struct split_file
*)f
->f_fsdata
;
309 /* stat as normal, but indicate that size is unknown */
310 if ((result
= fstat(sf
->curfd
, sb
)) == 0)