]> cygwin.com Git - cygwin-apps/setup.git/blob - io_stream_rsync.cc
2002-07-05 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / io_stream_rsync.cc
1 /*
2 * Copyright (c) 2001, Robert Collins.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by Robert Collins <rbtcollins@hotmail.com>
13 *
14 */
15
16 #if 0
17 static const char *cvsid =
18 "\n%%% $Id$\n";
19 #endif
20
21 #include "io_stream_rsync.h"
22 #include "IOStreamProvider.h"
23 #include "rsync/rsync.h"
24
25
26 /* completely private iostream registration class */
27 class RSyncProvider : public IOStreamProvider
28 {
29 public:
30 int exists (String const &path) const
31 {
32 return io_stream_rsync::exists(path);
33 }
34 int remove (String const &path) const
35 {
36 /* Not currently supported */
37 return 1;
38 }
39 int mklink (String const &a , String const &b, io_stream_link_t c) const
40 {
41 return io_stream_rsync::mklink(a,b,c);
42 }
43 io_stream *open (String const &a,String const &b) const
44 {
45 return new io_stream_rsync (a, b);
46 }
47 ~RSyncProvider (){}
48 int move (String const &a,String const &b) const
49 {
50 /* Not supported yet */
51 return 0;
52 }
53 int mkdir_p (enum path_type_t isadir, String const &path) const
54 {
55 /* Not supported yet */
56 return 0;
57 }
58 protected:
59 RSyncProvider() // no creating this
60 {
61 io_stream::registerProvider (theInstance, "rsync://");
62 }
63 RSyncProvider(RSyncProvider const &); // no copying
64 RSyncProvider &operator=(RSyncProvider const &); // no assignment
65 private:
66 static RSyncProvider theInstance;
67 };
68 RSyncProvider RSyncProvider::theInstance = RSyncProvider();
69
70 io_stream_rsync::io_stream_rsync (String const &name, String const &mode):
71 fp(), fname(name),lmode (mode)
72 {
73 errno = 0;
74 if (!name.size() || !mode.size())
75 return;
76 fp = fopen (name.cstr_oneuse(), mode.cstr_oneuse());
77 if (!fp)
78 lasterr = errno;
79 }
80
81 io_stream_rsync::~io_stream_rsync ()
82 {
83 if (fp)
84 fclose (fp);
85 destroyed = 1;
86 }
87
88 int
89 io_stream_rsync::exists (String const &path)
90 {
91 #if defined(WIN32) && !defined (_CYGWIN_)
92 if (_access (path.cstr_oneuse(), 0) == 0)
93 #else
94 if (access (path.cstr_oneuse(), F_OK) == 0)
95 #endif
96 return 1;
97 return 0;
98 }
99
100 int
101 io_stream_rsync::remove (String const &path)
102 {
103 if (!path.size())
104 return 1;
105 #if defined(WIN32) && !defined (_CYGWIN_)
106 unsigned long w = GetFileAttributes (path.cstr_oneuse());
107 if (w != 0xffffffff && w & FILE_ATTRIBUTE_DIRECTORY)
108 {
109 char *tmp = new char [path.size() + 10];
110 int i = 0;
111 do
112 {
113 i++;
114 sprintf (tmp, "%s.old-%d", path.cstr_oneuse(), i);
115 }
116 while (GetFileAttributes (tmp) != 0xffffffff);
117 fprintf (stderr, "warning: moving directory \"%s\" out of the way.\n",
118 path.cstr_oneuse());
119 MoveFile (path.cstr_oneuse(), tmp);
120 delete[] tmp;
121 }
122 return !DeleteFileA (path.cstr_oneuse());
123 #else
124 // FIXME: try rmdir if unlink fails - remove the dir
125 return unlink (path.cstr_oneuse());
126 #endif
127 }
128
129 int
130 io_stream_rsync::mklink (String const &from, String const &to,
131 io_stream_link_t linktype)
132 {
133 if (!from.size() || !to.size())
134 return 1;
135 #if defined(WIN32) && !defined (_CYGWIN_)
136 switch (linktype)
137 {
138 case IO_STREAM_SYMLINK:
139 return mkcygsymlink (from.cstr_oneuse(), to.cstr_oneuse());
140 case IO_STREAM_HARDLINK:
141 return 1;
142 }
143 #else
144 switch (linktype)
145 {
146 case IO_STREAM_SYMLINK:
147 return symlink (to.cstr_oneuse(), from.cstr_oneuse());
148 case IO_STREAM_HARDLINK:
149 return link (to.cstr_oneuse(), from.cstr_oneuse());
150 }
151 #endif
152 return 1;
153 }
154
155 /* virtuals */
156
157
158 ssize_t
159 io_stream_rsync::read (void *buffer, size_t len)
160 {
161 if (fp)
162 return fread (buffer, 1, len, fp);
163 return 0;
164 }
165
166 ssize_t
167 io_stream_rsync::write (const void *buffer, size_t len)
168 {
169 if (fp)
170 return fwrite (buffer, 1, len, fp);
171 return 0;
172 }
173
174 ssize_t
175 io_stream_rsync::peek (void *buffer, size_t len)
176 {
177 if (fp)
178 {
179 int pos = ftell (fp);
180 ssize_t rv = fread (buffer, 1, len, fp);
181 fseek (fp, pos, SEEK_SET);
182 return rv;
183 }
184 return 0;
185 }
186
187 long
188 io_stream_rsync::tell ()
189 {
190 if (fp)
191 {
192 return ftell (fp);
193 }
194 return 0;
195 }
196
197 int
198 io_stream_rsync::seek (long where, io_stream_seek_t whence)
199 {
200 if (fp)
201 {
202 return fseek (fp, where, (int) whence);
203 }
204 lasterr = EBADF;
205 return -1;
206 }
207
208 int
209 io_stream_rsync::error ()
210 {
211 if (fp)
212 return ferror (fp);
213 return lasterr;
214 }
215
216 int
217 io_stream_rsync::set_mtime (int mtime)
218 {
219 if (!fname.size())
220 return 1;
221 if (fp)
222 fclose (fp);
223 #if defined(WIN32) && !defined (_CYGWIN_)
224 long long ftimev = mtime * NSPERSEC + FACTOR;
225 FILETIME ftime;
226 ftime.dwHighDateTime = ftimev >> 32;
227 ftime.dwLowDateTime = ftimev;
228 HANDLE h =
229 CreateFileA (fname.cstr_oneuse(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
230 0, OPEN_EXISTING,
231 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0);
232 if (h)
233 {
234 SetFileTime (h, 0, 0, &ftime);
235 CloseHandle (h);
236 return 0;
237 }
238 #else
239 throw new runtime_error ("set_mtime not supported on posix yet.");
240 #endif
241 return 1;
242 }
243
244 int
245 io_stream_rsync::move (String const &from, String const &to)
246 {
247 if (!from.size()|| !to.size())
248 return 1;
249 return rename (from.cstr_oneuse(), to.cstr_oneuse());
250 }
251
252 size_t
253 io_stream_rsync::get_size ()
254 {
255 if (!fname.size())
256 return 0;
257 #if defined(WIN32) && !defined (_CYGWIN_)
258 HANDLE h;
259 WIN32_FIND_DATA buf;
260 DWORD ret = 0;
261
262 h = FindFirstFileA (fname.cstr_oneuse(), &buf);
263 if (h != INVALID_HANDLE_VALUE)
264 {
265 if (buf.nFileSizeHigh == 0)
266 ret = buf.nFileSizeLow;
267 FindClose (h);
268 }
269 return ret;
270 #else
271 struct stat buf;
272 if (stat(fname.cstr_oneuse(), &buf))
273 throw new runtime_error ("Failed to stat file - has it been deleted?");
274 return buf.st_size;
275 #endif
276 }
This page took 0.049572 seconds and 5 git commands to generate.