Index: PC/_msi.c =================================================================== --- PC/_msi.c (revision 85121) +++ PC/_msi.c (working copy) @@ -55,49 +55,141 @@ static FNFCIOPEN(cb_open) { - int result = _open(pszFile, oflag, pmode); - if (result == -1) - *err = errno; - return result; + HANDLE handle = NULL; + DWORD desired_access = 0; + DWORD disposition = 0; + DWORD share_mode = 0; + DWORD flags = 0; + int tmp_flag; + + /* file access */ + if (oflag & _O_RDWR) { + desired_access = GENERIC_READ | GENERIC_WRITE; + } + else if (oflag & _O_WRONLY) { + desired_access = GENERIC_WRITE; + } + else { + desired_access = GENERIC_READ; + } + + /* creation options */ + tmp_flag = oflag & (_O_CREAT | _O_TRUNC | _O_EXCL); + switch (tmp_flag) { + case _O_CREAT: + disposition = OPEN_ALWAYS; + break; + case _O_TRUNC: + case _O_TRUNC | _O_EXCL: + disposition = TRUNCATE_EXISTING; + break; + case _O_CREAT | _O_TRUNC: + disposition = CREATE_ALWAYS; + break; + case _O_CREAT | _O_EXCL: + case _O_CREAT | _O_EXCL | _O_TRUNC: + disposition = CREATE_NEW; + break; + default: + disposition = OPEN_EXISTING; + break; + } + + share_mode = FILE_SHARE_READ; + + /* flags and attributes */ + tmp_flag = oflag & (_O_CREAT | _O_SHORT_LIVED | _O_TEMPORARY); + switch (tmp_flag) { + case _O_CREAT | _O_SHORT_LIVED: + flags = FILE_ATTRIBUTE_TEMPORARY; + break; + case _O_CREAT | _O_TEMPORARY: + flags = FILE_FLAG_DELETE_ON_CLOSE; + desired_access |= DELETE; + share_mode |= FILE_SHARE_DELETE; + break; + default: + flags = FILE_ATTRIBUTE_NORMAL; + break; + } + + if (oflag & _O_SEQUENTIAL) + flags |= FILE_FLAG_SEQUENTIAL_SCAN; + else if (oflag & _O_RANDOM) + flags |= FILE_FLAG_RANDOM_ACCESS; + + handle = CreateFileA(pszFile, + desired_access, + share_mode, + NULL, + disposition, + flags, + NULL); + + if (handle == INVALID_HANDLE_VALUE) { + *err = GetLastError(); + } + + return (INT_PTR)handle; } static FNFCIREAD(cb_read) { - UINT result = (UINT)_read(hf, memory, cb); - if (result != cb) - *err = errno; - return result; + DWORD bytes_read = 0; + + if (ReadFile((HANDLE)hf, memory, cb, &bytes_read, NULL) == FALSE) { + bytes_read = (DWORD)-1; + *err = GetLastError(); + } + + return bytes_read; } static FNFCIWRITE(cb_write) { - UINT result = (UINT)_write(hf, memory, cb); - if (result != cb) - *err = errno; - return result; + DWORD bytes_written = 0; + + if (WriteFile((HANDLE)hf, memory, cb, &bytes_written, NULL) == FALSE) { + bytes_written = (DWORD)-1; + *err = GetLastError(); + } + + return bytes_written; } static FNFCICLOSE(cb_close) { - int result = _close(hf); - if (result != 0) - *err = errno; + int result = 0; + + if (CloseHandle((HANDLE)hf) == FALSE) { + *err = GetLastError(); + result = -1; + } + return result; } static FNFCISEEK(cb_seek) { - long result = (long)_lseek(hf, dist, seektype); - if (result == -1) - *err = errno; + int result = 0; + + result = SetFilePointer((HANDLE)hf, dist, NULL, seektype); + if (result == -1) { + *err = GetLastError(); + } + return result; } static FNFCIDELETE(cb_delete) { - int result = remove(pszFile); - if (result != 0) - *err = errno; + int result = 0; + + if (DeleteFileA(pszFile) == FALSE) { + *err = GetLastError(); + result = -1; + } + return result; } @@ -108,14 +200,17 @@ static FNFCIGETTEMPFILE(cb_gettempfile) { - char *name = _tempnam("", "tmp"); - if ((name != NULL) && ((int)strlen(name) < cbTempName)) { - strcpy(pszTempName, name); - free(name); - return TRUE; + char temp_path[MAX_PATH]; + + if (GetTempPathA(MAX_PATH, temp_path) != 0) { + /* pass any non-zero value for uUnique to suppress file creation */ + if (GetTempFileNameA(temp_path, "tmp", 1, temp_path) != 0 ) { + if (strlen(temp_path) < (size_t)cbTempName) { + strcpy(pszTempName, temp_path); + return TRUE; + } + } } - - if (name) free(name); return FALSE; } @@ -156,13 +251,12 @@ HANDLE handle; /* Need Win32 handle to get time stamps */ - handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + handle = (HANDLE)cb_open(pszName, _O_RDONLY, 0, err, pv); if (handle == INVALID_HANDLE_VALUE) return -1; - if (GetFileInformationByHandle(handle, &bhfi) == FALSE) - { + if (GetFileInformationByHandle(handle, &bhfi) == FALSE) { + *err = GetLastError(); CloseHandle(handle); return -1; } @@ -173,9 +267,7 @@ *pattribs = (int)(bhfi.dwFileAttributes & (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH)); - CloseHandle(handle); - - return _open(pszName, _O_RDONLY | _O_BINARY); + return (INT_PTR)handle; } static PyObject* fcicreate(PyObject* obj, PyObject* args)