+
+#include "findlib/find.h"
+
+int
+utime(const char *fname, struct utimbuf *times)
+{
+ FILETIME acc, mod;
+ char tmpbuf[1024];
+
+ cygwin_conv_to_win32_path(fname, tmpbuf, 1024);
+
+ cvt_utime_to_ftime(times->actime, acc);
+ cvt_utime_to_ftime(times->modtime, mod);
+
+ HANDLE h = INVALID_HANDLE_VALUE;
+
+ if (p_CreateFileW) {
+ WCHAR szBuf[MAX_PATH_UNICODE];
+ UTF8_2_wchar(szBuf, tmpbuf, MAX_PATH_UNICODE);
+
+ h = p_CreateFileW(szBuf,
+ FILE_WRITE_ATTRIBUTES,
+ FILE_SHARE_WRITE,
+ NULL,
+ OPEN_EXISTING,
+ 0,
+ NULL);
+ } else if (p_CreateFileA) {
+ h = p_CreateFileA(tmpbuf,
+ FILE_WRITE_ATTRIBUTES,
+ FILE_SHARE_WRITE,
+ NULL,
+ OPEN_EXISTING,
+ 0,
+ NULL);
+ }
+
+ if (h == INVALID_HANDLE_VALUE) {
+ const char *err = errorString();
+ d_msg(__FILE__, __LINE__, 99,
+ "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err);
+ LocalFree((void *)err);
+ errno = b_errno_win32;
+ return -1;
+ }
+
+ int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
+ CloseHandle(h);
+ if (rval == -1) {
+ errno = b_errno_win32;
+ }
+ return rval;
+}
+
+#if USE_WIN32_COMPAT_IO
+
+int
+open(const char *file, int flags, int mode)
+{
+ if (p_wopen) {
+ WCHAR szBuf[MAX_PATH_UNICODE];
+ UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
+
+ return p_wopen(szBuf, flags|_O_BINARY, mode);
+ }
+
+ return _open(file, flags|_O_BINARY, mode);
+}
+
+/*
+ * Note, this works only for a file. If you want
+ * to close a socket, use closesocket().
+ * Bacula has been modified in src/lib/bnet.c
+ * to use closesocket().
+ */
+int
+close(int fd)
+{
+ return _close(fd);
+}
+
+#ifndef HAVE_WXCONSOLE
+ssize_t
+read(int fd, void *buf, ssize_t len)
+{
+ return _read(fd, buf, (size_t)len);
+}
+
+ssize_t
+write(int fd, const void *buf, ssize_t len)
+{
+ return _write(fd, buf, (size_t)len);
+}
+#endif
+
+
+off_t
+lseek(int fd, off_t offset, int whence)
+{
+ return _lseeki64(fd, offset, whence);
+}
+
+int
+dup2(int fd1, int fd2)
+{
+ return _dup2(fd1, fd2);
+}
+#else
+int
+open(const char *file, int flags, int mode)
+{
+ DWORD access = 0;
+ DWORD shareMode = 0;
+ DWORD create = 0;
+ DWORD msflags = 0;
+ HANDLE foo = INVALID_HANDLE_VALUE;
+ const char *remap = file;
+
+ if (flags & O_WRONLY) access = GENERIC_WRITE;
+ else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
+ else access = GENERIC_READ;
+
+ if (flags & O_CREAT) create = CREATE_NEW;
+ else create = OPEN_EXISTING;
+
+ if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
+
+ if (!(flags & O_EXCL))
+ shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
+
+ if (flags & O_APPEND) {
+ printf("open...APPEND not implemented yet.");
+ exit(-1);
+ }
+
+ if (p_CreateFileW) {
+ WCHAR szBuf[MAX_PATH_UNICODE];
+ UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
+
+ foo = p_CreateFileW(szBuf, access, shareMode, NULL, create, msflags, NULL);
+ }
+ else if (p_CreateFileA)
+ foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
+
+ if (INVALID_HANDLE_VALUE == foo) {
+ errno = b_errno_win32;
+ return(int) -1;
+ }
+ return (int)foo;
+
+}
+
+
+int
+close(int fd)
+{
+ if (!CloseHandle((HANDLE)fd)) {
+ errno = b_errno_win32;
+ return -1;
+ }
+
+ return 0;
+}
+
+ssize_t
+write(int fd, const void *data, ssize_t len)
+{
+ BOOL status;
+ DWORD bwrite;
+ status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
+ if (status) return bwrite;
+ errno = b_errno_win32;
+ return -1;
+}
+
+
+ssize_t
+read(int fd, void *data, ssize_t len)
+{
+ BOOL status;
+ DWORD bread;
+
+ status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
+ if (status) return bread;
+ errno = b_errno_win32;
+ return -1;
+}
+
+off_t
+lseek(int fd, off_t offset, int whence)
+{
+ DWORD method = 0;
+ DWORD val;
+ switch (whence) {
+ case SEEK_SET :
+ method = FILE_BEGIN;
+ break;
+ case SEEK_CUR:
+ method = FILE_CURRENT;
+ break;
+ case SEEK_END:
+ method = FILE_END;
+ break;
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+
+ if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) {
+ errno = b_errno_win32;
+ return -1;
+ }
+ /* ***FIXME*** I doubt this works right */
+ return val;
+}
+
+int
+dup2(int, int)
+{
+ errno = ENOSYS;
+ return -1;
+}
+
+
+#endif
+
+#endif //HAVE_MINGW
+
+#ifdef HAVE_MINGW
+/* syslog function, added by Nicolas Boichat */
+void closelog() {}
+#endif //HAVE_MINGW