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 "filemanip.h"
32 #include "io_stream.h"
33 #include "io_stream_file.h"
36 #define FACTOR (0x19db1ded53ea710LL)
37 #define NSPERSEC 10000000LL
39 io_stream_file::io_stream_file (const char *name
, const char *mode
)
44 if (!name
|| IsBadStringPtr (name
, MAX_PATH
) || !name
[0] ||
45 !mode
|| IsBadStringPtr (mode
, 5) || !mode
[0])
47 lmode
= strdup (mode
);
48 fname
= strdup (name
);
49 fp
= fopen (name
, mode
);
54 io_stream_file::~io_stream_file ()
63 io_stream_file::exists (const char *path
)
65 if (_access (path
, 0) == 0)
71 io_stream_file::remove (const char *path
)
76 unsigned long w
= GetFileAttributes (path
);
77 if (w
!= 0xffffffff && w
& FILE_ATTRIBUTE_DIRECTORY
)
79 char *tmp
= (char *) malloc (strlen (path
) + 10);
84 sprintf (tmp
, "%s.old-%d", path
, i
);
86 while (GetFileAttributes (tmp
) != 0xffffffff);
87 fprintf (stderr
, "warning: moving directory \"%s\" out of the way.\n",
92 return !DeleteFileA (path
);
97 io_stream_file::mklink (const char *from
, const char *to
,
98 io_stream_link_t linktype
)
100 /* FIXME: badstring check */
105 case IO_STREAM_SYMLINK
:
106 return mkcygsymlink (from
, to
);
107 case IO_STREAM_HARDLINK
:
117 io_stream_file::read (void *buffer
, size_t len
)
120 return fread (buffer
, 1, len
, fp
);
125 io_stream_file::write (const void *buffer
, size_t len
)
128 return fwrite (buffer
, 1, len
, fp
);
133 io_stream_file::peek (void *buffer
, size_t len
)
135 log (LOG_TIMESTAMP
, "io_stream_file::peek called");
138 int pos
= ftell (fp
);
139 ssize_t rv
= fread (buffer
, 1, len
, fp
);
140 fseek (fp
, pos
, SEEK_SET
);
147 io_stream_file::tell ()
157 io_stream_file::seek (long where
, io_stream_seek_t whence
)
161 return fseek (fp
, where
, (int) whence
);
168 io_stream_file::error ()
176 io_stream_file::set_mtime (int mtime
)
182 long long ftimev
= mtime
* NSPERSEC
+ FACTOR
;
184 ftime
.dwHighDateTime
= ftimev
>> 32;
185 ftime
.dwLowDateTime
= ftimev
;
187 CreateFileA (fname
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
189 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
192 SetFileTime (h
, 0, 0, &ftime
);
200 io_stream_file::move (char const *from
, char const *to
)
202 if (!from
|| IsBadStringPtr (from
, MAX_PATH
) || !from
[0] ||
203 !to
|| IsBadStringPtr (to
, MAX_PATH
) || !to
[0])
205 return rename (from
, to
);
209 io_stream_file::get_size ()
213 return get_file_size (fname
);