]> cygwin.com Git - cygwin-apps/setup.git/blame - io_stream_cygfile.cc
2002-01-21 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / io_stream_cygfile.cc
CommitLineData
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
17static const char *cvsid =
18 "\n%%% $Id$\n";
19#endif
20
21
22#include "win32.h"
23#include <stdio.h>
24#include <stdlib.h>
ca9506cc 25#include <errno.h>
b24c88b3
RC
26#include "log.h"
27#include "port.h"
28#include "mount.h"
29#include "mkdir.h"
30#include "mklink2.h"
31#include <unistd.h>
341988b9 32#include "filemanip.h"
b24c88b3
RC
33
34#include "io_stream.h"
35#include "io_stream_cygfile.h"
36
37/* For set mtime */
38#define FACTOR (0x19db1ded53ea710LL)
39#define NSPERSEC 10000000LL
40
41static void
42get_root_dir_now ()
43{
44 if (get_root_dir ())
45 return;
46 read_mounts ();
47}
48
49io_stream_cygfile::io_stream_cygfile (const char *name, const char *mode)
50{
51 fname = NULL;
52 fp = NULL;
53 errno = 0;
54 if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0] ||
55 !mode || IsBadStringPtr (mode, 5) || !mode[0])
56 return;
57
58 /* do this every time because the mount points may change due to fwd/back button use...
59 * TODO: make this less...manual
60 */
61 get_root_dir_now ();
62 if (!get_root_dir ())
63 /* TODO: assign a errno for "no mount table :} " */
64 return;
65
66 fname = cygpath (name, 0);
5e0464a1
RC
67 lmode = new char[strlen(mode) + 1];
68 strcpy (lmode,mode);
b24c88b3
RC
69 fp = fopen (fname, mode);
70 if (!fp)
71 lasterr = errno;
72}
73
74io_stream_cygfile::~io_stream_cygfile ()
75{
76 if (lmode)
5e0464a1 77 delete[] lmode;
b24c88b3 78 if (fname)
5e0464a1 79 delete[] fname;
b24c88b3
RC
80 if (fp)
81 fclose (fp);
82}
83
84/* Static members */
85int
86io_stream_cygfile::exists (const char *path)
87{
88 get_root_dir_now ();
89 if (get_root_dir () && _access (cygpath (path, 0), 0) == 0)
90 return 1;
91 return 0;
92}
93
94int
95io_stream_cygfile::remove (const char *path)
96{
97 if (!path)
98 return 1;
99 get_root_dir_now ();
100 if (!get_root_dir ())
101 /* TODO: assign a errno for "no mount table :} " */
102 return 1;
103
104 unsigned long w = GetFileAttributes (cygpath (path, 0));
105 if (w != 0xffffffff && w & FILE_ATTRIBUTE_DIRECTORY)
106 {
5e0464a1 107 char *tmp = new char [strlen (cygpath (path, 0)) + 10];
b24c88b3
RC
108 int i = 0;
109 do
110 {
111 i++;
112 sprintf (tmp, "%s.old-%d", cygpath (path, 0), i);
113 }
114 while (GetFileAttributes (tmp) != 0xffffffff);
115 fprintf (stderr, "warning: moving directory \"%s\" out of the way.\n",
116 path);
117 MoveFile (cygpath (path, 0), tmp);
5e0464a1 118 delete[] tmp;
b24c88b3
RC
119 }
120 return !DeleteFileA (cygpath (path, 0));
121}
122
123int
124io_stream_cygfile::mklink (const char *from, const char *to,
125 io_stream_link_t linktype)
126{
127 /* FIXME: badstring check */
128 if (!from || !to)
129 return 1;
130 switch (linktype)
131 {
132 case IO_STREAM_SYMLINK:
133 return mkcygsymlink (cygpath (from, 0), to);
134 case IO_STREAM_HARDLINK:
135 {
136 /* For now, just copy */
137 /* textmode alert: should we translate when linking from an binmode to a
138 text mode mount and vice verca?
139 */
140 io_stream *in = io_stream::open (cygpath (to, 0), "rb");
141 if (!in)
142 {
143 log (LOG_TIMESTAMP, "could not open %s for reading in mklink",
144 to);
145 return 1;
146 }
147 io_stream *out = io_stream::open (cygpath (from, 0), "wb");
148 if (!out)
149 {
150 log (LOG_TIMESTAMP, "could not open %s for writing in mklink",
151 from);
152 delete in;
153 return 1;
154 }
155
156 ssize_t len;
157 char buf[16384];
158 while ((len = in->read (buf, 16384)) > 0)
159 {
160 ssize_t wrote = out->write (buf, len);
161 if (wrote != len)
162 {
163 log (LOG_TIMESTAMP, "error writing to %s in mklink", from);
164 delete in;
165 delete out;
166 return 1;
167 }
168 }
169 delete in;
170 delete out;
171 if (len == 0)
172 return 0;
173 log (LOG_TIMESTAMP,
174 "read error reading %s while copying to %s (emulated hardlink)",
175 to, from);
176 return 1;
177 }
178 }
179 return 1;
180}
181
182
183/* virtuals */
184
7c7034e8
RC
185ssize_t
186io_stream_cygfile::read (void *buffer, size_t len)
b24c88b3
RC
187{
188 if (fp)
189 return fread (buffer, 1, len, fp);
190 return 0;
191}
192
7c7034e8
RC
193ssize_t
194io_stream_cygfile::write (const void *buffer, size_t len)
b24c88b3
RC
195{
196 if (fp)
197 return fwrite (buffer, 1, len, fp);
198 return 0;
199}
200
7c7034e8
RC
201ssize_t
202io_stream_cygfile::peek (void *buffer, size_t len)
b24c88b3
RC
203{
204 log (LOG_TIMESTAMP, "io_stream_cygfile::peek called");
205 if (fp)
206 {
7c7034e8
RC
207 int pos = ftell (fp);
208 ssize_t rv = fread (buffer, 1, len, fp);
b24c88b3
RC
209 fseek (fp, pos, SEEK_SET);
210 return rv;
211 }
212 return 0;
213}
214
215long
216io_stream_cygfile::tell ()
217{
218 if (fp)
219 {
220 return ftell (fp);
221 }
222 return 0;
223}
224
ca9506cc
RC
225int
226io_stream_cygfile::seek (long where, io_stream_seek_t whence)
227{
228 if (fp)
7c7034e8
RC
229 {
230 return fseek (fp, where, (int) whence);
231 }
ca9506cc
RC
232 lasterr = EBADF;
233 return -1;
234}
235
b24c88b3
RC
236int
237io_stream_cygfile::error ()
238{
239 if (fp)
240 return ferror (fp);
241 return lasterr;
242}
243
244int
245cygmkdir_p (int isadir, const char *name)
246{
247 if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0])
248 return 1;
249 get_root_dir_now ();
250 if (!get_root_dir ())
251 /* TODO: assign a errno for "no mount table :} " */
252 return 1;
253 return mkdir_p (isadir, cygpath (name, 0));
254}
255
b24c88b3
RC
256int
257io_stream_cygfile::set_mtime (int mtime)
258{
259 if (!fname)
260 return 1;
261 if (fp)
262 fclose (fp);
263 long long ftimev = mtime * NSPERSEC + FACTOR;
264 FILETIME ftime;
265 ftime.dwHighDateTime = ftimev >> 32;
266 ftime.dwLowDateTime = ftimev;
267 HANDLE h =
268 CreateFileA (fname, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
269 0, OPEN_EXISTING,
270 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0);
271 if (h)
272 {
273 SetFileTime (h, 0, 0, &ftime);
274 CloseHandle (h);
bb849dbd 275#if 0
b24c88b3
RC
276 fp = fopen (fname, lmode);
277 if (!fp)
278 lasterr = errno;
bb849dbd 279#endif
b24c88b3
RC
280 return 0;
281 }
bb849dbd
RC
282#if 0
283// this results in truncated files.
284// also, semantically, it's nonsense, you cannot write to a file after setting the
285// mtime without changing the mtime
b24c88b3
RC
286 fp = fopen (fname, lmode);
287 if (!fp)
288 lasterr = errno;
bb849dbd 289#endif
b24c88b3
RC
290 return 1;
291}
7c7034e8
RC
292
293int
294io_stream_cygfile::move (char const *from, char const *to)
295{
296 if (!from || IsBadStringPtr (from, MAX_PATH) || !from[0] ||
297 !to || IsBadStringPtr (to, MAX_PATH) || !to[0])
298 return 1;
299 get_root_dir_now ();
300 if (!get_root_dir ())
301 /* TODO: assign a errno for "no mount table :} " */
302 return 1;
303 return rename (cygpath (from, 0), cygpath (to, 0));
304}
341988b9
RC
305
306size_t
307io_stream_cygfile::get_size ()
308{
309 if (!fname)
310 return 0;
311 return get_file_size (fname);
312}
This page took 0.051165 seconds and 5 git commands to generate.