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 (const char *name
, const char *mode
)
41 if (!name
|| IsBadStringPtr (name
, MAX_PATH
) || !name
[0] ||
42 !mode
|| IsBadStringPtr (mode
, 5) || !mode
[0])
44 lmode
= strdup (mode
);
45 fname
= strdup (name
);
46 fp
= fopen (name
, mode
);
51 io_stream_file::~io_stream_file ()
60 io_stream_file::exists (const char *path
)
62 if (_access (path
, 0) == 0)
68 io_stream_file::remove (const char *path
)
73 unsigned long w
= GetFileAttributes (path
);
74 if (w
!= 0xffffffff && w
& FILE_ATTRIBUTE_DIRECTORY
)
76 char *tmp
= (char *) malloc (strlen (path
) + 10);
81 sprintf (tmp
, "%s.old-%d", path
, i
);
83 while (GetFileAttributes (tmp
) != 0xffffffff);
84 fprintf (stderr
, "warning: moving directory \"%s\" out of the way.\n",
89 return !DeleteFileA (path
);
94 io_stream_file::mklink (const char *from
, const char *to
,
95 io_stream_link_t linktype
)
97 /* FIXME: badstring check */
102 case IO_STREAM_SYMLINK
:
103 return mkcygsymlink (from
, to
);
104 case IO_STREAM_HARDLINK
:
113 ssize_t
io_stream_file::read (void *buffer
, size_t len
)
116 return fread (buffer
, 1, len
, fp
);
120 ssize_t
io_stream_file::write (void *buffer
, size_t len
)
123 return fwrite (buffer
, 1, len
, fp
);
127 ssize_t
io_stream_file::peek (void *buffer
, size_t len
)
129 log (LOG_TIMESTAMP
, "io_stream_file::peek called");
135 rv
= fread (buffer
, 1, len
, fp
);
136 fseek (fp
, pos
, SEEK_SET
);
143 io_stream_file::tell ()
153 io_stream_file::error ()
161 io_stream_file::set_mtime (int mtime
)
167 long long ftimev
= mtime
* NSPERSEC
+ FACTOR
;
169 ftime
.dwHighDateTime
= ftimev
>> 32;
170 ftime
.dwLowDateTime
= ftimev
;
172 CreateFileA (fname
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
174 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
177 SetFileTime (h
, 0, 0, &ftime
);
179 fp
= fopen (fname
, lmode
);
184 fp
= fopen (fname
, lmode
);