]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libfetch/file.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libfetch / file.c
1 /*-
2  * Copyright (c) 1998-2011 Dag-Erling 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/stat.h>
34
35 #include <dirent.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "fetch.h"
41 #include "common.h"
42
43 FILE *
44 fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
45 {
46         FILE *f;
47
48         if (us && fetchStatFile(u, us, flags) == -1)
49                 return (NULL);
50
51         f = fopen(u->doc, "r");
52
53         if (f == NULL) {
54                 fetch_syserr();
55                 return (NULL);
56         }
57
58         if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
59                 fclose(f);
60                 fetch_syserr();
61                 return (NULL);
62         }
63
64         fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
65         return (f);
66 }
67
68 FILE *
69 fetchGetFile(struct url *u, const char *flags)
70 {
71         return (fetchXGetFile(u, NULL, flags));
72 }
73
74 FILE *
75 fetchPutFile(struct url *u, const char *flags)
76 {
77         FILE *f;
78
79         if (CHECK_FLAG('a'))
80                 f = fopen(u->doc, "a");
81         else
82                 f = fopen(u->doc, "w+");
83
84         if (f == NULL) {
85                 fetch_syserr();
86                 return (NULL);
87         }
88
89         if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
90                 fclose(f);
91                 fetch_syserr();
92                 return (NULL);
93         }
94
95         fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
96         return (f);
97 }
98
99 static int
100 fetch_stat_file(const char *fn, struct url_stat *us)
101 {
102         struct stat sb;
103
104         us->size = -1;
105         us->atime = us->mtime = 0;
106         if (stat(fn, &sb) == -1) {
107                 fetch_syserr();
108                 return (-1);
109         }
110         us->size = sb.st_size;
111         us->atime = sb.st_atime;
112         us->mtime = sb.st_mtime;
113         return (0);
114 }
115
116 int
117 fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
118 {
119         return (fetch_stat_file(u->doc, us));
120 }
121
122 struct url_ent *
123 fetchListFile(struct url *u, const char *flags __unused)
124 {
125         struct dirent *de;
126         struct url_stat us;
127         struct url_ent *ue;
128         int size, len;
129         char fn[PATH_MAX], *p;
130         DIR *dir;
131         int l;
132
133         if ((dir = opendir(u->doc)) == NULL) {
134                 fetch_syserr();
135                 return (NULL);
136         }
137
138         ue = NULL;
139         strncpy(fn, u->doc, sizeof(fn) - 2);
140         fn[sizeof(fn) - 2] = 0;
141         strcat(fn, "/");
142         p = strchr(fn, 0);
143         l = sizeof(fn) - strlen(fn) - 1;
144
145         while ((de = readdir(dir)) != NULL) {
146                 strncpy(p, de->d_name, l - 1);
147                 p[l - 1] = 0;
148                 if (fetch_stat_file(fn, &us) == -1)
149                         /* should I return a partial result, or abort? */
150                         break;
151                 fetch_add_entry(&ue, &size, &len, de->d_name, &us);
152         }
153
154         return (ue);
155 }