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