- Reimplement RtlSetCurrentDirectory_U. This fixes bugs & implements references count
- Fix the FIXME in RtlpDosPathNameToRelativeNtPathName_Ustr by incrementing references count

svn path=/trunk/; revision=55017
This commit is contained in:
Pierre Schweitzer 2012-01-20 20:21:16 +00:00
parent 823685091e
commit f65034e760

View file

@ -6,6 +6,8 @@
* PROGRAMMERS: Wine team * PROGRAMMERS: Wine team
* Thomas Weidenmueller * Thomas Weidenmueller
* Gunnar Dalsnes * Gunnar Dalsnes
* Alex Ionescu (alex.ionescu@reactos.org)
* Pierre Schweitzer (pierre@reactos.org)
*/ */
/* INCLUDES *****************************************************************/ /* INCLUDES *****************************************************************/
@ -21,6 +23,11 @@
#define IS_PATH_SEPARATOR(x) (((x)==L'\\')||((x)==L'/')) #define IS_PATH_SEPARATOR(x) (((x)==L'\\')||((x)==L'/'))
#define RTL_CURDIR_IS_REMOVABLE 0x1
#define RTL_CURDIR_DROP_OLD_HANDLE 0x2
#define RTL_CURDIR_ALL_FLAGS (RTL_CURDIR_DROP_OLD_HANDLE | RTL_CURDIR_IS_REMOVABLE) // 0x3
C_ASSERT(RTL_CURDIR_ALL_FLAGS == OBJ_HANDLE_TAGBITS);
/* GLOBALS ********************************************************************/ /* GLOBALS ********************************************************************/
@ -617,7 +624,7 @@ RtlpDosPathNameToRelativeNtPathName_Ustr(IN BOOLEAN HaveRelative,
if (InputPathType == RtlPathTypeRelative) if (InputPathType == RtlPathTypeRelative)
{ {
/* Get current directory */ /* Get current directory */
CurrentDirectory = (PCURDIR)&(NtCurrentPeb ()->ProcessParameters->CurrentDirectory.DosPath); CurrentDirectory = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);
if (CurrentDirectory->Handle) if (CurrentDirectory->Handle)
{ {
Status = RtlInitUnicodeStringEx(&FullPath, Buffer); Status = RtlInitUnicodeStringEx(&FullPath, Buffer);
@ -660,7 +667,7 @@ RtlpDosPathNameToRelativeNtPathName_Ustr(IN BOOLEAN HaveRelative,
RelativeName->CurDirRef = RtlpCurDirRef; RelativeName->CurDirRef = RtlpCurDirRef;
if (RelativeName->CurDirRef) if (RelativeName->CurDirRef)
{ {
/* FIXME: Increment reference count */ InterlockedIncrement(&RtlpCurDirRef->RefCount);
} }
RelativeName->ContainingDirectory = CurrentDirectory->Handle; RelativeName->ContainingDirectory = CurrentDirectory->Handle;
@ -831,8 +838,13 @@ RtlReleaseRelativeName(IN PRTL_RELATIVE_NAME_U RelativeName)
/* Check if a directory reference was grabbed */ /* Check if a directory reference was grabbed */
if (RelativeName->CurDirRef) if (RelativeName->CurDirRef)
{ {
/* FIXME: Not yet supported */ /* Decrease reference count */
UNIMPLEMENTED; if (!InterlockedDecrement(&RelativeName->CurDirRef->RefCount))
{
/* If no one uses it any longer, close handle & free */
NtClose(RelativeName->CurDirRef->Handle);
RtlFreeHeap(RtlGetProcessHeap(), 0, RelativeName->CurDirRef);
}
RelativeName->CurDirRef = NULL; RelativeName->CurDirRef = NULL;
} }
} }
@ -984,89 +996,201 @@ RtlGetCurrentDirectory_U(IN ULONG MaximumLength,
/* /*
* @implemented * @implemented
*/ */
NTSTATUS NTAPI NTSTATUS
RtlSetCurrentDirectory_U(PUNICODE_STRING dir) NTAPI
RtlSetCurrentDirectory_U(IN PUNICODE_STRING Path)
{ {
UNICODE_STRING full; PCURDIR CurDir;
FILE_FS_DEVICE_INFORMATION device_info; NTSTATUS Status;
OBJECT_ATTRIBUTES Attr; RTL_PATH_TYPE PathType;
IO_STATUS_BLOCK iosb; IO_STATUS_BLOCK IoStatusBlock;
PCURDIR cd; UNICODE_STRING FullPath, NtName;
NTSTATUS Status; PRTLP_CURDIR_REF OldCurDir = NULL;
USHORT size; OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE handle = NULL; FILE_FS_DEVICE_INFORMATION FileFsDeviceInfo;
PWSTR ptr; ULONG SavedLength, CharLength, FullPathLength;
HANDLE OldHandle = 0, CurDirHandle, OldCurDirHandle = 0;
DPRINT("RtlSetCurrentDirectory %wZ\n", dir); DPRINT("RtlSetCurrentDirectory_U %wZ\n", Path);
full.Buffer = NULL; /* Can't set current directory on DOS device */
if (RtlIsDosDeviceName_Ustr(Path))
{
return STATUS_NOT_A_DIRECTORY;
}
RtlAcquirePebLock (); /* Get current directory */
RtlAcquirePebLock();
CurDir = &NtCurrentPeb()->ProcessParameters->CurrentDirectory;
cd = (PCURDIR)&NtCurrentPeb ()->ProcessParameters->CurrentDirectory.DosPath; /* Check if we have to drop current handle */
if (((ULONG_PTR)(CurDir->Handle) & RTL_CURDIR_ALL_FLAGS) == RTL_CURDIR_DROP_OLD_HANDLE)
{
OldHandle = CurDir->Handle;
CurDir->Handle = NULL;
}
if (!RtlDosPathNameToNtPathName_U (dir->Buffer, &full, 0, 0)) /* Allocate a buffer for full path (using max possible length */
{ FullPath.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, CurDir->DosPath.MaximumLength);
RtlReleasePebLock (); if (!FullPath.Buffer)
return STATUS_OBJECT_NAME_INVALID; {
} Status = STATUS_NO_MEMORY;
goto Leave;
}
DPRINT("RtlSetCurrentDirectory: full %wZ\n",&full); /* Init string */
FullPath.Length = 0;
FullPath.MaximumLength = CurDir->DosPath.MaximumLength;
InitializeObjectAttributes (&Attr, /* Get new directory full path */
&full, FullPathLength = RtlGetFullPathName_Ustr(Path, FullPath.MaximumLength, FullPath.Buffer, NULL, NULL, &PathType);
OBJ_CASE_INSENSITIVE | OBJ_INHERIT, if (!FullPathLength)
NULL, {
NULL); Status = STATUS_OBJECT_NAME_INVALID;
goto Leave;
}
Status = ZwOpenFile (&handle, SavedLength = FullPath.MaximumLength;
SYNCHRONIZE | FILE_TRAVERSE, CharLength = FullPathLength / sizeof(WCHAR);
&Attr,
&iosb,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(Status)) if (FullPathLength > FullPath.MaximumLength)
{ {
RtlFreeUnicodeString( &full); Status = STATUS_NAME_TOO_LONG;
RtlReleasePebLock (); goto Leave;
return Status; }
}
/* don't keep the directory handle open on removable media */ /* Translate it to NT name */
if (NT_SUCCESS(ZwQueryVolumeInformationFile( handle, &iosb, &device_info, if (!RtlDosPathNameToNtPathName_U(FullPath.Buffer, &NtName, NULL, NULL))
sizeof(device_info), FileFsDeviceInformation )) && {
(device_info.Characteristics & FILE_REMOVABLE_MEDIA)) Status = STATUS_OBJECT_NAME_INVALID;
{ goto Leave;
DPRINT1("don't keep the directory handle open on removable media\n"); }
ZwClose( handle );
handle = 0;
}
if (cd->Handle) InitializeObjectAttributes(&ObjectAttributes, &NtName,
ZwClose(cd->Handle); OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
cd->Handle = handle; NULL, NULL);
/* append trailing \ if missing */ /* If previous current directory was removable, then check it for dropping */
size = full.Length / sizeof(WCHAR); if (((ULONG_PTR)(CurDir->Handle) & RTL_CURDIR_ALL_FLAGS) == RTL_CURDIR_ALL_FLAGS)
ptr = full.Buffer; {
ptr += 4; /* skip \??\ prefix */ /* Get back normal handle */
size -= 4; CurDirHandle = (HANDLE)((ULONG_PTR)(CurDir->Handle) & ~RTL_CURDIR_ALL_FLAGS);
CurDir->Handle = 0;
/* This is ok because RtlDosPathNameToNtPathName_U returns a nullterminated string. /* Get device information */
* So the nullterm is replaced with \ Status = NtQueryVolumeInformationFile(CurDirHandle,
* -Gunnar &IoStatusBlock,
*/ &FileFsDeviceInfo,
if (size && ptr[size - 1] != '\\') ptr[size++] = '\\'; sizeof(FileFsDeviceInfo),
FileFsDeviceInformation);
/* Retry without taking care of removable device */
if (!NT_SUCCESS(Status))
{
Status = RtlSetCurrentDirectory_U(Path);
goto Leave;
}
}
else
{
/* Open directory */
Status = NtOpenFile(&CurDirHandle,
SYNCHRONIZE | FILE_TRAVERSE,
&ObjectAttributes,
&IoStatusBlock,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(Status)) goto Leave;
memcpy( cd->DosPath.Buffer, ptr, size * sizeof(WCHAR)); /* Get device information */
cd->DosPath.Buffer[size] = 0; Status = NtQueryVolumeInformationFile(CurDirHandle,
cd->DosPath.Length = size * sizeof(WCHAR); &IoStatusBlock,
&FileFsDeviceInfo,
sizeof(FileFsDeviceInfo),
FileFsDeviceInformation);
if (!NT_SUCCESS(Status)) goto Leave;
}
RtlFreeUnicodeString( &full); /* If device is removable, mark handle */
RtlReleasePebLock(); if (FileFsDeviceInfo.Characteristics & FILE_REMOVABLE_MEDIA)
{
CurDirHandle = (HANDLE)((ULONG_PTR)CurDirHandle | RTL_CURDIR_IS_REMOVABLE);
}
return STATUS_SUCCESS; FullPath.Length = FullPathLength;
/* If full path isn't \ terminated, do it */
if (FullPath.Buffer[CharLength - 1] != L'\\')
{
if ((CharLength + 1) * sizeof(WCHAR) > SavedLength)
{
Status = STATUS_NAME_TOO_LONG;
goto Leave;
}
FullPath.Buffer[CharLength] = L'\\';
FullPath.Buffer[CharLength + 1] = UNICODE_NULL;
FullPath.Length += sizeof(WCHAR);
}
/* If we have previous current directory with only us as reference, save it */
if (RtlpCurDirRef != NULL && RtlpCurDirRef->RefCount == 1)
{
OldCurDirHandle = RtlpCurDirRef->Handle;
}
else
{
/* Allocate new current directory struct saving previous one */
OldCurDir = RtlpCurDirRef;
RtlpCurDirRef = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(RTLP_CURDIR_REF));
if (!RtlpCurDirRef)
{
RtlpCurDirRef = OldCurDir;
OldCurDir = NULL;
Status = STATUS_NO_MEMORY;
goto Leave;
}
/* Set reference to 1 (us) */
RtlpCurDirRef->RefCount = 1;
}
/* Save new data */
CurDir->Handle = CurDirHandle;
RtlpCurDirRef->Handle = CurDirHandle;
CurDirHandle = 0;
/* Copy full path */
RtlCopyMemory(CurDir->DosPath.Buffer, FullPath.Buffer, FullPath.Length + sizeof(WCHAR));
CurDir->DosPath.Length = FullPath.Length;
Status = STATUS_SUCCESS;
Leave:
RtlReleasePebLock();
if (FullPath.Buffer)
{
RtlFreeHeap(RtlGetProcessHeap(), 0, FullPath.Buffer);
}
if (NtName.Buffer)
{
RtlFreeHeap(RtlGetProcessHeap(), 0, NtName.Buffer);
}
if (CurDirHandle) NtClose(CurDirHandle);
if (OldHandle) NtClose(OldHandle);
if (OldCurDirHandle) NtClose(OldCurDirHandle);
if (OldCurDir && InterlockedDecrement(&OldCurDir->RefCount) == 0)
{
NtClose(OldCurDir->Handle);
RtlFreeHeap(RtlGetProcessHeap(), 0, OldCurDir);
}
return Status;
} }