1/*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * filestr.c
6 *
7 * This file contains extensions to the string/buffer API, to load a file
8 * into a buffer.
9 */
10
11#include "filestr.h"
12/* Get access to "private" functions */
13#define VSFTP_STRING_HELPER
14#include "str.h"
15#include "sysutil.h"
16#include "secbuf.h"
17
18int
19str_fileread(struct mystr* p_str, const char* p_filename, unsigned int maxsize)
20{
21  int fd;
22  int retval;
23  filesize_t size;
24  char* p_sec_buf = 0;
25  struct vsf_sysutil_statbuf* p_stat = 0;
26  /* In case we fail, make sure we return an empty string */
27  str_empty(p_str);
28  fd = vsf_sysutil_open_file(p_filename, kVSFSysUtilOpenReadOnly);
29  if (vsf_sysutil_retval_is_error(fd))
30  {
31    return fd;
32  }
33  vsf_sysutil_fstat(fd, &p_stat);
34  if (vsf_sysutil_statbuf_is_regfile(p_stat))
35  {
36    size = vsf_sysutil_statbuf_get_size(p_stat);
37    if (size > maxsize)
38    {
39      size = maxsize;
40    }
41    vsf_secbuf_alloc(&p_sec_buf, (unsigned int) size);
42
43    retval = vsf_sysutil_read_loop(fd, p_sec_buf, (unsigned int) size);
44    if (!vsf_sysutil_retval_is_error(retval) && (unsigned int) retval == size)
45    {
46      str_alloc_memchunk(p_str, p_sec_buf, size);
47    }
48  }
49  vsf_sysutil_free(p_stat);
50  vsf_secbuf_free(&p_sec_buf);
51  vsf_sysutil_close(fd);
52  return 0;
53}
54
55