2 * Copyright (c) 2001, Robert Collins.
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.
9 * A copy of the GNU General Public License can be found at
12 * Written by Robert Collins <rbtcollins@hotmail.com>
17 static const char *cvsid
=
29 #include "io_stream.h"
30 #include "io_stream_file.h"
33 #define FACTOR (0x19db1ded53ea710LL)
34 #define NSPERSEC 10000000LL
36 io_stream_file::io_stream_file (String
const &name
, String
const &mode
):
37 fp(), fname(name
),lmode (mode
)
40 if (!name
.size() || !mode
.size())
42 fp
= fopen (name
.cstr_oneuse(), mode
.cstr_oneuse());
47 io_stream_file::~io_stream_file ()
55 io_stream_file::exists (String
const &path
)
57 if (_access (path
.cstr_oneuse(), 0) == 0)
63 io_stream_file::remove (String
const &path
)
68 unsigned long w
= GetFileAttributes (path
.cstr_oneuse());
69 if (w
!= 0xffffffff && w
& FILE_ATTRIBUTE_DIRECTORY
)
71 char *tmp
= new char [path
.size() + 10];
76 sprintf (tmp
, "%s.old-%d", path
.cstr_oneuse(), i
);
78 while (GetFileAttributes (tmp
) != 0xffffffff);
79 fprintf (stderr
, "warning: moving directory \"%s\" out of the way.\n",
81 MoveFile (path
.cstr_oneuse(), tmp
);
84 return !DeleteFileA (path
.cstr_oneuse());
89 io_stream_file::mklink (String
const &from
, String
const &to
,
90 io_stream_link_t linktype
)
92 if (!from
.size() || !to
.size())
96 case IO_STREAM_SYMLINK
:
97 return mkcygsymlink (from
.cstr_oneuse(), to
.cstr_oneuse());
98 case IO_STREAM_HARDLINK
:
108 io_stream_file::read (void *buffer
, size_t len
)
111 return fread (buffer
, 1, len
, fp
);
116 io_stream_file::write (const void *buffer
, size_t len
)
119 return fwrite (buffer
, 1, len
, fp
);
124 io_stream_file::peek (void *buffer
, size_t len
)
128 int pos
= ftell (fp
);
129 ssize_t rv
= fread (buffer
, 1, len
, fp
);
130 fseek (fp
, pos
, SEEK_SET
);
137 io_stream_file::tell ()
147 io_stream_file::seek (long where
, io_stream_seek_t whence
)
151 return fseek (fp
, where
, (int) whence
);
158 io_stream_file::error ()
166 io_stream_file::set_mtime (int mtime
)
172 long long ftimev
= mtime
* NSPERSEC
+ FACTOR
;
174 ftime
.dwHighDateTime
= ftimev
>> 32;
175 ftime
.dwLowDateTime
= ftimev
;
177 CreateFileA (fname
.cstr_oneuse(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
179 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
182 SetFileTime (h
, 0, 0, &ftime
);
190 io_stream_file::move (String
const &from
, String
const &to
)
192 if (!from
.size()|| !to
.size())
194 return rename (from
.cstr_oneuse(), to
.cstr_oneuse());
198 io_stream_file::get_size ()
206 h
= FindFirstFileA (fname
.cstr_oneuse(), &buf
);
207 if (h
!= INVALID_HANDLE_VALUE
)
209 if (buf
.nFileSizeHigh
== 0)
210 ret
= buf
.nFileSizeLow
;