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