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