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