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