2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
41 #include "wine/winbase16.h"
43 #include "wine/winuser16.h"
47 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
52 WINE_DECLARE_DEBUG_CHANNEL(file
);
54 static WCHAR
*DIR_Windows
;
55 static WCHAR
*DIR_System
;
57 /***********************************************************************
60 * Get a path name from the wine.ini file and make sure it is valid.
62 static WCHAR
*DIR_GetPath( HKEY hkey
, LPCWSTR keyname
, LPCWSTR defval
, BOOL warn
)
66 WCHAR tmp
[MAX_PATHNAME_LEN
];
72 RtlInitUnicodeString( &nameW
, keyname
);
73 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
74 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
78 attr
= GetFileAttributesW( path
);
79 if (attr
== INVALID_FILE_ATTRIBUTES
) mess
= "does not exist";
80 else if (!(attr
& FILE_ATTRIBUTE_DIRECTORY
)) mess
= "not a directory";
83 DWORD len
= GetFullPathNameW( path
, 0, NULL
, NULL
);
84 ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
85 if (ret
) GetFullPathNameW( path
, len
, ret
, NULL
);
91 MESSAGE("Invalid path %s for %s directory: %s.\n",
92 debugstr_w(path
), debugstr_w(keyname
), mess
);
93 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
94 wine_get_config_dir());
100 /***********************************************************************
105 OBJECT_ATTRIBUTES attr
;
106 UNICODE_STRING nameW
;
108 char path
[MAX_PATHNAME_LEN
];
109 WCHAR longpath
[MAX_PATHNAME_LEN
];
110 WCHAR
*tmp_dir
, *profile_dir
;
113 static const WCHAR wineW
[] = {'M','a','c','h','i','n','e','\\',
114 'S','o','f','t','w','a','r','e','\\',
115 'W','i','n','e','\\','W','i','n','e','\\',
116 'C','o','n','f','i','g','\\','W','i','n','e',0};
117 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
118 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
119 static const WCHAR tempW
[] = {'t','e','m','p',0};
120 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
121 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
122 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
123 static const WCHAR temp_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','t','e','m','p',0};
124 static const WCHAR pathW
[] = {'p','a','t','h',0};
125 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
126 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
127 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
128 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
129 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
130 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
131 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
132 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
133 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
134 static const WCHAR wcmdW
[] = {'\\','w','c','m','d','.','e','x','e',0};
135 static const WCHAR comspecW
[] = {'C','O','M','S','P','E','C',0};
136 static const WCHAR empty_strW
[] = { 0 };
138 if (!getcwd( path
, MAX_PATHNAME_LEN
))
140 perror( "Could not get current directory" );
144 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
146 MESSAGE("Warning: could not find wine config [Drive x] entry "
147 "for current working directory %s; "
148 "starting in windows directory.\n", cwd
);
152 longpath
[0] = 'a' + drive
;
154 MultiByteToWideChar(CP_UNIXCP
, 0, cwd
, -1, longpath
+ 2, MAX_PATHNAME_LEN
);
155 SetCurrentDirectoryW( longpath
);
156 if(GetDriveTypeW(longpath
)==DRIVE_CDROM
)
157 chdir("/"); /* change to root directory so as not to lock cdroms */
160 attr
.Length
= sizeof(attr
);
161 attr
.RootDirectory
= 0;
162 attr
.ObjectName
= &nameW
;
164 attr
.SecurityDescriptor
= NULL
;
165 attr
.SecurityQualityOfService
= NULL
;
167 RtlInitUnicodeString( &nameW
, wineW
);
168 if (NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) hkey
= 0;
170 if (!(DIR_Windows
= DIR_GetPath( hkey
, windowsW
, windows_dirW
, TRUE
)) ||
171 !(DIR_System
= DIR_GetPath( hkey
, systemW
, system_dirW
, TRUE
)) ||
172 !(tmp_dir
= DIR_GetPath( hkey
, tempW
, temp_dirW
, TRUE
)))
174 if (hkey
) NtClose( hkey
);
178 if (drive
== -1) SetCurrentDirectoryW( DIR_Windows
);
180 /* Set the environment variables */
182 /* set COMSPEC only if it doesn't exist already */
183 if (!GetEnvironmentVariableW( comspecW
, NULL
, 0 ))
185 strcpyW( longpath
, DIR_System
);
186 strcatW( longpath
, wcmdW
);
187 SetEnvironmentVariableW( comspecW
, longpath
);
190 /* set PATH only if not set already */
191 if (!GetEnvironmentVariableW( path_capsW
, NULL
, 0 ))
193 WCHAR tmp
[MAX_PATHNAME_LEN
];
195 const WCHAR
*path
= path_dirW
;
197 RtlInitUnicodeString( &nameW
, pathW
);
198 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
199 tmp
, sizeof(tmp
), &dummy
))
201 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
204 if (strchrW(path
, '/'))
206 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
209 SetEnvironmentVariableW( path_capsW
, path
);
210 TRACE("Path = %s\n", debugstr_w(path
) );
213 if (!GetEnvironmentVariableW( temp_capsW
, NULL
, 0 ))
214 SetEnvironmentVariableW( temp_capsW
, tmp_dir
);
215 if (!GetEnvironmentVariableW( tmp_capsW
, NULL
, 0 ))
216 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
);
218 SetEnvironmentVariableW( windirW
, DIR_Windows
);
219 SetEnvironmentVariableW( systemrootW
, DIR_Windows
);
220 SetEnvironmentVariableW( winsysdirW
, DIR_System
);
222 TRACE("WindowsDir = %s\n", debugstr_w(DIR_Windows
) );
223 TRACE("SystemDir = %s\n", debugstr_w(DIR_System
) );
224 TRACE("TempDir = %s\n", debugstr_w(tmp_dir
) );
225 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
) );
226 TRACE("Cwd = %c:\\%s\n",
227 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
229 HeapFree( GetProcessHeap(), 0, tmp_dir
);
231 if ((profile_dir
= DIR_GetPath( hkey
, profileW
, empty_strW
, FALSE
)))
233 TRACE("USERPROFILE= %s\n", debugstr_w(profile_dir
) );
234 SetEnvironmentVariableW( userprofileW
, profile_dir
);
235 HeapFree( GetProcessHeap(), 0, profile_dir
);
238 if (hkey
) NtClose( hkey
);
244 /***********************************************************************
245 * GetWindowsDirectoryW (KERNEL32.@)
247 * See comment for GetWindowsDirectoryA.
249 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
251 UINT len
= strlenW( DIR_Windows
) + 1;
252 if (path
&& count
>= len
)
254 strcpyW( path
, DIR_Windows
);
261 /***********************************************************************
262 * GetWindowsDirectoryA (KERNEL32.@)
265 * If buffer is large enough to hold full path and terminating '\0' character
266 * function copies path to buffer and returns length of the path without '\0'.
267 * Otherwise function returns required size including '\0' character and
268 * does not touch the buffer.
270 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
272 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
, -1, NULL
, 0, NULL
, NULL
);
273 if (path
&& count
>= len
)
275 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
, -1, path
, count
, NULL
, NULL
);
282 /***********************************************************************
283 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
285 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
287 return GetWindowsDirectoryA( path
, count
);
291 /***********************************************************************
292 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
294 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
296 return GetWindowsDirectoryW( path
, count
);
300 /***********************************************************************
301 * GetSystemDirectoryW (KERNEL32.@)
303 * See comment for GetWindowsDirectoryA.
305 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
307 UINT len
= strlenW( DIR_System
) + 1;
308 if (path
&& count
>= len
)
310 strcpyW( path
, DIR_System
);
317 /***********************************************************************
318 * GetSystemDirectoryA (KERNEL32.@)
320 * See comment for GetWindowsDirectoryA.
322 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
324 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
, -1, NULL
, 0, NULL
, NULL
);
325 if (path
&& count
>= len
)
327 WideCharToMultiByte( CP_ACP
, 0, DIR_System
, -1, path
, count
, NULL
, NULL
);
334 /***********************************************************************
335 * CreateDirectoryW (KERNEL32.@)
339 * ERROR_DISK_FULL: on full disk
340 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
341 * ERROR_ACCESS_DENIED: on permission problems
342 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
344 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
345 LPSECURITY_ATTRIBUTES lpsecattribs
)
347 DOS_FULL_NAME full_name
;
351 SetLastError(ERROR_PATH_NOT_FOUND
);
355 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
357 if (RtlIsDosDeviceName_U( path
))
359 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
360 SetLastError( ERROR_ACCESS_DENIED
);
363 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
364 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
365 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
366 /* the FILE_SetDosError() generated error codes don't match the
367 * CreateDirectory ones for some errnos */
371 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
372 SetLastError(ERROR_ACCESS_DENIED
);
374 SetLastError(ERROR_ALREADY_EXISTS
);
377 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
378 default: FILE_SetDosError();break;
386 /***********************************************************************
387 * CreateDirectoryA (KERNEL32.@)
389 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
390 LPSECURITY_ATTRIBUTES lpsecattribs
)
392 UNICODE_STRING pathW
;
397 SetLastError(ERROR_PATH_NOT_FOUND
);
401 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
403 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
404 RtlFreeUnicodeString(&pathW
);
407 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
412 /***********************************************************************
413 * CreateDirectoryExA (KERNEL32.@)
415 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
416 LPSECURITY_ATTRIBUTES lpsecattribs
)
418 return CreateDirectoryA(path
,lpsecattribs
);
422 /***********************************************************************
423 * CreateDirectoryExW (KERNEL32.@)
425 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
426 LPSECURITY_ATTRIBUTES lpsecattribs
)
428 return CreateDirectoryW(path
,lpsecattribs
);
432 /***********************************************************************
433 * RemoveDirectoryW (KERNEL32.@)
435 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
437 DOS_FULL_NAME full_name
;
441 SetLastError(ERROR_INVALID_PARAMETER
);
445 TRACE_(file
)("%s\n", debugstr_w(path
));
447 if (RtlIsDosDeviceName_U( path
))
449 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
450 SetLastError( ERROR_FILE_NOT_FOUND
);
453 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
454 if (rmdir( full_name
.long_name
) == -1)
463 /***********************************************************************
464 * RemoveDirectoryA (KERNEL32.@)
466 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
468 UNICODE_STRING pathW
;
473 SetLastError(ERROR_INVALID_PARAMETER
);
477 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
479 ret
= RemoveDirectoryW(pathW
.Buffer
);
480 RtlFreeUnicodeString(&pathW
);
483 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);