]>
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 | ||
21 | #include "win32.h" | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
ca9506cc | 24 | #include <errno.h> |
b24c88b3 | 25 | #include <unistd.h> |
b24c88b3 RC |
26 | #include "port.h" |
27 | #include "mklink2.h" | |
28 | ||
341988b9 RC |
29 | #include "filemanip.h" |
30 | ||
b24c88b3 RC |
31 | #include "io_stream.h" |
32 | #include "io_stream_file.h" | |
33 | ||
34 | /* for set_mtime */ | |
35 | #define FACTOR (0x19db1ded53ea710LL) | |
36 | #define NSPERSEC 10000000LL | |
37 | ||
3c054baf RC |
38 | io_stream_file::io_stream_file (String const &name, String const &mode): |
39 | fp(), fname(name),lmode (mode) | |
b24c88b3 | 40 | { |
b24c88b3 | 41 | errno = 0; |
3c054baf | 42 | if (!name.size() || !mode.size()) |
b24c88b3 | 43 | return; |
3c054baf | 44 | fp = fopen (name.cstr_oneuse(), mode.cstr_oneuse()); |
b24c88b3 RC |
45 | if (!fp) |
46 | lasterr = errno; | |
47 | } | |
48 | ||
49 | io_stream_file::~io_stream_file () | |
50 | { | |
b24c88b3 RC |
51 | if (fp) |
52 | fclose (fp); | |
e9440f0f | 53 | destroyed = 1; |
b24c88b3 RC |
54 | } |
55 | ||
56 | int | |
3c054baf | 57 | io_stream_file::exists (String const &path) |
b24c88b3 | 58 | { |
3c054baf | 59 | if (_access (path.cstr_oneuse(), 0) == 0) |
b24c88b3 RC |
60 | return 1; |
61 | return 0; | |
62 | } | |
63 | ||
64 | int | |
3c054baf | 65 | io_stream_file::remove (String const &path) |
b24c88b3 | 66 | { |
3c054baf | 67 | if (!path.size()) |
b24c88b3 RC |
68 | return 1; |
69 | ||
3c054baf | 70 | unsigned long w = GetFileAttributes (path.cstr_oneuse()); |
b24c88b3 RC |
71 | if (w != 0xffffffff && w & FILE_ATTRIBUTE_DIRECTORY) |
72 | { | |
3c054baf | 73 | char *tmp = new char [path.size() + 10]; |
b24c88b3 RC |
74 | int i = 0; |
75 | do | |
76 | { | |
77 | i++; | |
3c054baf | 78 | sprintf (tmp, "%s.old-%d", path.cstr_oneuse(), i); |
b24c88b3 RC |
79 | } |
80 | while (GetFileAttributes (tmp) != 0xffffffff); | |
81 | fprintf (stderr, "warning: moving directory \"%s\" out of the way.\n", | |
3c054baf RC |
82 | path.cstr_oneuse()); |
83 | MoveFile (path.cstr_oneuse(), tmp); | |
5e0464a1 | 84 | delete[] tmp; |
b24c88b3 | 85 | } |
3c054baf | 86 | return !DeleteFileA (path.cstr_oneuse()); |
b24c88b3 RC |
87 | |
88 | } | |
89 | ||
90 | int | |
3c054baf | 91 | io_stream_file::mklink (String const &from, String const &to, |
b24c88b3 RC |
92 | io_stream_link_t linktype) |
93 | { | |
3c054baf | 94 | if (!from.size() || !to.size()) |
b24c88b3 RC |
95 | return 1; |
96 | switch (linktype) | |
97 | { | |
98 | case IO_STREAM_SYMLINK: | |
3c054baf | 99 | return mkcygsymlink (from.cstr_oneuse(), to.cstr_oneuse()); |
b24c88b3 RC |
100 | case IO_STREAM_HARDLINK: |
101 | return 1; | |
102 | } | |
103 | return 1; | |
104 | } | |
105 | ||
106 | /* virtuals */ | |
107 | ||
108 | ||
7c7034e8 RC |
109 | ssize_t |
110 | io_stream_file::read (void *buffer, size_t len) | |
b24c88b3 RC |
111 | { |
112 | if (fp) | |
113 | return fread (buffer, 1, len, fp); | |
114 | return 0; | |
115 | } | |
116 | ||
7c7034e8 RC |
117 | ssize_t |
118 | io_stream_file::write (const void *buffer, size_t len) | |
b24c88b3 RC |
119 | { |
120 | if (fp) | |
121 | return fwrite (buffer, 1, len, fp); | |
122 | return 0; | |
123 | } | |
124 | ||
7c7034e8 RC |
125 | ssize_t |
126 | io_stream_file::peek (void *buffer, size_t len) | |
b24c88b3 | 127 | { |
b24c88b3 RC |
128 | if (fp) |
129 | { | |
7c7034e8 RC |
130 | int pos = ftell (fp); |
131 | ssize_t rv = fread (buffer, 1, len, fp); | |
b24c88b3 RC |
132 | fseek (fp, pos, SEEK_SET); |
133 | return rv; | |
134 | } | |
135 | return 0; | |
136 | } | |
137 | ||
138 | long | |
139 | io_stream_file::tell () | |
140 | { | |
141 | if (fp) | |
142 | { | |
143 | return ftell (fp); | |
144 | } | |
145 | return 0; | |
146 | } | |
147 | ||
ca9506cc RC |
148 | int |
149 | io_stream_file::seek (long where, io_stream_seek_t whence) | |
150 | { | |
7c7034e8 RC |
151 | if (fp) |
152 | { | |
153 | return fseek (fp, where, (int) whence); | |
154 | } | |
155 | lasterr = EBADF; | |
156 | return -1; | |
ca9506cc RC |
157 | } |
158 | ||
b24c88b3 RC |
159 | int |
160 | io_stream_file::error () | |
161 | { | |
162 | if (fp) | |
163 | return ferror (fp); | |
164 | return lasterr; | |
165 | } | |
166 | ||
167 | int | |
168 | io_stream_file::set_mtime (int mtime) | |
169 | { | |
3c054baf | 170 | if (!fname.size()) |
b24c88b3 RC |
171 | return 1; |
172 | if (fp) | |
173 | fclose (fp); | |
174 | long long ftimev = mtime * NSPERSEC + FACTOR; | |
175 | FILETIME ftime; | |
176 | ftime.dwHighDateTime = ftimev >> 32; | |
177 | ftime.dwLowDateTime = ftimev; | |
178 | HANDLE h = | |
3c054baf | 179 | CreateFileA (fname.cstr_oneuse(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, |
b24c88b3 RC |
180 | 0, OPEN_EXISTING, |
181 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0); | |
182 | if (h) | |
183 | { | |
184 | SetFileTime (h, 0, 0, &ftime); | |
185 | CloseHandle (h); | |
b24c88b3 RC |
186 | return 0; |
187 | } | |
b24c88b3 RC |
188 | return 1; |
189 | } | |
7c7034e8 RC |
190 | |
191 | int | |
3c054baf | 192 | io_stream_file::move (String const &from, String const &to) |
7c7034e8 | 193 | { |
3c054baf | 194 | if (!from.size()|| !to.size()) |
7c7034e8 | 195 | return 1; |
3c054baf | 196 | return rename (from.cstr_oneuse(), to.cstr_oneuse()); |
7c7034e8 | 197 | } |
341988b9 RC |
198 | |
199 | size_t | |
200 | io_stream_file::get_size () | |
201 | { | |
3c054baf | 202 | if (!fname.size()) |
341988b9 RC |
203 | return 0; |
204 | return get_file_size (fname); | |
205 | } |