file.c
changeset 0 1d0ce1ebbc72
equal deleted inserted replaced
-1:000000000000 0:1d0ce1ebbc72
       
     1 /*
       
     2  * Copyright (c) 2015 Sunil Nimmagadda <sunil@openbsd.org>
       
     3  *
       
     4  * Permission to use, copy, modify, and distribute this software for any
       
     5  * purpose with or without fee is hereby granted, provided that the above
       
     6  * copyright notice and this permission notice appear in all copies.
       
     7  *
       
     8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
       
     9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
       
    10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
       
    11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
       
    12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
       
    13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
       
    14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
       
    15  */
       
    16 
       
    17 #include <sys/stat.h>
       
    18 
       
    19 #include <err.h>
       
    20 #include <fcntl.h>
       
    21 #include <stdio.h>
       
    22 
       
    23 #include "ftp.h"
       
    24 
       
    25 static FILE	*src_fp;
       
    26 
       
    27 struct url *
       
    28 file_get(struct url *url, off_t *offset, off_t *sz)
       
    29 {
       
    30 	struct stat	sb;
       
    31 	int		src_fd;
       
    32 
       
    33 	if ((src_fd = fd_request(url->path, O_RDONLY, NULL)) == -1)
       
    34 		err(1, "Can't open file %s", url->path);
       
    35 
       
    36 	if (fstat(src_fd, &sb) == 0)
       
    37 		*sz = sb.st_size;
       
    38 
       
    39 	if ((src_fp = fdopen(src_fd, "r")) == NULL)
       
    40 		err(1, "%s: fdopen", __func__);
       
    41 
       
    42 	if (*offset && fseeko(src_fp, *offset, SEEK_SET) == -1)
       
    43 		err(1, "%s: fseeko", __func__);
       
    44 
       
    45 	return url;
       
    46 }
       
    47 
       
    48 void
       
    49 file_save(struct url *url, FILE *dst_fp, off_t *offset)
       
    50 {
       
    51 	copy_file(dst_fp, src_fp, offset);
       
    52 	fclose(src_fp);
       
    53 }