Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * File handling functions |
| 3 | * |
| 4 | * Copyright 1993 John Burton |
| 5 | * Copyright 1996 Alexandre Julliard |
| 6 | */ |
| 7 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 8 | #include <assert.h> |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <sys/errno.h> |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 16 | #include <sys/stat.h> |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 17 | #include <sys/mman.h> |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 18 | #include <time.h> |
| 19 | #include <unistd.h> |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 20 | #include <utime.h> |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 21 | |
| 22 | #include "windows.h" |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 23 | #include "winerror.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 24 | #include "drive.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 25 | #include "file.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 26 | #include "global.h" |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 27 | #include "heap.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 28 | #include "msdos.h" |
| 29 | #include "options.h" |
| 30 | #include "ldt.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 31 | #include "process.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 32 | #include "task.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 33 | #include "debug.h" |
| 34 | |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 35 | #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) |
| 36 | #define MAP_ANON MAP_ANONYMOUS |
| 37 | #endif |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 38 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 39 | static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars, |
| 40 | LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped); |
| 41 | static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars, |
| 42 | LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 43 | static void FILE_Destroy( K32OBJ *obj ); |
| 44 | |
| 45 | const K32OBJ_OPS FILE_Ops = |
| 46 | { |
| 47 | /* Object cannot be waited upon (FIXME: for now) */ |
| 48 | NULL, /* signaled */ |
| 49 | NULL, /* satisfied */ |
| 50 | NULL, /* add_wait */ |
| 51 | NULL, /* remove_wait */ |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 52 | FILE_Read, /* read */ |
| 53 | FILE_Write, /* write */ |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 54 | FILE_Destroy /* destroy */ |
| 55 | }; |
| 56 | |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 57 | struct DOS_FILE_LOCK { |
| 58 | struct DOS_FILE_LOCK * next; |
| 59 | DWORD base; |
| 60 | DWORD len; |
| 61 | DWORD processId; |
| 62 | FILE_OBJECT * dos_file; |
| 63 | char * unix_name; |
| 64 | }; |
| 65 | |
| 66 | typedef struct DOS_FILE_LOCK DOS_FILE_LOCK; |
| 67 | |
| 68 | static DOS_FILE_LOCK *locks = NULL; |
| 69 | static void DOS_RemoveFileLocks(FILE_OBJECT *file); |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 70 | |
| 71 | /*********************************************************************** |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 72 | * FILE_Alloc |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 73 | * |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 74 | * Allocate a file. |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 75 | */ |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 76 | HFILE32 FILE_Alloc( FILE_OBJECT **file ) |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 77 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 78 | HFILE32 handle; |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 79 | *file = HeapAlloc( SystemHeap, 0, sizeof(FILE_OBJECT) ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 80 | if (!*file) |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 81 | { |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 82 | DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk ); |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 83 | return (HFILE32)NULL; |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 84 | } |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 85 | (*file)->header.type = K32OBJ_FILE; |
| 86 | (*file)->header.refcount = 0; |
| 87 | (*file)->unix_handle = -1; |
| 88 | (*file)->unix_name = NULL; |
| 89 | (*file)->type = FILE_TYPE_DISK; |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 90 | (*file)->pos = 0; |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 91 | (*file)->mode = 0; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 92 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 93 | handle = HANDLE_Alloc( PROCESS_Current(), &(*file)->header, |
| 94 | FILE_ALL_ACCESS | GENERIC_READ | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 95 | GENERIC_WRITE | GENERIC_EXECUTE /*FIXME*/, TRUE, -1 ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 96 | /* If the allocation failed, the object is already destroyed */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 97 | if (handle == INVALID_HANDLE_VALUE32) *file = NULL; |
| 98 | return handle; |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 102 | /* FIXME: lpOverlapped is ignored */ |
| 103 | static BOOL32 FILE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars, |
| 104 | LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped) |
| 105 | { |
| 106 | FILE_OBJECT *file = (FILE_OBJECT *)ptr; |
| 107 | int result; |
| 108 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 109 | TRACE(file, "%p %p %ld\n", ptr, lpBuffer, |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 110 | nNumberOfChars); |
| 111 | |
| 112 | if (nNumberOfChars == 0) { |
| 113 | *lpNumberOfChars = 0; /* FIXME: does this change */ |
| 114 | return TRUE; |
| 115 | } |
| 116 | |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 117 | if ( (file->pos < 0) || /* workaround, see SetFilePointer */ |
| 118 | ((result = read(file->unix_handle, lpBuffer, nNumberOfChars)) == -1) ) |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 119 | { |
| 120 | FILE_SetDosError(); |
| 121 | return FALSE; |
| 122 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 123 | file->pos += result; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 124 | *lpNumberOfChars = result; |
| 125 | return TRUE; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * experimentation yields that WriteFile: |
| 130 | * o does not truncate on write of 0 |
| 131 | * o always changes the *lpNumberOfChars to actual number of |
| 132 | * characters written |
| 133 | * o write of 0 nNumberOfChars returns TRUE |
| 134 | */ |
| 135 | static BOOL32 FILE_Write(K32OBJ *ptr, LPCVOID lpBuffer, DWORD nNumberOfChars, |
| 136 | LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped) |
| 137 | { |
| 138 | FILE_OBJECT *file = (FILE_OBJECT *)ptr; |
| 139 | int result; |
| 140 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 141 | TRACE(file, "%p %p %ld\n", ptr, lpBuffer, |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 142 | nNumberOfChars); |
| 143 | |
| 144 | *lpNumberOfChars = 0; |
| 145 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 146 | /* |
| 147 | * I assume this loop around EAGAIN is here because |
| 148 | * win32 doesn't have interrupted system calls |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 149 | */ |
| 150 | |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 151 | if (file->pos < 0) { /* workaround, see SetFilePointer */ |
| 152 | FILE_SetDosError(); |
| 153 | return FALSE; |
| 154 | } |
| 155 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 156 | for (;;) |
| 157 | { |
| 158 | result = write(file->unix_handle, lpBuffer, nNumberOfChars); |
| 159 | if (result != -1) { |
| 160 | *lpNumberOfChars = result; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 161 | file->pos += result; |
| 162 | return TRUE; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 163 | } |
| 164 | if (errno != EINTR) { |
| 165 | FILE_SetDosError(); |
| 166 | return FALSE; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 173 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 174 | * FILE_Destroy |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 175 | * |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 176 | * Destroy a DOS file. |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 177 | */ |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 178 | static void FILE_Destroy( K32OBJ *ptr ) |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 179 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 180 | FILE_OBJECT *file = (FILE_OBJECT *)ptr; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 181 | assert( ptr->type == K32OBJ_FILE ); |
| 182 | |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 183 | DOS_RemoveFileLocks(file); |
| 184 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 185 | if (file->unix_handle != -1) close( file->unix_handle ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 186 | if (file->unix_name) HeapFree( SystemHeap, 0, file->unix_name ); |
| 187 | ptr->type = K32OBJ_UNKNOWN; |
| 188 | HeapFree( SystemHeap, 0, file ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | /*********************************************************************** |
| 193 | * FILE_GetFile |
| 194 | * |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 195 | * Return the DOS file associated to a task file handle. FILE_ReleaseFile must |
| 196 | * be called to release the file. |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 197 | */ |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 198 | FILE_OBJECT *FILE_GetFile( HFILE32 handle ) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 199 | { |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 200 | return (FILE_OBJECT *)HANDLE_GetObjPtr( PROCESS_Current(), handle, |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 201 | K32OBJ_FILE, 0 /*FIXME*/, NULL ); |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 206 | * FILE_ReleaseFile |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 207 | * |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 208 | * Release a DOS file obtained with FILE_GetFile. |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 209 | */ |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 210 | void FILE_ReleaseFile( FILE_OBJECT *file ) |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 211 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 212 | K32OBJ_DecCount( &file->header ); |
| 213 | } |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 214 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 215 | |
| 216 | /*********************************************************************** |
| 217 | * FILE_GetUnixHandle |
| 218 | * |
| 219 | * Return the Unix handle associated to a file handle. |
| 220 | */ |
| 221 | int FILE_GetUnixHandle( HFILE32 hFile ) |
| 222 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 223 | FILE_OBJECT *file; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 224 | int ret; |
| 225 | |
| 226 | if (!(file = FILE_GetFile( hFile ))) return -1; |
| 227 | ret = file->unix_handle; |
| 228 | FILE_ReleaseFile( file ); |
| 229 | return ret; |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 232 | /*********************************************************************** |
| 233 | * FILE_UnixToDosMode |
| 234 | * |
| 235 | * PARAMS |
| 236 | * unixmode[I] |
| 237 | * RETURNS |
| 238 | * dosmode |
| 239 | */ |
| 240 | static int FILE_UnixToDosMode(int unixMode) |
| 241 | { |
| 242 | int dosMode; |
| 243 | switch(unixMode & 3) |
| 244 | { |
| 245 | case O_WRONLY: |
| 246 | dosMode = OF_WRITE; |
| 247 | break; |
| 248 | case O_RDWR: |
| 249 | dosMode =OF_READWRITE; |
| 250 | break; |
| 251 | case O_RDONLY: |
| 252 | default: |
| 253 | dosMode = OF_READ; |
| 254 | break; |
| 255 | } |
| 256 | return dosMode; |
| 257 | } |
| 258 | |
| 259 | /*********************************************************************** |
| 260 | * FILE_DOSToUnixMode |
| 261 | * |
| 262 | * PARAMS |
| 263 | * dosMode[I] |
| 264 | * RETURNS |
| 265 | * unixmode |
| 266 | */ |
| 267 | static int FILE_DOSToUnixMode(int dosMode) |
| 268 | { |
| 269 | int unixMode; |
| 270 | switch(dosMode & 3) |
| 271 | { |
| 272 | case OF_WRITE: |
| 273 | unixMode = O_WRONLY; break; |
| 274 | case OF_READWRITE: |
| 275 | unixMode = O_RDWR; break; |
| 276 | case OF_READ: |
| 277 | default: |
| 278 | unixMode = O_RDONLY; break; |
| 279 | } |
| 280 | return unixMode; |
| 281 | } |
| 282 | |
| 283 | /*********************************************************************** |
| 284 | * FILE_ShareDeny |
| 285 | * |
| 286 | * PARAMS |
| 287 | * oldmode[I] mode how file was first opened |
| 288 | * mode[I] mode how the file should get opened |
| 289 | * RETURNS |
| 290 | * TRUE: deny open |
| 291 | * FALSE: allow open |
| 292 | * |
| 293 | * Look what we have to do with the given SHARE modes |
| 294 | * |
| 295 | * Ralph Brown's interrupt list gives following explication, I guess |
| 296 | * the same holds for Windows, DENY ALL should be OF_SHARE_COMPAT |
| 297 | * |
| 298 | * FIXME: Validate this function |
| 299 | ========from Ralph Brown's list ========= |
| 300 | (Table 0750) |
| 301 | Values of DOS file sharing behavior: |
| 302 | | Second and subsequent Opens |
| 303 | First |Compat Deny Deny Deny Deny |
| 304 | Open | All Write Read None |
| 305 | |R W RW R W RW R W RW R W RW R W RW |
| 306 | - - - - -| - - - - - - - - - - - - - - - - - |
| 307 | Compat R |Y Y Y N N N 1 N N N N N 1 N N |
| 308 | W |Y Y Y N N N N N N N N N N N N |
| 309 | RW|Y Y Y N N N N N N N N N N N N |
| 310 | - - - - -| |
| 311 | Deny R |C C C N N N N N N N N N N N N |
| 312 | All W |C C C N N N N N N N N N N N N |
| 313 | RW|C C C N N N N N N N N N N N N |
| 314 | - - - - -| |
| 315 | Deny R |2 C C N N N Y N N N N N Y N N |
| 316 | Write W |C C C N N N N N N Y N N Y N N |
| 317 | RW|C C C N N N N N N N N N Y N N |
| 318 | - - - - -| |
| 319 | Deny R |C C C N N N N Y N N N N N Y N |
| 320 | Read W |C C C N N N N N N N Y N N Y N |
| 321 | RW|C C C N N N N N N N N N N Y N |
| 322 | - - - - -| |
| 323 | Deny R |2 C C N N N Y Y Y N N N Y Y Y |
| 324 | None W |C C C N N N N N N Y Y Y Y Y Y |
| 325 | RW|C C C N N N N N N N N N Y Y Y |
| 326 | Legend: Y = open succeeds, N = open fails with error code 05h |
| 327 | C = open fails, INT 24 generated |
| 328 | 1 = open succeeds if file read-only, else fails with error code |
| 329 | 2 = open succeeds if file read-only, else fails with INT 24 |
| 330 | ========end of description from Ralph Brown's List ===== |
| 331 | For every "Y" in the table we return FALSE |
| 332 | For every "N" we set the DOS_ERROR and return TRUE |
| 333 | For all other cases we barf,set the DOS_ERROR and return TRUE |
| 334 | |
| 335 | */ |
| 336 | static BOOL32 FILE_ShareDeny( int mode, int oldmode) |
| 337 | { |
| 338 | int oldsharemode = oldmode & 0x70; |
| 339 | int sharemode = mode & 0x70; |
| 340 | int oldopenmode = oldmode & 3; |
| 341 | int openmode = mode & 3; |
| 342 | |
| 343 | switch (oldsharemode) |
| 344 | { |
| 345 | case OF_SHARE_COMPAT: |
| 346 | if (sharemode == OF_SHARE_COMPAT) return FALSE; |
| 347 | if (openmode == OF_READ) goto test_ro_err05 ; |
| 348 | goto fail_error05; |
| 349 | case OF_SHARE_EXCLUSIVE: |
| 350 | if (sharemode == OF_SHARE_COMPAT) goto fail_int24; |
| 351 | goto fail_error05; |
| 352 | case OF_SHARE_DENY_WRITE: |
| 353 | if (openmode != OF_READ) |
| 354 | { |
| 355 | if (sharemode == OF_SHARE_COMPAT) goto fail_int24; |
| 356 | goto fail_error05; |
| 357 | } |
| 358 | switch (sharemode) |
| 359 | { |
| 360 | case OF_SHARE_COMPAT: |
| 361 | if (oldopenmode == OF_READ) goto test_ro_int24 ; |
| 362 | goto fail_int24; |
| 363 | case OF_SHARE_DENY_NONE : |
| 364 | return FALSE; |
| 365 | case OF_SHARE_DENY_WRITE : |
| 366 | if (oldopenmode == OF_READ) return FALSE; |
| 367 | case OF_SHARE_DENY_READ : |
| 368 | if (oldopenmode == OF_WRITE) return FALSE; |
| 369 | case OF_SHARE_EXCLUSIVE: |
| 370 | default: |
| 371 | goto fail_error05; |
| 372 | } |
| 373 | break; |
| 374 | case OF_SHARE_DENY_READ: |
| 375 | if (openmode != OF_WRITE) |
| 376 | { |
| 377 | if (sharemode == OF_SHARE_COMPAT) goto fail_int24; |
| 378 | goto fail_error05; |
| 379 | } |
| 380 | switch (sharemode) |
| 381 | { |
| 382 | case OF_SHARE_COMPAT: |
| 383 | goto fail_int24; |
| 384 | case OF_SHARE_DENY_NONE : |
| 385 | return FALSE; |
| 386 | case OF_SHARE_DENY_WRITE : |
| 387 | if (oldopenmode == OF_READ) return FALSE; |
| 388 | case OF_SHARE_DENY_READ : |
| 389 | if (oldopenmode == OF_WRITE) return FALSE; |
| 390 | case OF_SHARE_EXCLUSIVE: |
| 391 | default: |
| 392 | goto fail_error05; |
| 393 | } |
| 394 | break; |
| 395 | case OF_SHARE_DENY_NONE: |
| 396 | switch (sharemode) |
| 397 | { |
| 398 | case OF_SHARE_COMPAT: |
| 399 | goto fail_int24; |
| 400 | case OF_SHARE_DENY_NONE : |
| 401 | return FALSE; |
| 402 | case OF_SHARE_DENY_WRITE : |
| 403 | if (oldopenmode == OF_READ) return FALSE; |
| 404 | case OF_SHARE_DENY_READ : |
| 405 | if (oldopenmode == OF_WRITE) return FALSE; |
| 406 | case OF_SHARE_EXCLUSIVE: |
| 407 | default: |
| 408 | goto fail_error05; |
| 409 | } |
| 410 | default: |
| 411 | ERR(file,"unknown mode\n"); |
| 412 | } |
| 413 | ERR(file,"shouldn't happen\n"); |
| 414 | ERR(file,"Please report to bon@elektron.ikp.physik.tu-darmstadt.de\n"); |
| 415 | return TRUE; |
| 416 | |
| 417 | test_ro_int24: |
| 418 | FIXME(file,"test if file is RO missing\n"); |
| 419 | /* Fall through */ |
| 420 | fail_int24: |
| 421 | FIXME(file,"generate INT24 missing\n"); |
| 422 | /* Is this the right error? */ |
| 423 | DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk ); |
| 424 | return TRUE; |
| 425 | |
| 426 | test_ro_err05: |
| 427 | FIXME(file,"test if file is RO missing\n"); |
| 428 | /* fall through */ |
| 429 | fail_error05: |
| 430 | DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk ); |
| 431 | return TRUE; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | |
| 436 | /*********************************************************************** |
| 437 | * |
| 438 | * |
| 439 | * Look if the File is in Use For the OF_SHARE_XXX options |
| 440 | * |
| 441 | * PARAMS |
| 442 | * name [I]: full unix name of the file that should be opened |
| 443 | * mode [O]: mode how the file was first opened |
| 444 | * RETURNS |
| 445 | * TRUE if the file was opened before |
| 446 | * FALSE if we open the file exclusive for this process |
| 447 | * |
| 448 | * Scope of the files we look for is only the current pdb |
| 449 | * Could we use /proc/self/? on Linux for this? |
| 450 | * Should we use flock? Should we create another structure? |
| 451 | * Searching through all files seem quite expensive for me, but |
| 452 | * I don't see any other way. |
| 453 | * |
| 454 | * FIXME: Extend scope to the whole Wine process |
| 455 | * |
| 456 | */ |
| 457 | static BOOL32 FILE_InUse(char * name, int * mode) |
| 458 | { |
| 459 | FILE_OBJECT *file; |
| 460 | int i; |
| 461 | HGLOBAL16 hPDB = GetCurrentPDB(); |
| 462 | PDB *pdb = (PDB *)GlobalLock16( hPDB ); |
| 463 | |
| 464 | if (!pdb) return 0; |
| 465 | for (i=0;i<pdb->nbFiles;i++) |
| 466 | { |
| 467 | file =FILE_GetFile( (HFILE32) i); |
| 468 | if(file) |
| 469 | { |
| 470 | if(file->unix_name) |
| 471 | { |
| 472 | TRACE(file,"got %s at %d\n",file->unix_name,i); |
| 473 | if(!lstrcmp32A(file->unix_name,name)) |
| 474 | { |
| 475 | *mode = file->mode; |
| 476 | FILE_ReleaseFile(file); |
| 477 | return TRUE; |
| 478 | } |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 479 | } |
Rein Klazes | 1518a94 | 1998-10-31 12:08:55 +0000 | [diff] [blame] | 480 | FILE_ReleaseFile(file); |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | return FALSE; |
| 484 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 485 | |
| 486 | /*********************************************************************** |
| 487 | * FILE_SetDosError |
| 488 | * |
| 489 | * Set the DOS error code from errno. |
| 490 | */ |
| 491 | void FILE_SetDosError(void) |
| 492 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 493 | int save_errno = errno; /* errno gets overwritten by printf */ |
| 494 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 495 | TRACE(file, "errno = %d %s\n", errno, strerror(errno)); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 496 | switch (save_errno) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 497 | { |
| 498 | case EAGAIN: |
| 499 | DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk ); |
| 500 | break; |
| 501 | case EBADF: |
| 502 | DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk ); |
| 503 | break; |
| 504 | case ENOSPC: |
| 505 | DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 506 | break; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 507 | case EACCES: |
| 508 | case EPERM: |
| 509 | case EROFS: |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 510 | DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 511 | break; |
| 512 | case EBUSY: |
| 513 | DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 514 | break; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 515 | case ENOENT: |
| 516 | DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 517 | break; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 518 | case EISDIR: |
| 519 | DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown ); |
| 520 | break; |
| 521 | case ENFILE: |
| 522 | case EMFILE: |
| 523 | DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown ); |
| 524 | break; |
| 525 | case EEXIST: |
| 526 | DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 527 | break; |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 528 | case EINVAL: |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 529 | case ESPIPE: |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 530 | DOS_ERROR( ER_SeekError, EC_NotFound, SA_Ignore, EL_Disk ); |
| 531 | break; |
| 532 | case ENOTEMPTY: |
| 533 | DOS_ERROR( ERROR_DIR_NOT_EMPTY, EC_Exists, SA_Ignore, EL_Disk ); |
| 534 | break; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 535 | default: |
| 536 | perror( "int21: unknown errno" ); |
| 537 | DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown ); |
| 538 | break; |
| 539 | } |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 540 | errno = save_errno; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | |
| 544 | /*********************************************************************** |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 545 | * FILE_DupUnixHandle |
| 546 | * |
| 547 | * Duplicate a Unix handle into a task handle. |
| 548 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 549 | HFILE32 FILE_DupUnixHandle( int fd ) |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 550 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 551 | HFILE32 handle; |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 552 | FILE_OBJECT *file; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 553 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 554 | if ((handle = FILE_Alloc( &file )) != INVALID_HANDLE_VALUE32) |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 555 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 556 | if ((file->unix_handle = dup(fd)) == -1) |
| 557 | { |
| 558 | FILE_SetDosError(); |
| 559 | CloseHandle( handle ); |
| 560 | return INVALID_HANDLE_VALUE32; |
| 561 | } |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 562 | } |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 563 | return handle; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /*********************************************************************** |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 568 | * FILE_OpenUnixFile |
| 569 | */ |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 570 | HFILE32 FILE_OpenUnixFile( const char *name, int mode ) |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 571 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 572 | HFILE32 handle; |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 573 | FILE_OBJECT *file; |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 574 | struct stat st; |
| 575 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 576 | if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32) |
| 577 | return INVALID_HANDLE_VALUE32; |
| 578 | |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 579 | if ((file->unix_handle = open( name, mode, 0666 )) == -1) |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 580 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 581 | if (!Options.failReadOnly && (mode == O_RDWR)) |
| 582 | file->unix_handle = open( name, O_RDONLY ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 583 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 584 | if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1)) |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 585 | { |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 586 | FILE_SetDosError(); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 587 | CloseHandle( handle ); |
| 588 | return INVALID_HANDLE_VALUE32; |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 589 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 590 | if (S_ISDIR(st.st_mode)) |
| 591 | { |
| 592 | DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 593 | CloseHandle( handle ); |
| 594 | return INVALID_HANDLE_VALUE32; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 597 | /* File opened OK, now fill the FILE_OBJECT */ |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 598 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 599 | file->unix_name = HEAP_strdupA( SystemHeap, 0, name ); |
| 600 | return handle; |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
| 604 | /*********************************************************************** |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 605 | * FILE_Open |
Uwe Bonnes | a370ab4 | 1998-10-24 09:16:44 +0000 | [diff] [blame] | 606 | * |
| 607 | * path[I] name of file to open |
| 608 | * mode[I] mode how to open, in unix notation |
| 609 | * shareMode[I] the sharing mode in the win OpenFile notation |
| 610 | * |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 611 | */ |
Uwe Bonnes | a370ab4 | 1998-10-24 09:16:44 +0000 | [diff] [blame] | 612 | HFILE32 FILE_Open( LPCSTR path, INT32 mode, INT32 shareMode ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 613 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 614 | DOS_FULL_NAME full_name; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 615 | const char *unixName; |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 616 | int oldMode, dosMode; /* FIXME: Do we really need unixmode as argument for |
| 617 | FILE_Open */ |
| 618 | FILE_OBJECT *file; |
| 619 | HFILE32 hFileRet; |
| 620 | BOOL32 fileInUse = FALSE; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 621 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 622 | TRACE(file, "'%s' %04x\n", path, mode ); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 623 | |
| 624 | if (!path) return HFILE_ERROR32; |
| 625 | |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 626 | if (DOSFS_GetDevice( path )) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 627 | { |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 628 | HFILE32 ret; |
| 629 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 630 | TRACE(file, "opening device '%s'\n", path ); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 631 | |
| 632 | if (HFILE_ERROR32!=(ret=DOSFS_OpenDevice( path, mode ))) |
| 633 | return ret; |
| 634 | |
| 635 | /* Do not silence this please. It is a critical error. -MM */ |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 636 | ERR(file, "Couldn't open device '%s'!\n",path); |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 637 | DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk ); |
| 638 | return HFILE_ERROR32; |
| 639 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 640 | } |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 641 | else /* check for filename, don't check for last entry if creating */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 642 | { |
| 643 | if (!DOSFS_GetFullName( path, !(mode & O_CREAT), &full_name )) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 644 | return HFILE_ERROR32; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 645 | unixName = full_name.long_name; |
| 646 | } |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 647 | |
Uwe Bonnes | a370ab4 | 1998-10-24 09:16:44 +0000 | [diff] [blame] | 648 | dosMode = FILE_UnixToDosMode(mode)| shareMode; |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 649 | fileInUse = FILE_InUse(full_name.long_name,&oldMode); |
| 650 | if(fileInUse) |
| 651 | { |
| 652 | TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70); |
| 653 | if (FILE_ShareDeny(dosMode,oldMode)) return HFILE_ERROR32; |
| 654 | } |
| 655 | hFileRet = FILE_OpenUnixFile( unixName, mode ); |
| 656 | /* we need to save the mode, but only if it is not in use yet*/ |
| 657 | if ((hFileRet) && (!fileInUse) && ((file =FILE_GetFile(hFileRet)))) |
| 658 | { |
| 659 | file->mode=dosMode; |
| 660 | FILE_ReleaseFile(file); |
| 661 | } |
| 662 | return hFileRet; |
| 663 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | |
| 667 | /*********************************************************************** |
| 668 | * FILE_Create |
| 669 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 670 | static HFILE32 FILE_Create( LPCSTR path, int mode, int unique ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 671 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 672 | HFILE32 handle; |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 673 | FILE_OBJECT *file; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 674 | DOS_FULL_NAME full_name; |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 675 | BOOL32 fileInUse = FALSE; |
| 676 | int oldMode,dosMode; /* FIXME: Do we really need unixmode as argument for |
| 677 | FILE_Create */; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 678 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 679 | TRACE(file, "'%s' %04x %d\n", path, mode, unique ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 680 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 681 | if (!path) return INVALID_HANDLE_VALUE32; |
| 682 | |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 683 | if (DOSFS_GetDevice( path )) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 684 | { |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 685 | WARN(file, "cannot create DOS device '%s'!\n", path); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 686 | DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 687 | return INVALID_HANDLE_VALUE32; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 690 | if ((handle = FILE_Alloc( &file )) == INVALID_HANDLE_VALUE32) |
| 691 | return INVALID_HANDLE_VALUE32; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 692 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 693 | if (!DOSFS_GetFullName( path, FALSE, &full_name )) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 694 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 695 | CloseHandle( handle ); |
| 696 | return INVALID_HANDLE_VALUE32; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 697 | } |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 698 | |
| 699 | dosMode = FILE_UnixToDosMode(mode); |
| 700 | fileInUse = FILE_InUse(full_name.long_name,&oldMode); |
| 701 | if(fileInUse) |
| 702 | { |
| 703 | TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70); |
| 704 | if (FILE_ShareDeny(dosMode,oldMode)) return INVALID_HANDLE_VALUE32; |
| 705 | } |
| 706 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 707 | if ((file->unix_handle = open( full_name.long_name, |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 708 | O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0), |
| 709 | mode )) == -1) |
| 710 | { |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 711 | FILE_SetDosError(); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 712 | CloseHandle( handle ); |
| 713 | return INVALID_HANDLE_VALUE32; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 716 | /* File created OK, now fill the FILE_OBJECT */ |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 717 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 718 | file->unix_name = HEAP_strdupA( SystemHeap, 0, full_name.long_name ); |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 719 | file->mode = dosMode; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 720 | return handle; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | /*********************************************************************** |
| 725 | * FILE_FillInfo |
| 726 | * |
| 727 | * Fill a file information from a struct stat. |
| 728 | */ |
| 729 | static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info ) |
| 730 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 731 | if (S_ISDIR(st->st_mode)) |
Alexandre Julliard | 84c70f5 | 1997-05-09 08:40:27 +0000 | [diff] [blame] | 732 | info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; |
| 733 | else |
| 734 | info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 735 | if (!(st->st_mode & S_IWUSR)) |
| 736 | info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 737 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 738 | DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftCreationTime, 0 ); |
| 739 | DOSFS_UnixTimeToFileTime( st->st_mtime, &info->ftLastWriteTime, 0 ); |
| 740 | DOSFS_UnixTimeToFileTime( st->st_atime, &info->ftLastAccessTime, 0 ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 741 | |
| 742 | info->dwVolumeSerialNumber = 0; /* FIXME */ |
| 743 | info->nFileSizeHigh = 0; |
| 744 | info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size; |
| 745 | info->nNumberOfLinks = st->st_nlink; |
| 746 | info->nFileIndexHigh = 0; |
| 747 | info->nFileIndexLow = st->st_ino; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | |
| 751 | /*********************************************************************** |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 752 | * FILE_Stat |
| 753 | * |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 754 | * Stat a Unix path name. Return TRUE if OK. |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 755 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 756 | BOOL32 FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 757 | { |
| 758 | struct stat st; |
| 759 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 760 | if (!unixName || !info) return FALSE; |
| 761 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 762 | if (stat( unixName, &st ) == -1) |
| 763 | { |
| 764 | FILE_SetDosError(); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 765 | return FALSE; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 766 | } |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 767 | FILE_FillInfo( &st, info ); |
| 768 | return TRUE; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | |
| 772 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 773 | * GetFileInformationByHandle (KERNEL32.219) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 774 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 775 | DWORD WINAPI GetFileInformationByHandle( HFILE32 hFile, |
| 776 | BY_HANDLE_FILE_INFORMATION *info ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 777 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 778 | FILE_OBJECT *file; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 779 | DWORD ret = 0; |
| 780 | struct stat st; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 781 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 782 | if (!info) return 0; |
| 783 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 784 | if (!(file = FILE_GetFile( hFile ))) return 0; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 785 | if (fstat( file->unix_handle, &st ) == -1) FILE_SetDosError(); |
| 786 | else |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 787 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 788 | FILE_FillInfo( &st, info ); |
| 789 | ret = 1; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 790 | } |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 791 | FILE_ReleaseFile( file ); |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | |
| 796 | /************************************************************************** |
| 797 | * GetFileAttributes16 (KERNEL.420) |
| 798 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 799 | DWORD WINAPI GetFileAttributes16( LPCSTR name ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 800 | { |
| 801 | return GetFileAttributes32A( name ); |
| 802 | } |
| 803 | |
| 804 | |
| 805 | /************************************************************************** |
| 806 | * GetFileAttributes32A (KERNEL32.217) |
| 807 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 808 | DWORD WINAPI GetFileAttributes32A( LPCSTR name ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 809 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 810 | DOS_FULL_NAME full_name; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 811 | BY_HANDLE_FILE_INFORMATION info; |
| 812 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 813 | if (name == NULL || *name=='\0') return -1; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 814 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 815 | if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1; |
| 816 | if (!FILE_Stat( full_name.long_name, &info )) return -1; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 817 | return info.dwFileAttributes; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | /************************************************************************** |
| 822 | * GetFileAttributes32W (KERNEL32.218) |
| 823 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 824 | DWORD WINAPI GetFileAttributes32W( LPCWSTR name ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 825 | { |
| 826 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name ); |
| 827 | DWORD res = GetFileAttributes32A( nameA ); |
| 828 | HeapFree( GetProcessHeap(), 0, nameA ); |
| 829 | return res; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
| 833 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 834 | * GetFileSize (KERNEL32.220) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 835 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 836 | DWORD WINAPI GetFileSize( HFILE32 hFile, LPDWORD filesizehigh ) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 837 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 838 | BY_HANDLE_FILE_INFORMATION info; |
| 839 | if (!GetFileInformationByHandle( hFile, &info )) return 0; |
| 840 | if (filesizehigh) *filesizehigh = info.nFileSizeHigh; |
| 841 | return info.nFileSizeLow; |
| 842 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 843 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 844 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 845 | /*********************************************************************** |
| 846 | * GetFileTime (KERNEL32.221) |
| 847 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 848 | BOOL32 WINAPI GetFileTime( HFILE32 hFile, FILETIME *lpCreationTime, |
| 849 | FILETIME *lpLastAccessTime, |
| 850 | FILETIME *lpLastWriteTime ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 851 | { |
| 852 | BY_HANDLE_FILE_INFORMATION info; |
| 853 | if (!GetFileInformationByHandle( hFile, &info )) return FALSE; |
| 854 | if (lpCreationTime) *lpCreationTime = info.ftCreationTime; |
| 855 | if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime; |
| 856 | if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime; |
| 857 | return TRUE; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 860 | /*********************************************************************** |
| 861 | * CompareFileTime (KERNEL32.28) |
| 862 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 863 | INT32 WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y ) |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 864 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 865 | if (!x || !y) return -1; |
| 866 | |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 867 | if (x->dwHighDateTime > y->dwHighDateTime) |
| 868 | return 1; |
| 869 | if (x->dwHighDateTime < y->dwHighDateTime) |
| 870 | return -1; |
| 871 | if (x->dwLowDateTime > y->dwLowDateTime) |
| 872 | return 1; |
| 873 | if (x->dwLowDateTime < y->dwLowDateTime) |
| 874 | return -1; |
| 875 | return 0; |
| 876 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 877 | |
| 878 | /*********************************************************************** |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 879 | * FILE_Dup |
| 880 | * |
| 881 | * dup() function for DOS handles. |
| 882 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 883 | HFILE32 FILE_Dup( HFILE32 hFile ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 884 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 885 | HFILE32 handle; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 886 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 887 | TRACE(file, "FILE_Dup for handle %d\n", hFile ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 888 | if (!DuplicateHandle( GetCurrentProcess(), hFile, GetCurrentProcess(), |
| 889 | &handle, FILE_ALL_ACCESS /* FIXME */, FALSE, 0 )) |
| 890 | handle = HFILE_ERROR32; |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 891 | TRACE(file, "FILE_Dup return handle %d\n", handle ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 892 | return handle; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
| 896 | /*********************************************************************** |
| 897 | * FILE_Dup2 |
| 898 | * |
| 899 | * dup2() function for DOS handles. |
| 900 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 901 | HFILE32 FILE_Dup2( HFILE32 hFile1, HFILE32 hFile2 ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 902 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 903 | FILE_OBJECT *file; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 904 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 905 | TRACE(file, "FILE_Dup2 for handle %d\n", hFile1 ); |
Alexandre Julliard | 03468f7 | 1998-02-15 19:40:49 +0000 | [diff] [blame] | 906 | /* FIXME: should use DuplicateHandle */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 907 | if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR32; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 908 | if (!HANDLE_SetObjPtr( PROCESS_Current(), hFile2, &file->header, 0 )) |
| 909 | hFile2 = HFILE_ERROR32; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 910 | FILE_ReleaseFile( file ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 911 | return hFile2; |
| 912 | } |
| 913 | |
| 914 | |
| 915 | /*********************************************************************** |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 916 | * GetTempFileName16 (KERNEL.97) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 917 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 918 | UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique, |
| 919 | LPSTR buffer ) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 920 | { |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 921 | char temppath[144]; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 922 | |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 923 | if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */ |
Alexandre Julliard | 44ed71f | 1997-12-21 19:17:50 +0000 | [diff] [blame] | 924 | drive |= DRIVE_GetCurrentDrive() + 'A'; |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 925 | |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 926 | if ((drive & TF_FORCEDRIVE) && |
| 927 | !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' )) |
| 928 | { |
| 929 | drive &= ~TF_FORCEDRIVE; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 930 | WARN(file, "invalid drive %d specified\n", drive ); |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 933 | if (drive & TF_FORCEDRIVE) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 934 | sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 935 | else |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 936 | GetTempPath32A( 132, temppath ); |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 937 | return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer ); |
| 938 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 939 | |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 940 | |
| 941 | /*********************************************************************** |
| 942 | * GetTempFileName32A (KERNEL32.290) |
| 943 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 944 | UINT32 WINAPI GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique, |
| 945 | LPSTR buffer) |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 946 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 947 | DOS_FULL_NAME full_name; |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 948 | int i; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 949 | LPSTR p; |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 950 | UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff; |
| 951 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 952 | if ( !path || !prefix || !buffer ) return 0; |
| 953 | |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 954 | strcpy( buffer, path ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 955 | p = buffer + strlen(buffer); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 956 | |
| 957 | /* add a \, if there isn't one and path is more than just the drive letter ... */ |
| 958 | if ( !((strlen(buffer) == 2) && (buffer[1] == ':')) |
| 959 | && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\'; |
| 960 | |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 961 | *p++ = '~'; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 962 | for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++; |
| 963 | sprintf( p, "%04x.tmp", num ); |
| 964 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 965 | /* Now try to create it */ |
| 966 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 967 | if (!unique) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 968 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 969 | do |
| 970 | { |
| 971 | HFILE32 handle = FILE_Create( buffer, 0666, TRUE ); |
| 972 | if (handle != INVALID_HANDLE_VALUE32) |
| 973 | { /* We created it */ |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 974 | TRACE(file, "created %s\n", |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 975 | buffer); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 976 | CloseHandle( handle ); |
| 977 | break; |
| 978 | } |
| 979 | if (DOS_ExtendedError != ER_FileExists) |
| 980 | break; /* No need to go on */ |
| 981 | num++; |
| 982 | sprintf( p, "%04x.tmp", num ); |
| 983 | } while (num != (unique & 0xffff)); |
| 984 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 985 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 986 | /* Get the full path name */ |
| 987 | |
| 988 | if (DOSFS_GetFullName( buffer, FALSE, &full_name )) |
| 989 | { |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 990 | /* Check if we have write access in the directory */ |
| 991 | if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0'; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 992 | if (access( full_name.long_name, W_OK ) == -1) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 993 | WARN(file, "returns '%s', which doesn't seem to be writeable.\n", |
| 994 | buffer); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 995 | } |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 996 | TRACE(file, "returning %s\n", buffer ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 997 | return unique ? unique : num; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
| 1001 | /*********************************************************************** |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1002 | * GetTempFileName32W (KERNEL32.291) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1003 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1004 | UINT32 WINAPI GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique, |
| 1005 | LPWSTR buffer ) |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1006 | { |
| 1007 | LPSTR patha,prefixa; |
| 1008 | char buffera[144]; |
| 1009 | UINT32 ret; |
| 1010 | |
| 1011 | if (!path) return 0; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1012 | patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path ); |
| 1013 | prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix ); |
| 1014 | ret = GetTempFileName32A( patha, prefixa, unique, buffera ); |
| 1015 | lstrcpyAtoW( buffer, buffera ); |
| 1016 | HeapFree( GetProcessHeap(), 0, patha ); |
| 1017 | HeapFree( GetProcessHeap(), 0, prefixa ); |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1023 | * FILE_DoOpenFile |
| 1024 | * |
| 1025 | * Implementation of OpenFile16() and OpenFile32(). |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1026 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1027 | static HFILE32 FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode, |
| 1028 | BOOL32 win32 ) |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1029 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1030 | HFILE32 hFileRet; |
| 1031 | FILETIME filetime; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1032 | WORD filedatetime[2]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1033 | DOS_FULL_NAME full_name; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1034 | char *p; |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 1035 | int unixMode, oldMode; |
| 1036 | FILE_OBJECT *file; |
| 1037 | BOOL32 fileInUse = FALSE; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1038 | |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1039 | if (!ofs) return HFILE_ERROR32; |
| 1040 | |
| 1041 | |
| 1042 | ofs->cBytes = sizeof(OFSTRUCT); |
| 1043 | ofs->nErrCode = 0; |
| 1044 | if (mode & OF_REOPEN) name = ofs->szPathName; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (!name) { |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1047 | ERR(file, "called with `name' set to NULL ! Please debug.\n"); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 1048 | return HFILE_ERROR32; |
| 1049 | } |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1050 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1051 | TRACE(file, "%s %04x\n", name, mode ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1052 | |
Alexandre Julliard | 889f742 | 1997-04-15 17:19:52 +0000 | [diff] [blame] | 1053 | /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName |
| 1054 | Are there any cases where getting the path here is wrong? |
| 1055 | Uwe Bonnes 1997 Apr 2 */ |
| 1056 | if (!GetFullPathName32A( name, sizeof(ofs->szPathName), |
| 1057 | ofs->szPathName, NULL )) goto error; |
| 1058 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1059 | /* OF_PARSE simply fills the structure */ |
| 1060 | |
| 1061 | if (mode & OF_PARSE) |
| 1062 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1063 | ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' ) |
| 1064 | != DRIVE_REMOVABLE); |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1065 | TRACE(file, "(%s): OF_PARSE, res = '%s'\n", |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1066 | name, ofs->szPathName ); |
| 1067 | return 0; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | /* OF_CREATE is completely different from all other options, so |
| 1071 | handle it first */ |
| 1072 | |
| 1073 | if (mode & OF_CREATE) |
| 1074 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1075 | if ((hFileRet = FILE_Create(name,0666,FALSE))== INVALID_HANDLE_VALUE32) |
| 1076 | goto error; |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1077 | goto success; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1080 | /* If OF_SEARCH is set, ignore the given path */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1081 | |
| 1082 | if ((mode & OF_SEARCH) && !(mode & OF_REOPEN)) |
| 1083 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1084 | /* First try the file name as is */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1085 | if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1086 | /* Now remove the path */ |
| 1087 | if (name[0] && (name[1] == ':')) name += 2; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1088 | if ((p = strrchr( name, '\\' ))) name = p + 1; |
| 1089 | if ((p = strrchr( name, '/' ))) name = p + 1; |
| 1090 | if (!name[0]) goto not_found; |
| 1091 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1092 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1093 | /* Now look for the file */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1094 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1095 | if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1096 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1097 | found: |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1098 | TRACE(file, "found %s = %s\n", |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1099 | full_name.long_name, full_name.short_name ); |
| 1100 | lstrcpyn32A( ofs->szPathName, full_name.short_name, |
| 1101 | sizeof(ofs->szPathName) ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1102 | |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 1103 | fileInUse = FILE_InUse(full_name.long_name,&oldMode); |
| 1104 | if(fileInUse) |
| 1105 | { |
| 1106 | TRACE(file, "found another instance with mode 0x%02x\n",oldMode&0x70); |
| 1107 | if (FILE_ShareDeny(mode,oldMode)) return HFILE_ERROR32; |
| 1108 | } |
| 1109 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1110 | if (mode & OF_SHARE_EXCLUSIVE) |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 1111 | /* Some InstallShield version uses OF_SHARE_EXCLUSIVE |
| 1112 | on the file <tempdir>/_ins0432._mp to determine how |
| 1113 | far installation has proceeded. |
| 1114 | _ins0432._mp is an executable and while running the |
| 1115 | application expects the open with OF_SHARE_ to fail*/ |
| 1116 | /* Probable FIXME: |
| 1117 | As our loader closes the files after loading the executable, |
| 1118 | we can't find the running executable with FILE_InUse. |
| 1119 | Perhaps the loader should keep the file open. |
| 1120 | Recheck against how Win handles that case */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1121 | { |
| 1122 | char *last = strrchr(full_name.long_name,'/'); |
| 1123 | if (!last) |
| 1124 | last = full_name.long_name - 1; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1125 | if (GetModuleHandle16(last+1)) |
| 1126 | { |
| 1127 | TRACE(file,"Denying shared open for %s\n",full_name.long_name); |
| 1128 | return HFILE_ERROR32; |
| 1129 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1132 | if (mode & OF_DELETE) |
| 1133 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1134 | if (unlink( full_name.long_name ) == -1) goto not_found; |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1135 | TRACE(file, "(%s): OF_DELETE return = OK\n", name); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1136 | return 1; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 1139 | unixMode=FILE_DOSToUnixMode(mode); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1140 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1141 | hFileRet = FILE_OpenUnixFile( full_name.long_name, unixMode ); |
| 1142 | if (hFileRet == HFILE_ERROR32) goto not_found; |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 1143 | /* we need to save the mode, but only if it is not in use yet*/ |
| 1144 | if( (!fileInUse) &&(file =FILE_GetFile(hFileRet))) |
| 1145 | { |
| 1146 | file->mode=mode; |
| 1147 | FILE_ReleaseFile(file); |
| 1148 | } |
| 1149 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1150 | GetFileTime( hFileRet, NULL, NULL, &filetime ); |
| 1151 | FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1152 | if ((mode & OF_VERIFY) && (mode & OF_REOPEN)) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1153 | { |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1154 | if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) )) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1155 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1156 | CloseHandle( hFileRet ); |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1157 | WARN(file, "(%s): OF_VERIFY failed\n", name ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1158 | /* FIXME: what error here? */ |
| 1159 | DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk ); |
| 1160 | goto error; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1161 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1162 | } |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1163 | memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1164 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1165 | success: /* We get here if the open was successful */ |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1166 | TRACE(file, "(%s): OK, return = %d\n", name, hFileRet ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1167 | if (mode & OF_EXIST) /* Return the handle, but close it first */ |
| 1168 | CloseHandle( hFileRet ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1169 | return hFileRet; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1170 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1171 | not_found: /* We get here if the file does not exist */ |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1172 | WARN(file, "'%s' not found\n", name ); |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1173 | DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk ); |
| 1174 | /* fall through */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1175 | |
Alexandre Julliard | d2e1c1a | 1996-03-09 16:12:43 +0000 | [diff] [blame] | 1176 | error: /* We get here if there was an error opening the file */ |
| 1177 | ofs->nErrCode = DOS_ExtendedError; |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1178 | WARN(file, "(%s): return = HFILE_ERROR error= %d\n", |
Alexandre Julliard | 889f742 | 1997-04-15 17:19:52 +0000 | [diff] [blame] | 1179 | name,ofs->nErrCode ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1180 | return HFILE_ERROR32; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1183 | |
| 1184 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1185 | * OpenFile16 (KERNEL.74) |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1186 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1187 | HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode ) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1188 | { |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1189 | TRACE(file,"OpenFile16(%s,%i)\n", name, mode); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1190 | return HFILE32_TO_HFILE16(FILE_DoOpenFile( name, ofs, mode, FALSE )); |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1191 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1192 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1193 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1194 | /*********************************************************************** |
| 1195 | * OpenFile32 (KERNEL32.396) |
| 1196 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1197 | HFILE32 WINAPI OpenFile32( LPCSTR name, OFSTRUCT *ofs, UINT32 mode ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1198 | { |
| 1199 | return FILE_DoOpenFile( name, ofs, mode, TRUE ); |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | /*********************************************************************** |
| 1204 | * _lclose16 (KERNEL.81) |
| 1205 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1206 | HFILE16 WINAPI _lclose16( HFILE16 hFile ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1207 | { |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1208 | TRACE(file, "handle %d\n", hFile ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1209 | return CloseHandle( HFILE16_TO_HFILE32( hFile ) ) ? 0 : HFILE_ERROR16; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | /*********************************************************************** |
| 1214 | * _lclose32 (KERNEL32.592) |
| 1215 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1216 | HFILE32 WINAPI _lclose32( HFILE32 hFile ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1217 | { |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1218 | TRACE(file, "handle %d\n", hFile ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1219 | return CloseHandle( hFile ) ? 0 : HFILE_ERROR32; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | /*********************************************************************** |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1224 | * WIN16_hread |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1225 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1226 | LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1227 | { |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1228 | LONG maxlen; |
| 1229 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1230 | TRACE(file, "%d %08lx %ld\n", |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1231 | hFile, (DWORD)buffer, count ); |
| 1232 | |
| 1233 | /* Some programs pass a count larger than the allocated buffer */ |
| 1234 | maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1; |
| 1235 | if (count > maxlen) count = maxlen; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1236 | return _lread32(HFILE16_TO_HFILE32(hFile), PTR_SEG_TO_LIN(buffer), count ); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | /*********************************************************************** |
| 1241 | * WIN16_lread |
| 1242 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1243 | UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1244 | { |
| 1245 | return (UINT16)WIN16_hread( hFile, buffer, (LONG)count ); |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | /*********************************************************************** |
| 1250 | * _lread32 (KERNEL32.596) |
| 1251 | */ |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1252 | UINT32 WINAPI _lread32( HFILE32 handle, LPVOID buffer, UINT32 count ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1253 | { |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1254 | K32OBJ *ptr; |
| 1255 | DWORD numWritten; |
| 1256 | BOOL32 result = FALSE; |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 1257 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1258 | TRACE( file, "%d %p %d\n", handle, buffer, count); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1259 | if (!(ptr = HANDLE_GetObjPtr( PROCESS_Current(), handle, |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1260 | K32OBJ_UNKNOWN, 0, NULL))) return -1; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1261 | if (K32OBJ_OPS(ptr)->read) |
| 1262 | result = K32OBJ_OPS(ptr)->read(ptr, buffer, count, &numWritten, NULL); |
| 1263 | K32OBJ_DecCount( ptr ); |
| 1264 | if (!result) return -1; |
| 1265 | return (UINT32)numWritten; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | |
| 1269 | /*********************************************************************** |
| 1270 | * _lread16 (KERNEL.82) |
| 1271 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1272 | UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1273 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1274 | return (UINT16)_lread32(HFILE16_TO_HFILE32(hFile), buffer, (LONG)count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1279 | * _lcreat16 (KERNEL.83) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1280 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1281 | HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1282 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1283 | int mode = (attr & 1) ? 0444 : 0666; |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1284 | TRACE(file, "%s %02x\n", path, attr ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1285 | return (HFILE16) HFILE32_TO_HFILE16(FILE_Create( path, mode, FALSE )); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | /*********************************************************************** |
| 1290 | * _lcreat32 (KERNEL32.593) |
| 1291 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1292 | HFILE32 WINAPI _lcreat32( LPCSTR path, INT32 attr ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1293 | { |
| 1294 | int mode = (attr & 1) ? 0444 : 0666; |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1295 | TRACE(file, "%s %02x\n", path, attr ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1296 | return FILE_Create( path, mode, FALSE ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | /*********************************************************************** |
| 1301 | * _lcreat_uniq (Not a Windows API) |
| 1302 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1303 | HFILE32 _lcreat_uniq( LPCSTR path, INT32 attr ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1304 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1305 | int mode = (attr & 1) ? 0444 : 0666; |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1306 | TRACE(file, "%s %02x\n", path, attr ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1307 | return FILE_Create( path, mode, TRUE ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | /*********************************************************************** |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1312 | * SetFilePointer (KERNEL32.492) |
| 1313 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1314 | DWORD WINAPI SetFilePointer( HFILE32 hFile, LONG distance, LONG *highword, |
| 1315 | DWORD method ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1316 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1317 | FILE_OBJECT *file; |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1318 | DWORD result = 0xffffffff; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (highword && *highword) |
| 1321 | { |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1322 | FIXME(file, "64-bit offsets not supported yet\n"); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1323 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1324 | return 0xffffffff; |
| 1325 | } |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1326 | TRACE(file, "handle %d offset %ld origin %ld\n", |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1327 | hFile, distance, method ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (!(file = FILE_GetFile( hFile ))) return 0xffffffff; |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1330 | |
| 1331 | |
| 1332 | /* the pointer may be positioned before the start of the file; |
| 1333 | no error is returned in that case, |
| 1334 | but subsequent attempts at I/O will produce errors. |
| 1335 | This is not allowed with Unix lseek(), |
| 1336 | so we'll need some emulation here */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1337 | switch(method) |
| 1338 | { |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1339 | case FILE_CURRENT: |
| 1340 | distance += file->pos; /* fall through */ |
| 1341 | case FILE_BEGIN: |
| 1342 | if ((result = lseek(file->unix_handle, distance, SEEK_SET)) == -1) |
| 1343 | { |
| 1344 | if ((INT32)distance < 0) |
| 1345 | file->pos = result = distance; |
| 1346 | } |
| 1347 | else |
| 1348 | file->pos = result; |
| 1349 | break; |
| 1350 | case FILE_END: |
| 1351 | if ((result = lseek(file->unix_handle, distance, SEEK_END)) == -1) |
| 1352 | { |
| 1353 | if ((INT32)distance < 0) |
| 1354 | { |
| 1355 | /* get EOF */ |
| 1356 | result = lseek(file->unix_handle, 0, SEEK_END); |
| 1357 | |
| 1358 | /* return to the old pos, as the first lseek failed */ |
| 1359 | lseek(file->unix_handle, file->pos, SEEK_END); |
| 1360 | |
| 1361 | file->pos = (result += distance); |
| 1362 | } |
| 1363 | else |
| 1364 | ERR(file, "lseek: unknown error. Please report.\n"); |
| 1365 | } |
| 1366 | else file->pos = result; |
| 1367 | break; |
| 1368 | default: |
| 1369 | ERR(file, "Unknown origin %ld !\n", method); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1372 | if (result == -1) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1373 | FILE_SetDosError(); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1374 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1375 | FILE_ReleaseFile( file ); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 1376 | return result; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1381 | * _llseek16 (KERNEL.84) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1382 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1383 | LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1384 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1385 | return SetFilePointer( HFILE16_TO_HFILE32(hFile), lOffset, NULL, nOrigin ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
| 1389 | /*********************************************************************** |
| 1390 | * _llseek32 (KERNEL32.594) |
| 1391 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1392 | LONG WINAPI _llseek32( HFILE32 hFile, LONG lOffset, INT32 nOrigin ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1393 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1394 | return SetFilePointer( hFile, lOffset, NULL, nOrigin ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1399 | * _lopen16 (KERNEL.85) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1400 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1401 | HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1402 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1403 | return HFILE32_TO_HFILE16(_lopen32( path, mode )); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | /*********************************************************************** |
| 1408 | * _lopen32 (KERNEL32.595) |
| 1409 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1410 | HFILE32 WINAPI _lopen32( LPCSTR path, INT32 mode ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1411 | { |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1412 | INT32 unixMode; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1413 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1414 | TRACE(file, "('%s',%04x)\n", path, mode ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1415 | |
Uwe Bonnes | e6b5e38 | 1998-10-18 14:48:31 +0000 | [diff] [blame] | 1416 | unixMode= FILE_DOSToUnixMode(mode); |
Uwe Bonnes | a370ab4 | 1998-10-24 09:16:44 +0000 | [diff] [blame] | 1417 | return FILE_Open( path, unixMode , (mode & 0x70)); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | /*********************************************************************** |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1422 | * _lwrite16 (KERNEL.86) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1423 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1424 | UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1425 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1426 | return (UINT16)_hwrite32( HFILE16_TO_HFILE32(hFile), buffer, (LONG)count ); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /*********************************************************************** |
| 1430 | * _lwrite32 (KERNEL.86) |
| 1431 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1432 | UINT32 WINAPI _lwrite32( HFILE32 hFile, LPCSTR buffer, UINT32 count ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1433 | { |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1434 | return (UINT32)_hwrite32( hFile, buffer, (LONG)count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1439 | * _hread16 (KERNEL.349) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1440 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1441 | LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1442 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1443 | return _lread32( HFILE16_TO_HFILE32(hFile), buffer, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | /*********************************************************************** |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1448 | * _hread32 (KERNEL32.590) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1449 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1450 | LONG WINAPI _hread32( HFILE32 hFile, LPVOID buffer, LONG count) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1451 | { |
| 1452 | return _lread32( hFile, buffer, count ); |
| 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | /*********************************************************************** |
| 1457 | * _hwrite16 (KERNEL.350) |
| 1458 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1459 | LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1460 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1461 | return _hwrite32( HFILE16_TO_HFILE32(hFile), buffer, count ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | /*********************************************************************** |
| 1466 | * _hwrite32 (KERNEL32.591) |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1467 | * |
| 1468 | * experimenation yields that _lwrite: |
| 1469 | * o truncates the file at the current position with |
| 1470 | * a 0 len write |
| 1471 | * o returns 0 on a 0 length write |
| 1472 | * o works with console handles |
| 1473 | * |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1474 | */ |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1475 | LONG WINAPI _hwrite32( HFILE32 handle, LPCSTR buffer, LONG count ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1476 | { |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1477 | K32OBJ *ioptr; |
| 1478 | DWORD result; |
| 1479 | BOOL32 status = FALSE; |
| 1480 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1481 | TRACE(file, "%d %p %ld\n", handle, buffer, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1482 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1483 | if (count == 0) { /* Expand or truncate at current position */ |
| 1484 | FILE_OBJECT *file = FILE_GetFile(handle); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1485 | |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1486 | if ( ftruncate(file->unix_handle, |
| 1487 | lseek( file->unix_handle, 0, SEEK_CUR)) == 0 ) { |
| 1488 | FILE_ReleaseFile(file); |
| 1489 | return 0; |
| 1490 | } else { |
| 1491 | FILE_SetDosError(); |
| 1492 | FILE_ReleaseFile(file); |
| 1493 | return HFILE_ERROR32; |
| 1494 | } |
| 1495 | } |
| 1496 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1497 | if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), handle, |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1498 | K32OBJ_UNKNOWN, 0, NULL ))) |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 1499 | return HFILE_ERROR32; |
| 1500 | if (K32OBJ_OPS(ioptr)->write) |
| 1501 | status = K32OBJ_OPS(ioptr)->write(ioptr, buffer, count, &result, NULL); |
| 1502 | K32OBJ_DecCount( ioptr ); |
| 1503 | if (!status) result = HFILE_ERROR32; |
| 1504 | return result; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | /*********************************************************************** |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1509 | * SetHandleCount16 (KERNEL.199) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1510 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1511 | UINT16 WINAPI SetHandleCount16( UINT16 count ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1512 | { |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 1513 | HGLOBAL16 hPDB = GetCurrentPDB(); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1514 | PDB *pdb = (PDB *)GlobalLock16( hPDB ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1515 | BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1516 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1517 | TRACE(file, "(%d)\n", count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1518 | |
| 1519 | if (count < 20) count = 20; /* No point in going below 20 */ |
| 1520 | else if (count > 254) count = 254; |
| 1521 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1522 | if (count == 20) |
| 1523 | { |
| 1524 | if (pdb->nbFiles > 20) |
| 1525 | { |
| 1526 | memcpy( pdb->fileHandles, files, 20 ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1527 | GlobalFree16( pdb->hFileHandles ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1528 | pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18, |
| 1529 | GlobalHandleToSel( hPDB ) ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1530 | pdb->hFileHandles = 0; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1531 | pdb->nbFiles = 20; |
| 1532 | } |
| 1533 | } |
| 1534 | else /* More than 20, need a new file handles table */ |
| 1535 | { |
| 1536 | BYTE *newfiles; |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 1537 | HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1538 | if (!newhandle) |
| 1539 | { |
| 1540 | DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory ); |
| 1541 | return pdb->nbFiles; |
| 1542 | } |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1543 | newfiles = (BYTE *)GlobalLock16( newhandle ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1544 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1545 | if (count > pdb->nbFiles) |
| 1546 | { |
| 1547 | memcpy( newfiles, files, pdb->nbFiles ); |
| 1548 | memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles ); |
| 1549 | } |
| 1550 | else memcpy( newfiles, files, count ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1551 | if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles ); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1552 | pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1553 | pdb->hFileHandles = newhandle; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1554 | pdb->nbFiles = count; |
| 1555 | } |
| 1556 | return pdb->nbFiles; |
| 1557 | } |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1558 | |
| 1559 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1560 | /************************************************************************* |
| 1561 | * SetHandleCount32 (KERNEL32.494) |
| 1562 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1563 | UINT32 WINAPI SetHandleCount32( UINT32 count ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1564 | { |
| 1565 | return MIN( 256, count ); |
| 1566 | } |
| 1567 | |
| 1568 | |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1569 | /*********************************************************************** |
| 1570 | * FlushFileBuffers (KERNEL32.133) |
| 1571 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1572 | BOOL32 WINAPI FlushFileBuffers( HFILE32 hFile ) |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1573 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1574 | FILE_OBJECT *file; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1575 | BOOL32 ret; |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1576 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1577 | TRACE(file, "(%d)\n", hFile ); |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1578 | if (!(file = FILE_GetFile( hFile ))) return FALSE; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1579 | if (fsync( file->unix_handle ) != -1) ret = TRUE; |
| 1580 | else |
| 1581 | { |
| 1582 | FILE_SetDosError(); |
| 1583 | ret = FALSE; |
| 1584 | } |
| 1585 | FILE_ReleaseFile( file ); |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | |
| 1590 | /************************************************************************** |
| 1591 | * SetEndOfFile (KERNEL32.483) |
| 1592 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1593 | BOOL32 WINAPI SetEndOfFile( HFILE32 hFile ) |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1594 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1595 | FILE_OBJECT *file; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1596 | BOOL32 ret = TRUE; |
| 1597 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1598 | TRACE(file, "(%d)\n", hFile ); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1599 | if (!(file = FILE_GetFile( hFile ))) return FALSE; |
| 1600 | if (ftruncate( file->unix_handle, |
| 1601 | lseek( file->unix_handle, 0, SEEK_CUR ) )) |
| 1602 | { |
| 1603 | FILE_SetDosError(); |
| 1604 | ret = FALSE; |
| 1605 | } |
| 1606 | FILE_ReleaseFile( file ); |
| 1607 | return ret; |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | |
| 1611 | /*********************************************************************** |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1612 | * DeleteFile16 (KERNEL.146) |
| 1613 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1614 | BOOL16 WINAPI DeleteFile16( LPCSTR path ) |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1615 | { |
| 1616 | return DeleteFile32A( path ); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | /*********************************************************************** |
| 1621 | * DeleteFile32A (KERNEL32.71) |
| 1622 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1623 | BOOL32 WINAPI DeleteFile32A( LPCSTR path ) |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1624 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1625 | DOS_FULL_NAME full_name; |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1626 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1627 | TRACE(file, "'%s'\n", path ); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1628 | |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 1629 | if (DOSFS_GetDevice( path )) |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1630 | { |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1631 | WARN(file, "cannot remove DOS device '%s'!\n", path); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1632 | DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk ); |
| 1633 | return FALSE; |
| 1634 | } |
| 1635 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1636 | if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE; |
| 1637 | if (unlink( full_name.long_name ) == -1) |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1638 | { |
| 1639 | FILE_SetDosError(); |
| 1640 | return FALSE; |
| 1641 | } |
| 1642 | return TRUE; |
| 1643 | } |
| 1644 | |
| 1645 | |
| 1646 | /*********************************************************************** |
| 1647 | * DeleteFile32W (KERNEL32.72) |
| 1648 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1649 | BOOL32 WINAPI DeleteFile32W( LPCWSTR path ) |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1650 | { |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1651 | LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path ); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1652 | BOOL32 ret = DeleteFile32A( xpath ); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1653 | HeapFree( GetProcessHeap(), 0, xpath ); |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 1654 | return ret; |
| 1655 | } |
| 1656 | |
| 1657 | |
| 1658 | /*********************************************************************** |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1659 | * FILE_SetFileType |
| 1660 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1661 | BOOL32 FILE_SetFileType( HFILE32 hFile, DWORD type ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1662 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1663 | FILE_OBJECT *file = FILE_GetFile( hFile ); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1664 | if (!file) return FALSE; |
| 1665 | file->type = type; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1666 | FILE_ReleaseFile( file ); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1667 | return TRUE; |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | /*********************************************************************** |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1672 | * FILE_mmap |
| 1673 | */ |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 1674 | LPVOID FILE_mmap( HFILE32 hFile, LPVOID start, |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1675 | DWORD size_high, DWORD size_low, |
| 1676 | DWORD offset_high, DWORD offset_low, |
| 1677 | int prot, int flags ) |
| 1678 | { |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 1679 | LPVOID ret; |
| 1680 | FILE_OBJECT *file = FILE_GetFile( hFile ); |
| 1681 | if (!file) return (LPVOID)-1; |
| 1682 | ret = FILE_dommap( file, start, size_high, size_low, |
| 1683 | offset_high, offset_low, prot, flags ); |
| 1684 | FILE_ReleaseFile( file ); |
| 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | |
| 1689 | /*********************************************************************** |
| 1690 | * FILE_dommap |
| 1691 | */ |
| 1692 | LPVOID FILE_dommap( FILE_OBJECT *file, LPVOID start, |
| 1693 | DWORD size_high, DWORD size_low, |
| 1694 | DWORD offset_high, DWORD offset_low, |
| 1695 | int prot, int flags ) |
| 1696 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1697 | int fd = -1; |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 1698 | int pos; |
| 1699 | LPVOID ret; |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (size_high || offset_high) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1702 | FIXME(file, "offsets larger than 4Gb not supported\n"); |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1703 | |
| 1704 | if (!file) |
| 1705 | { |
| 1706 | #ifdef MAP_ANON |
| 1707 | flags |= MAP_ANON; |
| 1708 | #else |
| 1709 | static int fdzero = -1; |
| 1710 | |
| 1711 | if (fdzero == -1) |
| 1712 | { |
| 1713 | if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1) |
| 1714 | { |
| 1715 | perror( "/dev/zero: open" ); |
| 1716 | exit(1); |
| 1717 | } |
| 1718 | } |
| 1719 | fd = fdzero; |
| 1720 | #endif /* MAP_ANON */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1721 | /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */ |
| 1722 | #ifdef MAP_SHARED |
| 1723 | flags &= ~MAP_SHARED; |
| 1724 | #endif |
| 1725 | #ifdef MAP_PRIVATE |
| 1726 | flags |= MAP_PRIVATE; |
| 1727 | #endif |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1728 | } |
| 1729 | else fd = file->unix_handle; |
| 1730 | |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 1731 | if ((ret = mmap( start, size_low, prot, |
| 1732 | flags, fd, offset_low )) != (LPVOID)-1) |
| 1733 | return ret; |
| 1734 | |
| 1735 | /* mmap() failed; if this is because the file offset is not */ |
| 1736 | /* page-aligned (EINVAL), or because the underlying filesystem */ |
| 1737 | /* does not support mmap() (ENOEXEC), we do it by hand. */ |
| 1738 | |
| 1739 | if (!file) return ret; |
| 1740 | if ((errno != ENOEXEC) && (errno != EINVAL)) return ret; |
| 1741 | if (prot & PROT_WRITE) |
| 1742 | { |
| 1743 | /* We cannot fake shared write mappings */ |
| 1744 | #ifdef MAP_SHARED |
| 1745 | if (flags & MAP_SHARED) return ret; |
| 1746 | #endif |
| 1747 | #ifdef MAP_PRIVATE |
| 1748 | if (!(flags & MAP_PRIVATE)) return ret; |
| 1749 | #endif |
| 1750 | } |
| 1751 | /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/ |
| 1752 | /* Reserve the memory with an anonymous mmap */ |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 1753 | ret = FILE_dommap( NULL, start, size_high, size_low, 0, 0, |
| 1754 | PROT_READ | PROT_WRITE, flags ); |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 1755 | if (ret == (LPVOID)-1) return ret; |
| 1756 | /* Now read in the file */ |
| 1757 | if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1) |
| 1758 | { |
| 1759 | FILE_munmap( ret, size_high, size_low ); |
| 1760 | return (LPVOID)-1; |
| 1761 | } |
| 1762 | read( fd, ret, size_low ); |
| 1763 | lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */ |
| 1764 | mprotect( ret, size_low, prot ); /* Set the right protection */ |
| 1765 | return ret; |
| 1766 | } |
| 1767 | |
| 1768 | |
| 1769 | /*********************************************************************** |
| 1770 | * FILE_munmap |
| 1771 | */ |
| 1772 | int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low ) |
| 1773 | { |
| 1774 | if (size_high) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1775 | FIXME(file, "offsets larger than 4Gb not supported\n"); |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 1776 | return munmap( start, size_low ); |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | |
| 1780 | /*********************************************************************** |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1781 | * GetFileType (KERNEL32.222) |
| 1782 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1783 | DWORD WINAPI GetFileType( HFILE32 hFile ) |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1784 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1785 | FILE_OBJECT *file = FILE_GetFile(hFile); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1786 | if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */ |
| 1787 | FILE_ReleaseFile( file ); |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 1788 | return file->type; |
| 1789 | } |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1790 | |
| 1791 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1792 | /************************************************************************** |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1793 | * MoveFileEx32A (KERNEL32.???) |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1794 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1795 | BOOL32 WINAPI MoveFileEx32A( LPCSTR fn1, LPCSTR fn2, DWORD flag ) |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1796 | { |
| 1797 | DOS_FULL_NAME full_name1, full_name2; |
| 1798 | int mode=0; /* mode == 1: use copy */ |
| 1799 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1800 | TRACE(file, "(%s,%s,%04lx)\n", fn1, fn2, flag); |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1801 | |
| 1802 | if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE; |
| 1803 | if (fn2) { /* !fn2 means delete fn1 */ |
| 1804 | if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE; |
| 1805 | /* Source name and target path are valid */ |
| 1806 | if ( full_name1.drive != full_name2.drive) |
| 1807 | /* use copy, if allowed */ |
| 1808 | if (!(flag & MOVEFILE_COPY_ALLOWED)) { |
| 1809 | /* FIXME: Use right error code */ |
| 1810 | DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk ); |
| 1811 | return FALSE; |
| 1812 | } |
| 1813 | else mode =1; |
| 1814 | if (DOSFS_GetFullName( fn2, TRUE, &full_name2 )) |
| 1815 | /* target exists, check if we may overwrite */ |
| 1816 | if (!(flag & MOVEFILE_REPLACE_EXISTING)) { |
| 1817 | /* FIXME: Use right error code */ |
| 1818 | DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk ); |
| 1819 | return FALSE; |
| 1820 | } |
| 1821 | } |
| 1822 | else /* fn2 == NULL means delete source */ |
| 1823 | if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) { |
| 1824 | if (flag & MOVEFILE_COPY_ALLOWED) { |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1825 | WARN(file, "Illegal flag\n"); |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1826 | DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, |
| 1827 | EL_Unknown ); |
| 1828 | return FALSE; |
| 1829 | } |
| 1830 | /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706) |
| 1831 | Perhaps we should queue these command and execute it |
| 1832 | when exiting... What about using on_exit(2) |
| 1833 | */ |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1834 | FIXME(file, "Please delete file '%s' when Wine has finished\n", |
| 1835 | full_name1.long_name); |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1836 | return TRUE; |
| 1837 | } |
| 1838 | else if (unlink( full_name1.long_name ) == -1) |
| 1839 | { |
| 1840 | FILE_SetDosError(); |
| 1841 | return FALSE; |
| 1842 | } |
| 1843 | else return TRUE; /* successfully deleted */ |
| 1844 | |
| 1845 | if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) { |
| 1846 | /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706) |
| 1847 | Perhaps we should queue these command and execute it |
| 1848 | when exiting... What about using on_exit(2) |
| 1849 | */ |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1850 | FIXME(file,"Please move existing file '%s' to file '%s'" |
| 1851 | "when Wine has finished\n", |
| 1852 | full_name1.long_name, full_name2.long_name); |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1853 | return TRUE; |
| 1854 | } |
| 1855 | |
| 1856 | if (!mode) /* move the file */ |
| 1857 | if (rename( full_name1.long_name, full_name2.long_name ) == -1) |
| 1858 | { |
| 1859 | FILE_SetDosError(); |
| 1860 | return FALSE; |
| 1861 | } |
| 1862 | else return TRUE; |
| 1863 | else /* copy File */ |
| 1864 | return CopyFile32A(fn1, fn2, (!(flag & MOVEFILE_REPLACE_EXISTING))); |
| 1865 | |
| 1866 | } |
| 1867 | |
| 1868 | /************************************************************************** |
| 1869 | * MoveFileEx32W (KERNEL32.???) |
| 1870 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1871 | BOOL32 WINAPI MoveFileEx32W( LPCWSTR fn1, LPCWSTR fn2, DWORD flag ) |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1872 | { |
| 1873 | LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 ); |
| 1874 | LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 ); |
| 1875 | BOOL32 res = MoveFileEx32A( afn1, afn2, flag ); |
| 1876 | HeapFree( GetProcessHeap(), 0, afn1 ); |
| 1877 | HeapFree( GetProcessHeap(), 0, afn2 ); |
| 1878 | return res; |
| 1879 | } |
| 1880 | |
| 1881 | |
| 1882 | /************************************************************************** |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1883 | * MoveFile32A (KERNEL32.387) |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1884 | * |
| 1885 | * Move file or directory |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1886 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1887 | BOOL32 WINAPI MoveFile32A( LPCSTR fn1, LPCSTR fn2 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1888 | { |
| 1889 | DOS_FULL_NAME full_name1, full_name2; |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1890 | struct stat fstat; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1891 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1892 | TRACE(file, "(%s,%s)\n", fn1, fn2 ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE; |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1895 | if (DOSFS_GetFullName( fn2, TRUE, &full_name2 )) |
| 1896 | /* The new name must not already exist */ |
| 1897 | return FALSE; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1898 | if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE; |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1899 | |
| 1900 | if (full_name1.drive == full_name2.drive) /* move */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1901 | if (rename( full_name1.long_name, full_name2.long_name ) == -1) |
| 1902 | { |
| 1903 | FILE_SetDosError(); |
| 1904 | return FALSE; |
| 1905 | } |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1906 | else return TRUE; |
| 1907 | else /*copy */ { |
| 1908 | if (stat( full_name1.long_name, &fstat )) |
| 1909 | { |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 1910 | WARN(file, "Invalid source file %s\n", |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1911 | full_name1.long_name); |
| 1912 | FILE_SetDosError(); |
| 1913 | return FALSE; |
| 1914 | } |
| 1915 | if (S_ISDIR(fstat.st_mode)) { |
| 1916 | /* No Move for directories across file systems */ |
| 1917 | /* FIXME: Use right error code */ |
| 1918 | DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, |
| 1919 | EL_Unknown ); |
| 1920 | return FALSE; |
| 1921 | } |
| 1922 | else |
| 1923 | return CopyFile32A(fn1, fn2, TRUE); /*fail, if exist */ |
| 1924 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | /************************************************************************** |
| 1929 | * MoveFile32W (KERNEL32.390) |
| 1930 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1931 | BOOL32 WINAPI MoveFile32W( LPCWSTR fn1, LPCWSTR fn2 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1932 | { |
| 1933 | LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 ); |
| 1934 | LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 ); |
| 1935 | BOOL32 res = MoveFile32A( afn1, afn2 ); |
| 1936 | HeapFree( GetProcessHeap(), 0, afn1 ); |
| 1937 | HeapFree( GetProcessHeap(), 0, afn2 ); |
| 1938 | return res; |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | /************************************************************************** |
| 1943 | * CopyFile32A (KERNEL32.36) |
| 1944 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1945 | BOOL32 WINAPI CopyFile32A( LPCSTR source, LPCSTR dest, BOOL32 fail_if_exists ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1946 | { |
| 1947 | HFILE32 h1, h2; |
| 1948 | BY_HANDLE_FILE_INFORMATION info; |
| 1949 | UINT32 count; |
| 1950 | BOOL32 ret = FALSE; |
| 1951 | int mode; |
| 1952 | char buffer[2048]; |
| 1953 | |
| 1954 | if ((h1 = _lopen32( source, OF_READ )) == HFILE_ERROR32) return FALSE; |
| 1955 | if (!GetFileInformationByHandle( h1, &info )) |
| 1956 | { |
| 1957 | CloseHandle( h1 ); |
| 1958 | return FALSE; |
| 1959 | } |
| 1960 | mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666; |
| 1961 | if ((h2 = FILE_Create( dest, mode, fail_if_exists )) == HFILE_ERROR32) |
| 1962 | { |
| 1963 | CloseHandle( h1 ); |
| 1964 | return FALSE; |
| 1965 | } |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1966 | while ((count = _lread32( h1, buffer, sizeof(buffer) )) > 0) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1967 | { |
| 1968 | char *p = buffer; |
| 1969 | while (count > 0) |
| 1970 | { |
| 1971 | INT32 res = _lwrite32( h2, p, count ); |
| 1972 | if (res <= 0) goto done; |
| 1973 | p += res; |
| 1974 | count -= res; |
| 1975 | } |
| 1976 | } |
| 1977 | ret = TRUE; |
| 1978 | done: |
| 1979 | CloseHandle( h1 ); |
| 1980 | CloseHandle( h2 ); |
| 1981 | return ret; |
| 1982 | } |
| 1983 | |
| 1984 | |
| 1985 | /************************************************************************** |
| 1986 | * CopyFile32W (KERNEL32.37) |
| 1987 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1988 | BOOL32 WINAPI CopyFile32W( LPCWSTR source, LPCWSTR dest, BOOL32 fail_if_exists) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1989 | { |
| 1990 | LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source ); |
| 1991 | LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest ); |
| 1992 | BOOL32 ret = CopyFile32A( sourceA, destA, fail_if_exists ); |
| 1993 | HeapFree( GetProcessHeap(), 0, sourceA ); |
| 1994 | HeapFree( GetProcessHeap(), 0, destA ); |
| 1995 | return ret; |
| 1996 | } |
| 1997 | |
| 1998 | |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 1999 | /*********************************************************************** |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 2000 | * SetFileTime (KERNEL32.650) |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2001 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 2002 | BOOL32 WINAPI SetFileTime( HFILE32 hFile, |
| 2003 | const FILETIME *lpCreationTime, |
| 2004 | const FILETIME *lpLastAccessTime, |
| 2005 | const FILETIME *lpLastWriteTime ) |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2006 | { |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 2007 | FILE_OBJECT *file = FILE_GetFile(hFile); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2008 | struct utimbuf utimbuf; |
| 2009 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 2010 | if (!file) return FILE_TYPE_UNKNOWN; /* FIXME: correct? */ |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 2011 | TRACE(file,"('%s',%p,%p,%p)\n", |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2012 | file->unix_name, |
| 2013 | lpCreationTime, |
| 2014 | lpLastAccessTime, |
| 2015 | lpLastWriteTime |
| 2016 | ); |
| 2017 | if (lpLastAccessTime) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 2018 | utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime, NULL); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2019 | else |
| 2020 | utimbuf.actime = 0; /* FIXME */ |
| 2021 | if (lpLastWriteTime) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 2022 | utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime, NULL); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2023 | else |
| 2024 | utimbuf.modtime = 0; /* FIXME */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 2025 | if (-1==utime(file->unix_name,&utimbuf)) |
| 2026 | { |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 2027 | MSG("Couldn't set the time for file '%s'. Insufficient permissions !?\n", file->unix_name); |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 2028 | FILE_ReleaseFile( file ); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2029 | FILE_SetDosError(); |
| 2030 | return FALSE; |
| 2031 | } |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 2032 | FILE_ReleaseFile( file ); |
Alexandre Julliard | 75d86e1 | 1996-11-17 18:59:11 +0000 | [diff] [blame] | 2033 | return TRUE; |
| 2034 | } |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2035 | |
| 2036 | /* Locks need to be mirrored because unix file locking is based |
| 2037 | * on the pid. Inside of wine there can be multiple WINE processes |
| 2038 | * that share the same unix pid. |
| 2039 | * Read's and writes should check these locks also - not sure |
| 2040 | * how critical that is at this point (FIXME). |
| 2041 | */ |
| 2042 | |
| 2043 | static BOOL32 DOS_AddLock(FILE_OBJECT *file, struct flock *f) |
| 2044 | { |
| 2045 | DOS_FILE_LOCK *curr; |
| 2046 | DWORD processId; |
| 2047 | |
| 2048 | processId = GetCurrentProcessId(); |
| 2049 | |
| 2050 | /* check if lock overlaps a current lock for the same file */ |
| 2051 | for (curr = locks; curr; curr = curr->next) { |
| 2052 | if (strcmp(curr->unix_name, file->unix_name) == 0) { |
Alexandre Julliard | 44ed71f | 1997-12-21 19:17:50 +0000 | [diff] [blame] | 2053 | if ((f->l_start == curr->base) && (f->l_len == curr->len)) |
| 2054 | return TRUE;/* region is identic */ |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2055 | if ((f->l_start < (curr->base + curr->len)) && |
| 2056 | ((f->l_start + f->l_len) > curr->base)) { |
| 2057 | /* region overlaps */ |
| 2058 | return FALSE; |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | curr = HeapAlloc( SystemHeap, 0, sizeof(DOS_FILE_LOCK) ); |
| 2064 | curr->processId = GetCurrentProcessId(); |
| 2065 | curr->base = f->l_start; |
| 2066 | curr->len = f->l_len; |
| 2067 | curr->unix_name = HEAP_strdupA( SystemHeap, 0, file->unix_name); |
| 2068 | curr->next = locks; |
| 2069 | curr->dos_file = file; |
| 2070 | locks = curr; |
| 2071 | return TRUE; |
| 2072 | } |
| 2073 | |
| 2074 | static void DOS_RemoveFileLocks(FILE_OBJECT *file) |
| 2075 | { |
| 2076 | DWORD processId; |
| 2077 | DOS_FILE_LOCK **curr; |
| 2078 | DOS_FILE_LOCK *rem; |
| 2079 | |
| 2080 | processId = GetCurrentProcessId(); |
| 2081 | curr = &locks; |
| 2082 | while (*curr) { |
| 2083 | if ((*curr)->dos_file == file) { |
| 2084 | rem = *curr; |
| 2085 | *curr = (*curr)->next; |
| 2086 | HeapFree( SystemHeap, 0, rem->unix_name ); |
| 2087 | HeapFree( SystemHeap, 0, rem ); |
| 2088 | } |
| 2089 | else |
| 2090 | curr = &(*curr)->next; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | static BOOL32 DOS_RemoveLock(FILE_OBJECT *file, struct flock *f) |
| 2095 | { |
| 2096 | DWORD processId; |
| 2097 | DOS_FILE_LOCK **curr; |
| 2098 | DOS_FILE_LOCK *rem; |
| 2099 | |
| 2100 | processId = GetCurrentProcessId(); |
| 2101 | for (curr = &locks; *curr; curr = &(*curr)->next) { |
| 2102 | if ((*curr)->processId == processId && |
| 2103 | (*curr)->dos_file == file && |
| 2104 | (*curr)->base == f->l_start && |
| 2105 | (*curr)->len == f->l_len) { |
| 2106 | /* this is the same lock */ |
| 2107 | rem = *curr; |
| 2108 | *curr = (*curr)->next; |
| 2109 | HeapFree( SystemHeap, 0, rem->unix_name ); |
| 2110 | HeapFree( SystemHeap, 0, rem ); |
| 2111 | return TRUE; |
| 2112 | } |
| 2113 | } |
| 2114 | /* no matching lock found */ |
| 2115 | return FALSE; |
| 2116 | } |
| 2117 | |
| 2118 | |
| 2119 | /************************************************************************** |
| 2120 | * LockFile (KERNEL32.511) |
| 2121 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 2122 | BOOL32 WINAPI LockFile( |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2123 | HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh, |
| 2124 | DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh ) |
| 2125 | { |
| 2126 | struct flock f; |
| 2127 | FILE_OBJECT *file; |
| 2128 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 2129 | TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n", |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2130 | hFile, dwFileOffsetLow, dwFileOffsetHigh, |
| 2131 | nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh); |
| 2132 | |
| 2133 | if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) { |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 2134 | FIXME(file, "Unimplemented bytes > 32bits\n"); |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2135 | return FALSE; |
| 2136 | } |
| 2137 | |
| 2138 | f.l_start = dwFileOffsetLow; |
| 2139 | f.l_len = nNumberOfBytesToLockLow; |
| 2140 | f.l_whence = SEEK_SET; |
| 2141 | f.l_pid = 0; |
| 2142 | f.l_type = F_WRLCK; |
| 2143 | |
| 2144 | if (!(file = FILE_GetFile(hFile))) return FALSE; |
| 2145 | |
| 2146 | /* shadow locks internally */ |
| 2147 | if (!DOS_AddLock(file, &f)) { |
| 2148 | DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk ); |
| 2149 | return FALSE; |
| 2150 | } |
| 2151 | |
| 2152 | /* FIXME: Unix locking commented out for now, doesn't work with Excel */ |
| 2153 | #ifdef USE_UNIX_LOCKS |
| 2154 | if (fcntl(file->unix_handle, F_SETLK, &f) == -1) { |
| 2155 | if (errno == EACCES || errno == EAGAIN) { |
| 2156 | DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Ignore, EL_Disk ); |
| 2157 | } |
| 2158 | else { |
| 2159 | FILE_SetDosError(); |
| 2160 | } |
| 2161 | /* remove our internal copy of the lock */ |
| 2162 | DOS_RemoveLock(file, &f); |
| 2163 | return FALSE; |
| 2164 | } |
| 2165 | #endif |
| 2166 | return TRUE; |
| 2167 | } |
| 2168 | |
| 2169 | |
| 2170 | /************************************************************************** |
| 2171 | * UnlockFile (KERNEL32.703) |
| 2172 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 2173 | BOOL32 WINAPI UnlockFile( |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2174 | HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh, |
| 2175 | DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh ) |
| 2176 | { |
| 2177 | FILE_OBJECT *file; |
| 2178 | struct flock f; |
| 2179 | |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 2180 | TRACE(file, "handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n", |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2181 | hFile, dwFileOffsetLow, dwFileOffsetHigh, |
| 2182 | nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh); |
| 2183 | |
| 2184 | if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) { |
Alexandre Julliard | a69b88b | 1998-03-15 20:29:56 +0000 | [diff] [blame] | 2185 | WARN(file, "Unimplemented bytes > 32bits\n"); |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 2186 | return FALSE; |
| 2187 | } |
| 2188 | |
| 2189 | f.l_start = dwFileOffsetLow; |
| 2190 | f.l_len = nNumberOfBytesToUnlockLow; |
| 2191 | f.l_whence = SEEK_SET; |
| 2192 | f.l_pid = 0; |
| 2193 | f.l_type = F_UNLCK; |
| 2194 | |
| 2195 | if (!(file = FILE_GetFile(hFile))) return FALSE; |
| 2196 | |
| 2197 | DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */ |
| 2198 | |
| 2199 | /* FIXME: Unix locking commented out for now, doesn't work with Excel */ |
| 2200 | #ifdef USE_UNIX_LOCKS |
| 2201 | if (fcntl(file->unix_handle, F_SETLK, &f) == -1) { |
| 2202 | FILE_SetDosError(); |
| 2203 | return FALSE; |
| 2204 | } |
| 2205 | #endif |
| 2206 | return TRUE; |
| 2207 | } |
| 2208 | |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 2209 | /************************************************************************** |
| 2210 | * GetFileAttributesEx32A [KERNEL32.874] |
| 2211 | */ |
| 2212 | BOOL32 WINAPI GetFileAttributesEx32A( |
| 2213 | LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, |
| 2214 | LPVOID lpFileInformation) |
| 2215 | { |
| 2216 | DOS_FULL_NAME full_name; |
| 2217 | BY_HANDLE_FILE_INFORMATION info; |
| 2218 | |
| 2219 | if (lpFileName == NULL) return FALSE; |
| 2220 | if (lpFileInformation == NULL) return FALSE; |
| 2221 | |
| 2222 | if (fInfoLevelId == GetFileExInfoStandard) { |
| 2223 | LPWIN32_FILE_ATTRIBUTE_DATA lpFad = |
| 2224 | (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation; |
| 2225 | if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE; |
| 2226 | if (!FILE_Stat( full_name.long_name, &info )) return FALSE; |
| 2227 | |
| 2228 | lpFad->dwFileAttributes = info.dwFileAttributes; |
| 2229 | lpFad->ftCreationTime = info.ftCreationTime; |
| 2230 | lpFad->ftLastAccessTime = info.ftLastAccessTime; |
| 2231 | lpFad->ftLastWriteTime = info.ftLastWriteTime; |
| 2232 | lpFad->nFileSizeHigh = info.nFileSizeHigh; |
| 2233 | lpFad->nFileSizeLow = info.nFileSizeLow; |
| 2234 | } |
| 2235 | else { |
| 2236 | FIXME (file, "invalid info level %d!\n", fInfoLevelId); |
| 2237 | return FALSE; |
| 2238 | } |
| 2239 | |
| 2240 | return TRUE; |
| 2241 | } |
| 2242 | |
| 2243 | |
| 2244 | /************************************************************************** |
| 2245 | * GetFileAttributesEx32W [KERNEL32.875] |
| 2246 | */ |
| 2247 | BOOL32 WINAPI GetFileAttributesEx32W( |
| 2248 | LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, |
| 2249 | LPVOID lpFileInformation) |
| 2250 | { |
| 2251 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName ); |
| 2252 | BOOL32 res = |
| 2253 | GetFileAttributesEx32A( nameA, fInfoLevelId, lpFileInformation); |
| 2254 | HeapFree( GetProcessHeap(), 0, nameA ); |
| 2255 | return res; |
| 2256 | } |
| 2257 | |
| 2258 | |