]>
Commit | Line | Data |
---|---|---|
b24c88b3 RC |
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 | ||
2b48ecd0 | 21 | #if defined(WIN32) && !defined (_CYGWIN_) |
b24c88b3 | 22 | #include "win32.h" |
2b48ecd0 RC |
23 | #include "port.h" |
24 | #include "mkdir.h" | |
25 | #include "mklink2.h" | |
26 | #endif | |
27 | ||
b24c88b3 RC |
28 | #include <stdio.h> |
29 | #include <stdlib.h> | |
ca9506cc | 30 | #include <errno.h> |
b24c88b3 | 31 | #include <unistd.h> |
b24c88b3 | 32 | |
b24c88b3 | 33 | #include "io_stream_file.h" |
19911586 RC |
34 | #include "IOStreamProvider.h" |
35 | ||
36 | /* completely private iostream registration class */ | |
37 | class FileProvider : public IOStreamProvider | |
38 | { | |
39 | public: | |
40 | int exists (String const &path) const | |
41 | {return io_stream_file::exists(path);} | |
42 | int remove (String const &path) const | |
43 | {return io_stream_file::remove(path);} | |
44 | int mklink (String const &a , String const &b, io_stream_link_t c) const | |
45 | {return io_stream_file::mklink(a,b,c);} | |
46 | io_stream *open (String const &a,String const &b) const | |
47 | {return new io_stream_file (a, b);} | |
48 | ~FileProvider (){} | |
49 | int move (String const &a,String const &b) const | |
50 | {return io_stream_file::move (a, b);} | |
51 | int mkdir_p (enum path_type_t isadir, String const &path) const | |
52 | {return ::mkdir_p (isadir == PATH_TO_DIR ? 1 : 0, path.cstr_oneuse());} | |
53 | protected: | |
54 | FileProvider() // no creating this | |
55 | { | |
56 | io_stream::registerProvider (theInstance, "file://"); | |
57 | } | |
58 | FileProvider(FileProvider const &); // no copying | |
59 | FileProvider &operator=(FileProvider const &); // no assignment | |
60 | private: | |
61 | static FileProvider theInstance; | |
62 | }; | |
63 | FileProvider FileProvider::theInstance = FileProvider(); | |
64 | ||
b24c88b3 RC |
65 | |
66 | /* for set_mtime */ | |
67 | #define FACTOR (0x19db1ded53ea710LL) | |
68 | #define NSPERSEC 10000000LL | |
69 | ||
3c054baf RC |
70 | io_stream_file::io_stream_file (String const &name, String const &mode): |
71 | fp(), fname(name),lmode (mode) | |
b24c88b3 | 72 | { |
b24c88b3 | 73 | errno = 0; |
3c054baf | 74 | if (!name.size() || !mode.size()) |
b24c88b3 | 75 | return; |
3c054baf | 76 | fp = fopen (name.cstr_oneuse(), mode.cstr_oneuse()); |
b24c88b3 RC |
77 | if (!fp) |
78 | lasterr = errno; | |
79 | } | |
80 | ||
81 | io_stream_file::~io_stream_file () | |
82 | { | |
b24c88b3 RC |
83 | if (fp) |
84 | fclose (fp); | |
e9440f0f | 85 | destroyed = 1; |
b24c88b3 RC |
86 | } |
87 | ||
88 | int | |
3c054baf | 89 | io_stream_file::exists (String const &path) |
b24c88b3 | 90 | { |
2b48ecd0 | 91 | #if defined(WIN32) && !defined (_CYGWIN_) |
3c054baf | 92 | if (_access (path.cstr_oneuse(), 0) == 0) |
2b48ecd0 RC |
93 | #else |
94 | if (access (path.cstr_oneuse(), F_OK) == 0) | |
95 | #endif | |
b24c88b3 RC |
96 | return 1; |
97 | return 0; | |
98 | } | |
99 | ||
100 | int | |
3c054baf | 101 | io_stream_file::remove (String const &path) |
b24c88b3 | 102 | { |
3c054baf | 103 | if (!path.size()) |
b24c88b3 | 104 | return 1; |
2b48ecd0 | 105 | #if defined(WIN32) && !defined (_CYGWIN_) |
3c054baf | 106 | unsigned long w = GetFileAttributes (path.cstr_oneuse()); |
b24c88b3 RC |
107 | if (w != 0xffffffff && w & FILE_ATTRIBUTE_DIRECTORY) |
108 | { | |
3c054baf | 109 | char *tmp = new char [path.size() + 10]; |
b24c88b3 RC |
110 | int i = 0; |
111 | do | |
112 | { | |
113 | i++; | |
3c054baf | 114 | sprintf (tmp, "%s.old-%d", path.cstr_oneuse(), i); |
b24c88b3 RC |
115 | } |
116 | while (GetFileAttributes (tmp) != 0xffffffff); | |
117 | fprintf (stderr, "warning: moving directory \"%s\" out of the way.\n", | |
3c054baf RC |
118 | path.cstr_oneuse()); |
119 | MoveFile (path.cstr_oneuse(), tmp); | |
5e0464a1 | 120 | delete[] tmp; |
b24c88b3 | 121 | } |
3c054baf | 122 | return !DeleteFileA (path.cstr_oneuse()); |
2b48ecd0 RC |
123 | #else |
124 | // FIXME: try rmdir if unlink fails - remove the dir | |
125 | return unlink (path.cstr_oneuse()); | |
126 | #endif | |
b24c88b3 RC |
127 | } |
128 | ||
129 | int | |
3c054baf | 130 | io_stream_file::mklink (String const &from, String const &to, |
b24c88b3 RC |
131 | io_stream_link_t linktype) |
132 | { | |
3c054baf | 133 | if (!from.size() || !to.size()) |
b24c88b3 | 134 | return 1; |
2b48ecd0 | 135 | #if defined(WIN32) && !defined (_CYGWIN_) |
b24c88b3 RC |
136 | switch (linktype) |
137 | { | |
138 | case IO_STREAM_SYMLINK: | |
3c054baf | 139 | return mkcygsymlink (from.cstr_oneuse(), to.cstr_oneuse()); |
b24c88b3 RC |
140 | case IO_STREAM_HARDLINK: |
141 | return 1; | |
142 | } | |
2b48ecd0 RC |
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 | |
b24c88b3 RC |
152 | return 1; |
153 | } | |
154 | ||
155 | /* virtuals */ | |
156 | ||
157 | ||
7c7034e8 RC |
158 | ssize_t |
159 | io_stream_file::read (void *buffer, size_t len) | |
b24c88b3 RC |
160 | { |
161 | if (fp) | |
162 | return fread (buffer, 1, len, fp); | |
163 | return 0; | |
164 | } | |
165 | ||
7c7034e8 RC |
166 | ssize_t |
167 | io_stream_file::write (const void *buffer, size_t len) | |
b24c88b3 RC |
168 | { |
169 | if (fp) | |
170 | return fwrite (buffer, 1, len, fp); | |
171 | return 0; | |
172 | } | |
173 | ||
7c7034e8 RC |
174 | ssize_t |
175 | io_stream_file::peek (void *buffer, size_t len) | |
b24c88b3 | 176 | { |
b24c88b3 RC |
177 | if (fp) |
178 | { | |
7c7034e8 RC |
179 | int pos = ftell (fp); |
180 | ssize_t rv = fread (buffer, 1, len, fp); | |
b24c88b3 RC |
181 | fseek (fp, pos, SEEK_SET); |
182 | return rv; | |
183 | } | |
184 | return 0; | |
185 | } | |
186 | ||
187 | long | |
188 | io_stream_file::tell () | |
189 | { | |
190 | if (fp) | |
191 | { | |
192 | return ftell (fp); | |
193 | } | |
194 | return 0; | |
195 | } | |
196 | ||
ca9506cc RC |
197 | int |
198 | io_stream_file::seek (long where, io_stream_seek_t whence) | |
199 | { | |
7c7034e8 RC |
200 | if (fp) |
201 | { | |
202 | return fseek (fp, where, (int) whence); | |
203 | } | |
204 | lasterr = EBADF; | |
205 | return -1; | |
ca9506cc RC |
206 | } |
207 | ||
b24c88b3 RC |
208 | int |
209 | io_stream_file::error () | |
210 | { | |
211 | if (fp) | |
212 | return ferror (fp); | |
213 | return lasterr; | |
214 | } | |
215 | ||
216 | int | |
217 | io_stream_file::set_mtime (int mtime) | |
218 | { | |
3c054baf | 219 | if (!fname.size()) |
b24c88b3 RC |
220 | return 1; |
221 | if (fp) | |
222 | fclose (fp); | |
2b48ecd0 | 223 | #if defined(WIN32) && !defined (_CYGWIN_) |
b24c88b3 RC |
224 | long long ftimev = mtime * NSPERSEC + FACTOR; |
225 | FILETIME ftime; | |
226 | ftime.dwHighDateTime = ftimev >> 32; | |
227 | ftime.dwLowDateTime = ftimev; | |
228 | HANDLE h = | |
3c054baf | 229 | CreateFileA (fname.cstr_oneuse(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, |
b24c88b3 RC |
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); | |
b24c88b3 RC |
236 | return 0; |
237 | } | |
2b48ecd0 RC |
238 | #else |
239 | throw new runtime_error ("set_mtime not supported on posix yet."); | |
240 | #endif | |
b24c88b3 RC |
241 | return 1; |
242 | } | |
7c7034e8 RC |
243 | |
244 | int | |
3c054baf | 245 | io_stream_file::move (String const &from, String const &to) |
7c7034e8 | 246 | { |
3c054baf | 247 | if (!from.size()|| !to.size()) |
7c7034e8 | 248 | return 1; |
3c054baf | 249 | return rename (from.cstr_oneuse(), to.cstr_oneuse()); |
7c7034e8 | 250 | } |
341988b9 RC |
251 | |
252 | size_t | |
253 | io_stream_file::get_size () | |
254 | { | |
3c054baf | 255 | if (!fname.size()) |
341988b9 | 256 | return 0; |
2b48ecd0 | 257 | #if defined(WIN32) && !defined (_CYGWIN_) |
e0a4db64 RC |
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; | |
2b48ecd0 RC |
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 | |
341988b9 | 276 | } |