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
=
30 #include "io_stream.h"
31 #include "io_stream_file.h"
34 #define FACTOR (0x19db1ded53ea710LL)
35 #define NSPERSEC 10000000LL
37 io_stream_file::io_stream_file (const char *name
, const char *mode
)
42 if (!name
|| IsBadStringPtr (name
, MAX_PATH
) || !name
[0] ||
43 !mode
|| IsBadStringPtr (mode
, 5) || !mode
[0])
45 lmode
= strdup (mode
);
46 fname
= strdup (name
);
47 fp
= fopen (name
, mode
);
52 io_stream_file::~io_stream_file ()
61 io_stream_file::exists (const char *path
)
63 if (_access (path
, 0) == 0)
69 io_stream_file::remove (const char *path
)
74 unsigned long w
= GetFileAttributes (path
);
75 if (w
!= 0xffffffff && w
& FILE_ATTRIBUTE_DIRECTORY
)
77 char *tmp
= (char *) malloc (strlen (path
) + 10);
82 sprintf (tmp
, "%s.old-%d", path
, i
);
84 while (GetFileAttributes (tmp
) != 0xffffffff);
85 fprintf (stderr
, "warning: moving directory \"%s\" out of the way.\n",
90 return !DeleteFileA (path
);
95 io_stream_file::mklink (const char *from
, const char *to
,
96 io_stream_link_t linktype
)
98 /* FIXME: badstring check */
103 case IO_STREAM_SYMLINK
:
104 return mkcygsymlink (from
, to
);
105 case IO_STREAM_HARDLINK
:
114 ssize_t
io_stream_file::read (void *buffer
, size_t len
)
117 return fread (buffer
, 1, len
, fp
);
121 ssize_t
io_stream_file::write (const void *buffer
, size_t len
)
124 return fwrite (buffer
, 1, len
, fp
);
128 ssize_t
io_stream_file::peek (void *buffer
, size_t len
)
130 log (LOG_TIMESTAMP
, "io_stream_file::peek called");
136 rv
= fread (buffer
, 1, len
, fp
);
137 fseek (fp
, pos
, SEEK_SET
);
144 io_stream_file::tell ()
154 io_stream_file::seek (long where
, io_stream_seek_t whence
)
158 return fseek (fp
, where
, (int) whence
);
165 io_stream_file::error ()
173 io_stream_file::set_mtime (int mtime
)
179 long long ftimev
= mtime
* NSPERSEC
+ FACTOR
;
181 ftime
.dwHighDateTime
= ftimev
>> 32;
182 ftime
.dwLowDateTime
= ftimev
;
184 CreateFileA (fname
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
186 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
189 SetFileTime (h
, 0, 0, &ftime
);
191 fp
= fopen (fname
, lmode
);
196 fp
= fopen (fname
, lmode
);