#include "data/sys-file-private.h"
#include <errno.h>
+#include <fcntl.h>
#include <float.h>
#include <inttypes.h>
#include <stdlib.h>
#include "libpspp/array.h"
#include "libpspp/assertion.h"
#include "libpspp/compiler.h"
+#include "libpspp/fbuf.h"
#include "libpspp/i18n.h"
#include "libpspp/ll.h"
#include "libpspp/message.h"
/* File state. */
struct file_handle *fh; /* File handle. */
struct fh_lock *lock; /* Mutual exclusion for file handle. */
- FILE *file; /* File stream. */
+ struct fbuf *fbuf; /* File stream. */
off_t pos; /* Position in file. */
bool error; /* I/O or corruption error? */
struct caseproto *proto; /* Format of output cases. */
if (r->lock == NULL)
goto error;
- r->file = fn_open (fh, "rb");
- if (r->file == NULL)
+ int fd = fn_open (fh, O_RDONLY | O_BINARY, 0);
+ if (fd < 0)
{
msg (ME, _("Error opening `%s' for reading as a system file: %s."),
fh_get_file_name (r->fh), strerror (errno));
goto error;
}
+ r->fbuf = fbuf_open_fd (fd);
if (!read_dictionary (r))
goto error;
struct sfm_reader *r = sfm_reader_cast (r_);
bool error;
- if (r->file)
+ if (r->fbuf)
{
- if (fn_close (r->fh, r->file) == EOF)
+ int error = fbuf_close (r->fbuf);
+ if (error)
{
msg (ME, _("Error closing system file `%s': %s."),
- fh_get_file_name (r->fh), strerror (errno));
+ fh_get_file_name (r->fh), strerror (error));
r->error = true;
}
- r->file = NULL;
+ r->fbuf = NULL;
}
any_read_info_destroy (&r->info);
read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
void *buf, size_t byte_cnt)
{
- size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
+ size_t bytes_read = fbuf_read (r->fbuf, buf, byte_cnt);
r->pos += bytes_read;
if (bytes_read == byte_cnt)
return 1;
- else if (ferror (r->file))
+
+ int status = fbuf_get_status (r->fbuf);
+ if (status > 0)
{
sys_error (r, r->pos, _("System error: %s."), strerror (errno));
return -1;
static void
seek (struct sfm_reader *r, off_t offset)
{
- if (fseeko (r->file, offset, SEEK_SET))
+ int error = fbuf_seek (r->fbuf, offset);
+ if (error)
sys_error (r, 0, _("%s: seek failed (%s)."),
- fh_get_file_name (r->fh), strerror (errno));
+ fh_get_file_name (r->fh), strerror (error));
r->pos = offset;
}
unsigned int block_size;
unsigned int n_blocks;
unsigned int i;
- struct stat s;
- if (fstat (fileno (r->file), &s))
+ int seekable = fbuf_is_seekable (r->fbuf);
+ if (seekable < 0)
{
sys_error (r, 0, _("%s: stat failed (%s)."),
- fh_get_file_name (r->fh), strerror (errno));
+ fh_get_file_name (r->fh), strerror (-seekable));
return false;
}
-
- if (!S_ISREG (s.st_mode))
+ else if (!seekable)
{
/* We can't seek to the trailer and then back to the data in this file,
so skip doing extra checks. */
return true;
}
- if (r->ztrailer_ofs + ztrailer_len != s.st_size)
+ off_t size = fbuf_get_size (r->fbuf);
+ if (size >= 0 && r->ztrailer_ofs + ztrailer_len != size)
sys_warn (r, r->pos,
_("End of ZLIB trailer (0x%llx) is not file size (0x%llx)."),
- r->ztrailer_ofs + ztrailer_len, (long long int) s.st_size);
+ r->ztrailer_ofs + ztrailer_len, (long long int) size);
seek (r, r->ztrailer_ofs);