file.c revision 75891
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 75891 2001-04-24 00:06:21Z archie $
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 *
4275891SarchiefetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
4337535Sdes{
4440975Sdes    FILE *f;
4540975Sdes
4663340Sdes    if (us && fetchStatFile(u, us, flags) == -1)
4763340Sdes	return NULL;
4863340Sdes
4940975Sdes    f = fopen(u->doc, "r");
5040975Sdes
5140975Sdes    if (f == NULL)
5240975Sdes	_fetch_syserr();
5360187Sdes
5460187Sdes    if (u->offset && fseek(f, u->offset, SEEK_SET) == -1) {
5560187Sdes	fclose(f);
5660187Sdes	_fetch_syserr();
5760187Sdes    }
5860187Sdes
5940975Sdes    return f;
6037535Sdes}
6137535Sdes
6237535SdesFILE *
6375891SarchiefetchGetFile(struct url *u, const char *flags)
6463340Sdes{
6563340Sdes    return fetchXGetFile(u, NULL, flags);
6663340Sdes}
6763340Sdes
6863340SdesFILE *
6975891SarchiefetchPutFile(struct url *u, const char *flags)
7037535Sdes{
7140975Sdes    FILE *f;
7240975Sdes
7367892Sdes    if (CHECK_FLAG('a'))
7440975Sdes	f = fopen(u->doc, "a");
7540975Sdes    else
7660187Sdes	f = fopen(u->doc, "w+");
7740975Sdes
7840975Sdes    if (f == NULL)
7940975Sdes	_fetch_syserr();
8060187Sdes
8160187Sdes    if (u->offset && fseek(f, u->offset, SEEK_SET) == -1) {
8260187Sdes	fclose(f);
8360187Sdes	_fetch_syserr();
8460187Sdes    }
8560187Sdes
8640975Sdes    return f;
8737535Sdes}
8840975Sdes
8941989Sdesstatic int
9075891Sarchie_fetch_stat_file(const char *fn, struct url_stat *us)
9140975Sdes{
9240975Sdes    struct stat sb;
9340975Sdes
9460584Sdes    us->size = -1;
9560584Sdes    us->atime = us->mtime = 0;
9641989Sdes    if (stat(fn, &sb) == -1) {
9740975Sdes	_fetch_syserr();
9840975Sdes	return -1;
9940975Sdes    }
10040975Sdes    us->size = sb.st_size;
10141862Sdes    us->atime = sb.st_atime;
10241862Sdes    us->mtime = sb.st_mtime;
10340975Sdes    return 0;
10440975Sdes}
10541989Sdes
10641989Sdesint
10775891SarchiefetchStatFile(struct url *u, struct url_stat *us, const char *flags)
10841989Sdes{
10941989Sdes    return _fetch_stat_file(u->doc, us);
11041989Sdes}
11141989Sdes
11241989Sdesstruct url_ent *
11375891SarchiefetchListFile(struct url *u, const char *flags)
11441989Sdes{
11541989Sdes    DIR *dir;
11641989Sdes    struct dirent *de;
11741989Sdes    struct url_stat us;
11841989Sdes    struct url_ent *ue;
11941989Sdes    int size, len;
12041989Sdes    char fn[MAXPATHLEN], *p;
12141989Sdes    int l;
12241989Sdes
12341989Sdes    if ((dir = opendir(u->doc)) == NULL) {
12441989Sdes	_fetch_syserr();
12541989Sdes	return NULL;
12641989Sdes    }
12741989Sdes
12841989Sdes    ue = NULL;
12941989Sdes    strncpy(fn, u->doc, sizeof fn - 2);
13041989Sdes    fn[sizeof fn - 2] = 0;
13141989Sdes    strcat(fn, "/");
13241989Sdes    p = strchr(fn, 0);
13341989Sdes    l = sizeof fn - strlen(fn) - 1;
13441989Sdes
13541989Sdes    while ((de = readdir(dir)) != NULL) {
13641989Sdes	strncpy(p, de->d_name, l - 1);
13741989Sdes	p[l - 1] = 0;
13841989Sdes	if (_fetch_stat_file(fn, &us) == -1)
13941989Sdes	    /* should I return a partial result, or abort? */
14041989Sdes	    break;
14141989Sdes	_fetch_add_entry(&ue, &size, &len, de->d_name, &us);
14241989Sdes    }
14341989Sdes
14441989Sdes    return ue;
14541989Sdes}
146