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