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 (String
const &name
, String
const &mode
):
40 fp(), fname(name
),lmode (mode
)
43 if (!name
.size() || !mode
.size())
45 fp
= fopen (name
.cstr_oneuse(), mode
.cstr_oneuse());
50 io_stream_file::~io_stream_file ()
58 io_stream_file::exists (String
const &path
)
60 if (_access (path
.cstr_oneuse(), 0) == 0)
66 io_stream_file::remove (String
const &path
)
71 unsigned long w
= GetFileAttributes (path
.cstr_oneuse());
72 if (w
!= 0xffffffff && w
& FILE_ATTRIBUTE_DIRECTORY
)
74 char *tmp
= new char [path
.size() + 10];
79 sprintf (tmp
, "%s.old-%d", path
.cstr_oneuse(), i
);
81 while (GetFileAttributes (tmp
) != 0xffffffff);
82 fprintf (stderr
, "warning: moving directory \"%s\" out of the way.\n",
84 MoveFile (path
.cstr_oneuse(), tmp
);
87 return !DeleteFileA (path
.cstr_oneuse());
92 io_stream_file::mklink (String
const &from
, String
const &to
,
93 io_stream_link_t linktype
)
95 if (!from
.size() || !to
.size())
99 case IO_STREAM_SYMLINK
:
100 return mkcygsymlink (from
.cstr_oneuse(), to
.cstr_oneuse());
101 case IO_STREAM_HARDLINK
:
111 io_stream_file::read (void *buffer
, size_t len
)
114 return fread (buffer
, 1, len
, fp
);
119 io_stream_file::write (const void *buffer
, size_t len
)
122 return fwrite (buffer
, 1, len
, fp
);
127 io_stream_file::peek (void *buffer
, size_t len
)
129 log (LOG_TIMESTAMP
, "io_stream_file::peek called");
132 int pos
= ftell (fp
);
133 ssize_t rv
= fread (buffer
, 1, len
, fp
);
134 fseek (fp
, pos
, SEEK_SET
);
141 io_stream_file::tell ()
151 io_stream_file::seek (long where
, io_stream_seek_t whence
)
155 return fseek (fp
, where
, (int) whence
);
162 io_stream_file::error ()
170 io_stream_file::set_mtime (int mtime
)
176 long long ftimev
= mtime
* NSPERSEC
+ FACTOR
;
178 ftime
.dwHighDateTime
= ftimev
>> 32;
179 ftime
.dwLowDateTime
= ftimev
;
181 CreateFileA (fname
.cstr_oneuse(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
183 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
186 SetFileTime (h
, 0, 0, &ftime
);
194 io_stream_file::move (String
const &from
, String
const &to
)
196 if (!from
.size()|| !to
.size())
198 return rename (from
.cstr_oneuse(), to
.cstr_oneuse());
202 io_stream_file::get_size ()
206 return get_file_size (fname
);