added hooks for compression in token.c
[rsync/rsync.git] / util.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 Utilities used in rsync
22
23 tridge, June 1996
24 */
25#include "rsync.h"
26
27int num_waiting(int fd)
28{
29 int len=0;
30 ioctl(fd,FIONREAD,&len);
31 return(len);
32}
33
34static int map_fd = -1;
35static off_t map_size = 0;
36static char *p = NULL;
37static int p_size = 0;
38static int p_offset = 0;
39static int p_len = 0;
40
41
42char *map_file(int fd,off_t len)
43{
44 char *ret = NULL;
45#ifdef HAVE_MMAP
46 if (len < MAX_MAP_SIZE)
47 ret = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
48#endif
49 map_fd = fd;
50 map_size = len;
51 return ret;
52}
53
54char *map_ptr(char *buf,off_t offset,int len)
55{
56 if (buf)
57 return buf+offset;
58
59 if (len == 0)
60 return NULL;
61
62 len = MIN(len,map_size-offset);
63
64 if (offset >= p_offset &&
65 offset+len <= p_offset+p_len) {
66 return (p + (offset - p_offset));
67 }
68
69 len = MAX(len,CHUNK_SIZE);
70 len = MIN(len,map_size - offset);
71
72 if (len > p_size) {
73 if (p) free(p);
74 p = (char *)malloc(len);
75 if (!p) out_of_memory("map_ptr");
76 p_size = len;
77 }
78
79 if (lseek(map_fd,offset,SEEK_SET) != offset ||
80 read(map_fd,p,len) != len) {
81 fprintf(FERROR,"EOF in map_ptr!\n");
82 exit_cleanup(1);
83 }
84
85 p_offset = offset;
86 p_len = len;
87
88 return p;
89}
90
91
92void unmap_file(char *buf,off_t len)
93{
94#ifdef HAVE_MMAP
95 if (len > 0 && buf)
96 munmap(buf,len);
97#endif
98 map_fd = -1;
99 map_size = 0;
100 p_len = 0;
101}
102
103
104/* this is taken from CVS */
105int piped_child(char **command,int *f_in,int *f_out)
106{
107 int pid;
108 int to_child_pipe[2];
109 int from_child_pipe[2];
110
111 if (pipe(to_child_pipe) < 0 ||
112 pipe(from_child_pipe) < 0) {
113 fprintf(FERROR,"pipe: %s\n",strerror(errno));
114 exit_cleanup(1);
115 }
116
117
118 pid = fork();
119 if (pid < 0) {
120 fprintf(FERROR,"fork: %s\n",strerror(errno));
121 exit_cleanup(1);
122 }
123
124 if (pid == 0)
125 {
126 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
127 close(to_child_pipe[1]) < 0 ||
128 close(from_child_pipe[0]) < 0 ||
129 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
130 fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
131 exit_cleanup(1);
132 }
133 execvp(command[0], command);
134 fprintf(FERROR,"Failed to exec %s : %s\n",
135 command[0],strerror(errno));
136 exit_cleanup(1);
137 }
138
139 if (close(from_child_pipe[1]) < 0 ||
140 close(to_child_pipe[0]) < 0) {
141 fprintf(FERROR,"Failed to close : %s\n",strerror(errno));
142 exit_cleanup(1);
143 }
144
145 *f_in = from_child_pipe[0];
146 *f_out = to_child_pipe[1];
147
148 return pid;
149}
150
151
152void out_of_memory(char *str)
153{
154 fprintf(FERROR,"out of memory in %s\n",str);
155 exit_cleanup(1);
156}
157
158
159#ifndef HAVE_STRDUP
160 char *strdup(char *s)
161{
162 int l = strlen(s) + 1;
163 char *ret = (char *)malloc(l);
164 if (ret)
165 strcpy(ret,s);
166 return ret;
167}
168#endif
169
170
171int set_modtime(char *fname,time_t modtime)
172{
173#ifdef HAVE_UTIME_H
174 struct utimbuf tbuf;
175 tbuf.actime = time(NULL);
176 tbuf.modtime = modtime;
177 return utime(fname,&tbuf);
178#elif defined(HAVE_UTIME)
179 time_t t[2];
180 t[0] = time(NULL);
181 t[1] = modtime;
182 return utime(fname,t);
183#else
184 struct timeval t[2];
185 t[0].tv_sec = time(NULL);
186 t[0].tv_usec = 0;
187 t[1].tv_sec = modtime;
188 t[1].tv_usec = 0;
189 return utimes(fname,t);
190#endif
191}
192
193
194
195/****************************************************************************
196Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
197else
198if SYSV use O_NDELAY
199if BSD use FNDELAY
200****************************************************************************/
201int set_blocking(int fd, int set)
202{
203 int val;
204#ifdef O_NONBLOCK
205#define FLAG_TO_SET O_NONBLOCK
206#else
207#ifdef SYSV
208#define FLAG_TO_SET O_NDELAY
209#else /* BSD */
210#define FLAG_TO_SET FNDELAY
211#endif
212#endif
213
214 if((val = fcntl(fd, F_GETFL, 0)) == -1)
215 return -1;
216 if(set) /* Turn blocking on - ie. clear nonblock flag */
217 val &= ~FLAG_TO_SET;
218 else
219 val |= FLAG_TO_SET;
220 return fcntl( fd, F_SETFL, val);
221#undef FLAG_TO_SET
222}