35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_FILE_H
39 # include <sys/file.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
48 #if defined(HAVE_FSEEKO64) && (SIZEOF_OFF_T < 8)
49 # define fopen(f, m) fopen64((f), (m))
50 # define fseek(f, o, w) fseeko64((f), (o), (w))
51 #elif defined(HAVE__FSEEKI64)
52 # define fseek(f, o, w) _fseeki64((f), (o), (w))
53 #elif defined(HAVE_FSEEKO)
54 # define fseek(f, o, w) fseeko((f), (o), (w))
58 #if defined(HAVE_FSTAT64) && (SIZEOF_OFF_T < 8)
60 # define fstat(f,s) fstat64((f), (s))
61 #elif defined(HAVE__FSTATI64)
62 # define stat _stati64
63 # define fstat(f,s) _fstati64((f), (s))
68 # define S_ISREG(x) ((x) & _S_IFREG)
72 #if !defined(HAVE_FILENO) && defined(HAVE__FILENO)
73 # define fileno(f) _fileno((f))
76 FILE *
rs_file_open(
char const *filename,
char const *mode,
int force)
81 is_write = mode[0] ==
'w';
83 if (!filename || !strcmp(
"-", filename)) {
86 _setmode(_fileno(stdout), _O_BINARY);
91 _setmode(_fileno(stdin), _O_BINARY);
97 if (!force && is_write) {
98 if ((f = fopen(filename,
"rb"))) {
100 rs_error(
"File exists \"%s\", aborting!", filename);
106 if (!(f = fopen(filename, mode))) {
107 rs_error(
"Error opening \"%s\" for %s: %s", filename,
108 is_write ?
"write" :
"read", strerror(errno));
117 if ((f == stdin) || (f == stdout))
125 if ((fstat(fileno(f), &st) == 0) && (S_ISREG(st.st_mode)))
133 FILE *f = (FILE *)arg;
135 if (fseek(f, pos, SEEK_SET)) {
136 rs_error(
"seek failed: %s", strerror(errno));
140 got = fread(*buf, 1, *len, f);
142 rs_error(
"read error: %s", strerror(errno));
144 }
else if (got == 0) {
145 rs_error(
"unexpected eof on fd%d", fileno(f));
Public header for librsync.
LIBRSYNC_EXPORT int rs_file_close(FILE *file)
Close a file with special handling for stdin or stdout.
LIBRSYNC_EXPORT rs_long_t rs_file_size(FILE *file)
Get the size of a file.
rs_result
Return codes from nonblocking rsync operations.
@ RS_DONE
Completed successfully.
@ RS_INPUT_ENDED
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
@ RS_IO_ERROR
Error in file or network IO.
LIBRSYNC_EXPORT rs_result rs_file_copy_cb(void *arg, rs_long_t pos, size_t *len, void **buf)
rs_copy_cb that reads from a stdio file.
LIBRSYNC_EXPORT FILE * rs_file_open(char const *filename, char const *mode, int force)
Open a file with special handling for stdin or stdout.