file.c revision 60584
137535Sdes/*- 237535Sdes * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 337535Sdes * All rights reserved. 437535Sdes * 537535Sdes * Redistribution and use in source and binary forms, with or without 637535Sdes * modification, are permitted provided that the following conditions 737535Sdes * are met: 837535Sdes * 1. Redistributions of source code must retain the above copyright 937535Sdes * notice, this list of conditions and the following disclaimer 1037535Sdes * in this position and unchanged. 1137535Sdes * 2. Redistributions in binary form must reproduce the above copyright 1237535Sdes * notice, this list of conditions and the following disclaimer in the 1337535Sdes * documentation and/or other materials provided with the distribution. 1437535Sdes * 3. The name of the author may not be used to endorse or promote products 1537535Sdes * derived from this software without specific prior written permission 1637535Sdes * 1737535Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1837535Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1937535Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2037535Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2137535Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2237535Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2337535Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2437535Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2537535Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2637535Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2737535Sdes * 2850476Speter * $FreeBSD: head/lib/libfetch/file.c 60584 2000-05-15 08:33:58Z des $ 2937535Sdes */ 3037535Sdes 3141862Sdes#include <sys/param.h> 3240975Sdes#include <sys/stat.h> 3341989Sdes 3441989Sdes#include <dirent.h> 3537535Sdes#include <stdio.h> 3637535Sdes#include <string.h> 3737535Sdes 3837535Sdes#include "fetch.h" 3940975Sdes#include "common.h" 4037535Sdes 4137535SdesFILE * 4240975SdesfetchGetFile(struct url *u, char *flags) 4337535Sdes{ 4440975Sdes FILE *f; 4540975Sdes 4640975Sdes f = fopen(u->doc, "r"); 4740975Sdes 4840975Sdes if (f == NULL) 4940975Sdes _fetch_syserr(); 5060187Sdes 5160187Sdes if (u->offset && fseek(f, u->offset, SEEK_SET) == -1) { 5260187Sdes fclose(f); 5360187Sdes _fetch_syserr(); 5460187Sdes } 5560187Sdes 5640975Sdes return f; 5737535Sdes} 5837535Sdes 5937535SdesFILE * 6040975SdesfetchPutFile(struct url *u, char *flags) 6137535Sdes{ 6240975Sdes FILE *f; 6340975Sdes 6455544Sdes if (flags && strchr(flags, 'a')) 6540975Sdes f = fopen(u->doc, "a"); 6640975Sdes else 6760187Sdes f = fopen(u->doc, "w+"); 6840975Sdes 6940975Sdes if (f == NULL) 7040975Sdes _fetch_syserr(); 7160187Sdes 7260187Sdes if (u->offset && fseek(f, u->offset, SEEK_SET) == -1) { 7360187Sdes fclose(f); 7460187Sdes _fetch_syserr(); 7560187Sdes } 7660187Sdes 7740975Sdes return f; 7837535Sdes} 7940975Sdes 8041989Sdesstatic int 8141989Sdes_fetch_stat_file(char *fn, struct url_stat *us) 8240975Sdes{ 8340975Sdes struct stat sb; 8440975Sdes 8560584Sdes us->size = -1; 8660584Sdes us->atime = us->mtime = 0; 8741989Sdes if (stat(fn, &sb) == -1) { 8840975Sdes _fetch_syserr(); 8940975Sdes return -1; 9040975Sdes } 9140975Sdes us->size = sb.st_size; 9241862Sdes us->atime = sb.st_atime; 9341862Sdes us->mtime = sb.st_mtime; 9440975Sdes return 0; 9540975Sdes} 9641989Sdes 9741989Sdesint 9841989SdesfetchStatFile(struct url *u, struct url_stat *us, char *flags) 9941989Sdes{ 10041989Sdes return _fetch_stat_file(u->doc, us); 10141989Sdes} 10241989Sdes 10341989Sdesstruct url_ent * 10441989SdesfetchListFile(struct url *u, char *flags) 10541989Sdes{ 10641989Sdes DIR *dir; 10741989Sdes struct dirent *de; 10841989Sdes struct url_stat us; 10941989Sdes struct url_ent *ue; 11041989Sdes int size, len; 11141989Sdes char fn[MAXPATHLEN], *p; 11241989Sdes int l; 11341989Sdes 11441989Sdes if ((dir = opendir(u->doc)) == NULL) { 11541989Sdes _fetch_syserr(); 11641989Sdes return NULL; 11741989Sdes } 11841989Sdes 11941989Sdes ue = NULL; 12041989Sdes strncpy(fn, u->doc, sizeof fn - 2); 12141989Sdes fn[sizeof fn - 2] = 0; 12241989Sdes strcat(fn, "/"); 12341989Sdes p = strchr(fn, 0); 12441989Sdes l = sizeof fn - strlen(fn) - 1; 12541989Sdes 12641989Sdes while ((de = readdir(dir)) != NULL) { 12741989Sdes strncpy(p, de->d_name, l - 1); 12841989Sdes p[l - 1] = 0; 12941989Sdes if (_fetch_stat_file(fn, &us) == -1) 13041989Sdes /* should I return a partial result, or abort? */ 13141989Sdes break; 13241989Sdes _fetch_add_entry(&ue, &size, &len, de->d_name, &us); 13341989Sdes } 13441989Sdes 13541989Sdes return ue; 13641989Sdes} 137