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