Home | History | Annotate | Line # | Download | only in libfetch
file.c revision 1.1.2.2
      1  1.1.2.2  matt /*-
      2  1.1.2.2  matt  * Copyright (c) 1998-2004 Dag-Erling Codan Smrgrav
      3  1.1.2.2  matt  * All rights reserved.
      4  1.1.2.2  matt  *
      5  1.1.2.2  matt  * Redistribution and use in source and binary forms, with or without
      6  1.1.2.2  matt  * modification, are permitted provided that the following conditions
      7  1.1.2.2  matt  * are met:
      8  1.1.2.2  matt  * 1. Redistributions of source code must retain the above copyright
      9  1.1.2.2  matt  *    notice, this list of conditions and the following disclaimer
     10  1.1.2.2  matt  *    in this position and unchanged.
     11  1.1.2.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1.2.2  matt  *    notice, this list of conditions and the following disclaimer in the
     13  1.1.2.2  matt  *    documentation and/or other materials provided with the distribution.
     14  1.1.2.2  matt  * 3. The name of the author may not be used to endorse or promote products
     15  1.1.2.2  matt  *    derived from this software without specific prior written permission
     16  1.1.2.2  matt  *
     17  1.1.2.2  matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1.2.2  matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1.2.2  matt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1.2.2  matt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1.2.2  matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1.2.2  matt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1.2.2  matt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1.2.2  matt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1.2.2  matt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1.2.2  matt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1.2.2  matt  */
     28  1.1.2.2  matt 
     29  1.1.2.2  matt #include "free2net.h"
     30  1.1.2.2  matt 
     31  1.1.2.2  matt #include <sys/cdefs.h>
     32  1.1.2.2  matt __FBSDID("$FreeBSD: src/lib/libfetch/file.c,v 1.16.6.1 2006/11/11 00:16:07 des Exp $");
     33  1.1.2.2  matt 
     34  1.1.2.2  matt #include <sys/param.h>
     35  1.1.2.2  matt #include <sys/stat.h>
     36  1.1.2.2  matt 
     37  1.1.2.2  matt #include <dirent.h>
     38  1.1.2.2  matt #include <stdio.h>
     39  1.1.2.2  matt #include <string.h>
     40  1.1.2.2  matt 
     41  1.1.2.2  matt #include "fetch.h"
     42  1.1.2.2  matt #include "common.h"
     43  1.1.2.2  matt 
     44  1.1.2.2  matt FILE *
     45  1.1.2.2  matt fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
     46  1.1.2.2  matt {
     47  1.1.2.2  matt 	FILE *f;
     48  1.1.2.2  matt 
     49  1.1.2.2  matt 	if (us && fetchStatFile(u, us, flags) == -1)
     50  1.1.2.2  matt 		return (NULL);
     51  1.1.2.2  matt 
     52  1.1.2.2  matt 	f = fopen(u->doc, "r");
     53  1.1.2.2  matt 
     54  1.1.2.2  matt 	if (f == NULL)
     55  1.1.2.2  matt 		_fetch_syserr();
     56  1.1.2.2  matt 
     57  1.1.2.2  matt 	if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
     58  1.1.2.2  matt 		fclose(f);
     59  1.1.2.2  matt 		_fetch_syserr();
     60  1.1.2.2  matt 	}
     61  1.1.2.2  matt 
     62  1.1.2.2  matt 	return (f);
     63  1.1.2.2  matt }
     64  1.1.2.2  matt 
     65  1.1.2.2  matt FILE *
     66  1.1.2.2  matt fetchGetFile(struct url *u, const char *flags)
     67  1.1.2.2  matt {
     68  1.1.2.2  matt 	return (fetchXGetFile(u, NULL, flags));
     69  1.1.2.2  matt }
     70  1.1.2.2  matt 
     71  1.1.2.2  matt FILE *
     72  1.1.2.2  matt fetchPutFile(struct url *u, const char *flags)
     73  1.1.2.2  matt {
     74  1.1.2.2  matt 	FILE *f;
     75  1.1.2.2  matt 
     76  1.1.2.2  matt 	if (CHECK_FLAG('a'))
     77  1.1.2.2  matt 		f = fopen(u->doc, "a");
     78  1.1.2.2  matt 	else
     79  1.1.2.2  matt 		f = fopen(u->doc, "w+");
     80  1.1.2.2  matt 
     81  1.1.2.2  matt 	if (f == NULL)
     82  1.1.2.2  matt 		_fetch_syserr();
     83  1.1.2.2  matt 
     84  1.1.2.2  matt 	if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
     85  1.1.2.2  matt 		fclose(f);
     86  1.1.2.2  matt 		_fetch_syserr();
     87  1.1.2.2  matt 	}
     88  1.1.2.2  matt 
     89  1.1.2.2  matt 	return (f);
     90  1.1.2.2  matt }
     91  1.1.2.2  matt 
     92  1.1.2.2  matt static int
     93  1.1.2.2  matt _fetch_stat_file(const char *fn, struct url_stat *us)
     94  1.1.2.2  matt {
     95  1.1.2.2  matt 	struct stat sb;
     96  1.1.2.2  matt 
     97  1.1.2.2  matt 	us->size = -1;
     98  1.1.2.2  matt 	us->atime = us->mtime = 0;
     99  1.1.2.2  matt 	if (stat(fn, &sb) == -1) {
    100  1.1.2.2  matt 		_fetch_syserr();
    101  1.1.2.2  matt 		return (-1);
    102  1.1.2.2  matt 	}
    103  1.1.2.2  matt 	us->size = sb.st_size;
    104  1.1.2.2  matt 	us->atime = sb.st_atime;
    105  1.1.2.2  matt 	us->mtime = sb.st_mtime;
    106  1.1.2.2  matt 	return (0);
    107  1.1.2.2  matt }
    108  1.1.2.2  matt 
    109  1.1.2.2  matt /* ARGSUSED2 */
    110  1.1.2.2  matt int
    111  1.1.2.2  matt fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
    112  1.1.2.2  matt {
    113  1.1.2.2  matt 	return (_fetch_stat_file(u->doc, us));
    114  1.1.2.2  matt }
    115  1.1.2.2  matt 
    116  1.1.2.2  matt /* ARGSUSED1 */
    117  1.1.2.2  matt struct url_ent *
    118  1.1.2.2  matt fetchListFile(struct url *u, const char *flags __unused)
    119  1.1.2.2  matt {
    120  1.1.2.2  matt 	struct dirent *de;
    121  1.1.2.2  matt 	struct url_stat us;
    122  1.1.2.2  matt 	struct url_ent *ue;
    123  1.1.2.2  matt 	int size, len;
    124  1.1.2.2  matt 	char fn[PATH_MAX], *p;
    125  1.1.2.2  matt 	DIR *dir;
    126  1.1.2.2  matt 	int l;
    127  1.1.2.2  matt 
    128  1.1.2.2  matt 	if ((dir = opendir(u->doc)) == NULL) {
    129  1.1.2.2  matt 		_fetch_syserr();
    130  1.1.2.2  matt 		return (NULL);
    131  1.1.2.2  matt 	}
    132  1.1.2.2  matt 
    133  1.1.2.2  matt 	ue = NULL;
    134  1.1.2.2  matt 	strncpy(fn, u->doc, sizeof(fn) - 2);
    135  1.1.2.2  matt 	fn[sizeof(fn) - 2] = 0;
    136  1.1.2.2  matt 	strcat(fn, "/");
    137  1.1.2.2  matt 	p = strchr(fn, 0);
    138  1.1.2.2  matt 	l = sizeof(fn) - strlen(fn) - 1;
    139  1.1.2.2  matt 
    140  1.1.2.2  matt 	while ((de = readdir(dir)) != NULL) {
    141  1.1.2.2  matt 		strncpy(p, de->d_name, (unsigned)(l - 1));
    142  1.1.2.2  matt 		p[l - 1] = 0;
    143  1.1.2.2  matt 		if (_fetch_stat_file(fn, &us) == -1)
    144  1.1.2.2  matt 			/* should I return a partial result, or abort? */
    145  1.1.2.2  matt 			break;
    146  1.1.2.2  matt 		_fetch_add_entry(&ue, &size, &len, de->d_name, &us);
    147  1.1.2.2  matt 	}
    148  1.1.2.2  matt 
    149  1.1.2.2  matt 	return (ue);
    150  1.1.2.2  matt }
    151