1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1998-2011 Dag-Erling Smørgrav 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/stat.h> 36 37 #include <dirent.h> 38 #include <fcntl.h> 39 #include <stdio.h> 40 #include <string.h> 41 42 #include "fetch.h" 43 #include "common.h" 44 45 FILE * 46 fetchXGetFile(struct url *u, struct url_stat *us, const char *flags) 47 { 48 FILE *f; 49 50 if (us && fetchStatFile(u, us, flags) == -1) 51 return (NULL); 52 53 f = fopen(u->doc, "re"); 54 55 if (f == NULL) { 56 fetch_syserr(); 57 return (NULL); 58 } 59 60 if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) { 61 fclose(f); 62 fetch_syserr(); 63 return (NULL); 64 } 65 66 return (f); 67 } 68 69 FILE * 70 fetchGetFile(struct url *u, const char *flags) 71 { 72 return (fetchXGetFile(u, NULL, flags)); 73 } 74 75 FILE * 76 fetchPutFile(struct url *u, const char *flags) 77 { 78 FILE *f; 79 80 if (CHECK_FLAG('a')) 81 f = fopen(u->doc, "ae"); 82 else 83 f = fopen(u->doc, "w+e"); 84 85 if (f == NULL) { 86 fetch_syserr(); 87 return (NULL); 88 } 89 90 if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) { 91 fclose(f); 92 fetch_syserr(); 93 return (NULL); 94 } 95 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 closedir(dir); 155 return (ue); 156 } 157