librsync  2.3.1
fileutil.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- library for network deltas
4  *
5  * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6  * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 #include "config.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #ifdef HAVE_FCNTL_H
33 # include <fcntl.h>
34 #endif
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_FILE_H
39 # include <sys/file.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 #endif
44 #include "librsync.h"
45 #include "trace.h"
46 
47 /* Use fseeko64, _fseeki64, or fseeko for long files if they exist. */
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))
55 #endif
56 
57 /* Use fstat64 or _fstati64 for long file fstat if they exist. */
58 #if defined(HAVE_FSTAT64) && (SIZEOF_OFF_T < 8)
59 # define stat stat64
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))
64 #endif
65 
66 /* Make sure S_ISREG is defined. */
67 #ifndef S_ISREG
68 # define S_ISREG(x) ((x) & _S_IFREG)
69 #endif
70 
71 /* Use _fileno if it exists and fileno doesn't. */
72 #if !defined(HAVE_FILENO) && defined(HAVE__FILENO)
73 # define fileno(f) _fileno((f))
74 #endif
75 
76 FILE *rs_file_open(char const *filename, char const *mode, int force)
77 {
78  FILE *f;
79  int is_write;
80 
81  is_write = mode[0] == 'w';
82 
83  if (!filename || !strcmp("-", filename)) {
84  if (is_write) {
85 #if _WIN32
86  _setmode(_fileno(stdout), _O_BINARY);
87 #endif
88  return stdout;
89  } else {
90 #if _WIN32
91  _setmode(_fileno(stdin), _O_BINARY);
92 #endif
93  return stdin;
94  }
95  }
96 
97  if (!force && is_write) {
98  if ((f = fopen(filename, "rb"))) {
99  // File exists
100  rs_error("File exists \"%s\", aborting!", filename);
101  fclose(f);
102  exit(RS_IO_ERROR);
103  }
104  }
105 
106  if (!(f = fopen(filename, mode))) {
107  rs_error("Error opening \"%s\" for %s: %s", filename,
108  is_write ? "write" : "read", strerror(errno));
109  exit(RS_IO_ERROR);
110  }
111 
112  return f;
113 }
114 
115 int rs_file_close(FILE *f)
116 {
117  if ((f == stdin) || (f == stdout))
118  return 0;
119  return fclose(f);
120 }
121 
122 rs_long_t rs_file_size(FILE *f)
123 {
124  struct stat st;
125  if ((fstat(fileno(f), &st) == 0) && (S_ISREG(st.st_mode)))
126  return st.st_size;
127  return -1;
128 }
129 
130 rs_result rs_file_copy_cb(void *arg, rs_long_t pos, size_t *len, void **buf)
131 {
132  int got;
133  FILE *f = (FILE *)arg;
134 
135  if (fseek(f, pos, SEEK_SET)) {
136  rs_error("seek failed: %s", strerror(errno));
137  return RS_IO_ERROR;
138  }
139 
140  got = fread(*buf, 1, *len, f);
141  if (got == -1) {
142  rs_error("read error: %s", strerror(errno));
143  return RS_IO_ERROR;
144  } else if (got == 0) {
145  rs_error("unexpected eof on fd%d", fileno(f));
146  return RS_INPUT_ENDED;
147  } else {
148  *len = got;
149  return RS_DONE;
150  }
151 }
rs_file_size
LIBRSYNC_EXPORT rs_long_t rs_file_size(FILE *file)
Get the size of a file.
Definition: fileutil.c:122
rs_result
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
trace.h
RS_DONE
Completed successfully.
Definition: librsync.h:181
rs_file_open
LIBRSYNC_EXPORT FILE * rs_file_open(char const *filename, char const *mode, int force)
Open a file with special handling for stdin or stdout.
Definition: fileutil.c:76
librsync.h
RS_INPUT_ENDED
Unexpected end of input file, perhaps due to a truncated file or dropped network connection.
Definition: librsync.h:190
RS_IO_ERROR
Error in file or network IO.
Definition: librsync.h:187
rs_file_close
LIBRSYNC_EXPORT int rs_file_close(FILE *file)
Close a file with special handling for stdin or stdout.
Definition: fileutil.c:115
rs_file_copy_cb
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.
Definition: fileutil.c:130