xref: /freebsd/lib/libfetch/file.c (revision b95b56c7a06bd268299c6d3b1104e09831e600dc)
1 /*-
2  * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 
34 #include <dirent.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #include "fetch.h"
39 #include "common.h"
40 
41 FILE *
42 fetchGetFile(struct url *u, char *flags)
43 {
44     FILE *f;
45 
46     f = fopen(u->doc, "r");
47 
48     if (f == NULL)
49 	_fetch_syserr();
50 
51     if (u->offset && fseek(f, u->offset, SEEK_SET) == -1) {
52 	fclose(f);
53 	_fetch_syserr();
54     }
55 
56     return f;
57 }
58 
59 FILE *
60 fetchPutFile(struct url *u, char *flags)
61 {
62     FILE *f;
63 
64     if (flags && strchr(flags, 'a'))
65 	f = fopen(u->doc, "a");
66     else
67 	f = fopen(u->doc, "w+");
68 
69     if (f == NULL)
70 	_fetch_syserr();
71 
72     if (u->offset && fseek(f, u->offset, SEEK_SET) == -1) {
73 	fclose(f);
74 	_fetch_syserr();
75     }
76 
77     return f;
78 }
79 
80 static int
81 _fetch_stat_file(char *fn, struct url_stat *us)
82 {
83     struct stat sb;
84 
85     if (stat(fn, &sb) == -1) {
86 	_fetch_syserr();
87 	return -1;
88     }
89     us->size = sb.st_size;
90     us->atime = sb.st_atime;
91     us->mtime = sb.st_mtime;
92     return 0;
93 }
94 
95 int
96 fetchStatFile(struct url *u, struct url_stat *us, char *flags)
97 {
98     return _fetch_stat_file(u->doc, us);
99 }
100 
101 struct url_ent *
102 fetchListFile(struct url *u, char *flags)
103 {
104     DIR *dir;
105     struct dirent *de;
106     struct url_stat us;
107     struct url_ent *ue;
108     int size, len;
109     char fn[MAXPATHLEN], *p;
110     int l;
111 
112     if ((dir = opendir(u->doc)) == NULL) {
113 	_fetch_syserr();
114 	return NULL;
115     }
116 
117     ue = NULL;
118     strncpy(fn, u->doc, sizeof fn - 2);
119     fn[sizeof fn - 2] = 0;
120     strcat(fn, "/");
121     p = strchr(fn, 0);
122     l = sizeof fn - strlen(fn) - 1;
123 
124     while ((de = readdir(dir)) != NULL) {
125 	strncpy(p, de->d_name, l - 1);
126 	p[l - 1] = 0;
127 	if (_fetch_stat_file(fn, &us) == -1)
128 	    /* should I return a partial result, or abort? */
129 	    break;
130 	_fetch_add_entry(&ue, &size, &len, de->d_name, &us);
131     }
132 
133     return ue;
134 }
135