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
:
115 io_stream_file::read (void *buffer
, size_t len
)
118 return fread (buffer
, 1, len
, fp
);
123 io_stream_file::write (const void *buffer
, size_t len
)
126 return fwrite (buffer
, 1, len
, fp
);
131 io_stream_file::peek (void *buffer
, size_t len
)
133 log (LOG_TIMESTAMP
, "io_stream_file::peek called");
136 int pos
= ftell (fp
);
137 ssize_t rv
= fread (buffer
, 1, len
, fp
);
138 fseek (fp
, pos
, SEEK_SET
);
145 io_stream_file::tell ()
155 io_stream_file::seek (long where
, io_stream_seek_t whence
)
159 return fseek (fp
, where
, (int) whence
);
166 io_stream_file::error ()
174 io_stream_file::set_mtime (int mtime
)
180 long long ftimev
= mtime
* NSPERSEC
+ FACTOR
;
182 ftime
.dwHighDateTime
= ftimev
>> 32;
183 ftime
.dwLowDateTime
= ftimev
;
185 CreateFileA (fname
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
187 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
190 SetFileTime (h
, 0, 0, &ftime
);
198 io_stream_file::move (char const *from
, char const *to
)
200 if (!from
|| IsBadStringPtr (from
, MAX_PATH
) || !from
[0] ||
201 !to
|| IsBadStringPtr (to
, MAX_PATH
) || !to
[0])
203 return rename (from
, to
);