/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Reads or writes specified number of sectors from/to specified buffer. // static BOOL GetDriveGeometry( PWCHAR Drive, // drive name to read/write sectors from/to PDISK_GEOMETRY DiskGeometry ) { BOOL Ret = FALSE; HANDLE hDrive; NTSTATUS ntStatus; OBJECT_ATTRIBUTES oa = {0}; UNICODE_STRING us; IO_STATUS_BLOCK IoStatus = {0}; HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); RtlInitUnicodeString(&us, Drive); InitializeObjectAttributes(&oa, &us, OBJ_CASE_INSENSITIVE, NULL, NULL); ntStatus = NtCreateFile(&hDrive, GENERIC_WRITE | GENERIC_READ, &oa, &IoStatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0); if (NT_SUCCESS(ntStatus)) { ntStatus = NtDeviceIoControlFile(hDrive, hEvent, NULL, NULL, &IoStatus, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, DiskGeometry, sizeof(DISK_GEOMETRY)); if (ntStatus == STATUS_PENDING) WaitForSingleObject(hEvent, INFINITE); NtClose(hDrive); if (NT_SUCCESS(IoStatus.Status)) Ret = TRUE; } // if (NT_SUCCESS(ntStatus)) return(Ret); }
//@@unimplemented BOOL SfuIsDirectoryExists( _In_ PWSTR DirectoryName ) { UNICODE_STRING usDirectoryName; NTSTATUS status; HANDLE DirectoryHandle; IO_STATUS_BLOCK IoStatusBlock; OBJECT_ATTRIBUTES ObjectAttributes; RtlInitUnicodeString(&usDirectoryName, DirectoryName); InitializeObjectAttributes(&ObjectAttributes, &usDirectoryName, OBJ_CASE_INSENSITIVE, 0, NULL); status = NtCreateFile(&DirectoryHandle, SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_READONLY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0 ); if (NT_SUCCESS(status)) { NtClose(DirectoryHandle); return TRUE; } return FALSE; }
NTSTATUS WINAPI SafeNtCreateFile( PHANDLE FileHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength ) { NTSTATUS rc; if (CheckOldFunction(&OldNtCreateFile)) rc=OldNtCreateFile(FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,AllocationSize,FileAttributes, ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength); else rc=NtCreateFile(FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,AllocationSize,FileAttributes, ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength); return rc; }
static NTSTATUS MyCreateDirectory(TFileTestData * pData, POBJECT_ATTRIBUTES pObjAttr, PIO_STATUS_BLOCK pIoStatus) { NTSTATUS Status; HANDLE SaveTransactionHandle = NULL; HANDLE DirectoryHandle = NULL; if(pData->bUseTransaction) { SaveTransactionHandle = pfnRtlGetCurrentTransaction(); pfnRtlSetCurrentTransaction(pData->hTransaction); } Status = NtCreateFile(&DirectoryHandle, FILE_ADD_FILE, pObjAttr, pIoStatus, NULL, FILE_ATTRIBUTE_DIRECTORY, FILE_SHARE_READ, FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0); if(DirectoryHandle != NULL) NtClose(DirectoryHandle); if(pData->bUseTransaction) { pfnRtlSetCurrentTransaction(SaveTransactionHandle); } // If the directory already exists, take it as success return Status; }
/* * SfuCreateDirectory * * Purpose: * * Native create directory. * */ BOOL SfuCreateDirectory( _In_ OBJECT_ATTRIBUTES *ObjectAttributes ) { NTSTATUS status; HANDLE DirectoryHandle; IO_STATUS_BLOCK IoStatusBlock; status = NtCreateFile(&DirectoryHandle, FILE_GENERIC_WRITE, ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL,//za use hidden+system 0, FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0 ); if (!NT_SUCCESS(status)) { return FALSE; } NtClose(DirectoryHandle); return TRUE; }
/* * Should be called under privileges */ static NTSTATUS CreateRegistryFile( IN HANDLE RootDirectory OPTIONAL, IN PUNICODE_STRING RootPath OPTIONAL, IN PCWSTR RegistryKey, IN HANDLE ProtoKeyHandle) { NTSTATUS Status; HANDLE RootPathHandle, FileHandle; UNICODE_STRING FileName; OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; /* Open the root directory */ Status = OpenDirectoryByHandleOrPath(&RootPathHandle, RootDirectory, RootPath); if (!NT_SUCCESS(Status)) { DPRINT1("OpenDirectoryByHandleOrPath failed, Status 0x%08lx\n", Status); return Status; } /* Create the file */ RtlInitUnicodeString(&FileName, RegistryKey); InitializeObjectAttributes(&ObjectAttributes, &FileName, OBJ_CASE_INSENSITIVE, (HANDLE)((ULONG_PTR)RootPathHandle & ~1), // Remove the opened-locally flag NULL); Status = NtCreateFile(&FileHandle, FILE_GENERIC_WRITE /* | DELETE */, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL /* | FILE_FLAG_DELETE_ON_CLOSE */, 0, FILE_OVERWRITE_IF, FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, NULL, 0); if (!NT_SUCCESS(Status)) { DPRINT1("NtCreateFile(%wZ) failed, Status 0x%08lx\n", &FileName, Status); goto Cleanup; } /* Save the selected hive into the file */ Status = NtSaveKeyEx(ProtoKeyHandle, FileHandle, REG_LATEST_FORMAT); if (!NT_SUCCESS(Status)) { DPRINT1("NtSaveKeyEx(%wZ) failed, Status 0x%08lx\n", &FileName, Status); } /* Close the file, the root directory (if opened locally), and return */ NtClose(FileHandle); Cleanup: if ((ULONG_PTR)RootPathHandle & 1) NtClose((HANDLE)((ULONG_PTR)RootPathHandle & ~1)); return Status; }
NTSTATUS NtDeleteFile2(POBJECT_ATTRIBUTES ObjectAttributes) { HANDLE hFile; IO_STATUS_BLOCK IoStatusBlock; auto Status = NtOpenFile(&hFile, SYNCHRONIZE | DELETE, ObjectAttributes, &IoStatusBlock, FILE_SHARE_DELETE | FILE_SHARE_READ, FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT); if (Status) { return Status; } FILE_DISPOSITION_INFORMATION Dispostion = { TRUE }; Status = NtSetInformationFile(hFile, &IoStatusBlock, &Dispostion, sizeof(Dispostion), FileDispositionInformation); NtClose(hFile); if (Status == 0) return 0; //使用文件取代法删除 auto cbData = ObjectAttributes->ObjectName->Length + sizeof(L"~$") - 2; UNICODE_STRING TempFileName = { cbData,cbData, (wchar_t*)new byte[cbData] }; memcpy(TempFileName.Buffer, ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length); *(wchar_t*)(((byte*)TempFileName.Buffer) + ObjectAttributes->ObjectName->Length) = L'~'; *(wchar_t*)(((byte*)TempFileName.Buffer) + ObjectAttributes->ObjectName->Length + 2) = L'$'; OBJECT_ATTRIBUTES ObjectAttributes2 = { sizeof(OBJECT_ATTRIBUTES),ObjectAttributes->RootDirectory, &TempFileName, OBJ_CASE_INSENSITIVE }; auto Status2 = NtCreateFile(&hFile, SYNCHRONIZE | DELETE, &ObjectAttributes2, &IoStatusBlock, 0, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_OPEN_FOR_BACKUP_INTENT | FILE_DELETE_ON_CLOSE, NULL, 0); delete[](byte*)TempFileName.Buffer; cbData = sizeof(FILE_RENAME_INFORMATION) + ObjectAttributes->ObjectName->Length; FILE_RENAME_INFORMATION* pRenameInfo = (FILE_RENAME_INFORMATION*)new byte[cbData]; memcpy(pRenameInfo->FileName, ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length); pRenameInfo->ReplaceIfExists = TRUE; pRenameInfo->RootDirectory = ObjectAttributes->RootDirectory; pRenameInfo->FileNameLength = ObjectAttributes->ObjectName->Length; //FILE_RENAME_INFORMATION RenameInfo = {TRUE,hRootDir ,FileName.}; Status2 = NtSetInformationFile(hFile, &IoStatusBlock, pRenameInfo, cbData, FileRenameInformation); delete[](byte*)pRenameInfo; NtClose(hFile); return Status2 == 0 ? 0 : Status; }
VOID Mkdir( IN PCHAR String ) { NTSTATUS Status; HANDLE FileHandle; OBJECT_ATTRIBUTES ObjectAttributes; STRING NameString; IO_STATUS_BLOCK IoStatus; LARGE_INTEGER AllocationSize; // // Get the filename // simprintf("Mkdir ", 0); simprintf(String, 0); simprintf("\n", 0); // // Create the new directory // AllocationSize = LiFromLong( 4 ); RtlInitString( &NameString, String ); InitializeObjectAttributes( &ObjectAttributes, &NameString, 0, NULL, NULL ); if (!NT_SUCCESS(Status = NtCreateFile( &FileHandle, SYNCHRONIZE, &ObjectAttributes, &IoStatus, &AllocationSize, 0L, 0L, FILE_CREATE, WriteThrough | FILE_DIRECTORY_FILE, (PVOID)NULL, 0L ))) { CreateFileError( Status , String ); return; } // // Now close the directory // if (!NT_SUCCESS(Status = NtClose( FileHandle ))) { CloseError( Status ); } // // And return to our caller // return; }
int XDeleteDirectory(char *directoryName) { ANSI_STRING xboxFilename; IO_STATUS_BLOCK ioStatusBlock; OBJECT_ATTRIBUTES objectAttributes; FILE_DISPOSITION_INFORMATION deleteInfo; NTSTATUS status; int handle; #ifdef DEBUG debugPrint("XDeleteDirectory directoryName=%s\n", directoryName); #endif char tmp[200]; int rc = XConvertDOSFilenameToXBOX(directoryName, tmp); if (rc != STATUS_SUCCESS) return rc; RtlInitAnsiString(&xboxFilename, tmp); objectAttributes.RootDirectory = NULL; objectAttributes.Attributes = OBJ_CASE_INSENSITIVE; objectAttributes.ObjectName = &xboxFilename; // Open the file for deletion status = NtCreateFile( (PHANDLE)&handle, DELETE | SYNCHRONIZE, &objectAttributes, &ioStatusBlock, NULL, 0, FILE_SHARE_DELETE, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE); if (!NT_SUCCESS(status)) return RtlNtStatusToDosError(status); // Delete the file deleteInfo.DeleteFile = TRUE; status = NtSetInformationFile( (HANDLE)handle, &ioStatusBlock, &deleteInfo, sizeof(deleteInfo), FileDispositionInformation); if (!NT_SUCCESS(status)) return RtlNtStatusToDosError(status); else { // Close the dead handle and return NtClose((HANDLE)handle); return STATUS_SUCCESS; } }
/*++ * @name RtlCliOpenInputDevice * * The RtlCliOpenInputDevice routine opens an input device. * * @param Handle * Pointer where the handle for the input device will be returned. * * @param Type * Type of the input device to use. * * @return STATUS_SUCCESS or error code when attemping to open the device. * * @remarks This routine supports both mouse and keyboard input devices. * *--*/ NTSTATUS RtlCliOpenInputDevice(OUT PHANDLE Handle, IN CON_DEVICE_TYPE Type) { UNICODE_STRING Driver; OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK Iosb; HANDLE hDriver; NTSTATUS Status; // // Chose the driver to use // FIXME: Support MouseType later // FIXME: Don't hardcode keyboard path // if (Type == KeyboardType) { RtlInitUnicodeString(&Driver, L"\\Device\\KeyboardClass0"); } // // Initialize the object attributes // InitializeObjectAttributes(&ObjectAttributes, &Driver, OBJ_CASE_INSENSITIVE, NULL, NULL); // // Open a handle to it // Status = NtCreateFile(&hDriver, SYNCHRONIZE | GENERIC_READ | FILE_READ_ATTRIBUTES, &ObjectAttributes, &Iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0); // // Now create an event that will be used to wait on the device // InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL); Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &ObjectAttributes, 1, 0); // // Return the handle // *Handle = hDriver; return Status; }
static NTSTATUS SetupCreateSingleDirectory( PWCHAR DirectoryName) { OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; UNICODE_STRING PathName; HANDLE DirectoryHandle; NTSTATUS Status; if(!RtlCreateUnicodeString(&PathName, DirectoryName)) return STATUS_NO_MEMORY; if (PathName.Length > sizeof(WCHAR) && PathName.Buffer[PathName.Length / sizeof(WCHAR) - 2] == L'\\' && PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'.') { PathName.Length -= sizeof(WCHAR); PathName.Buffer[PathName.Length / sizeof(WCHAR)] = 0; } if (PathName.Length > sizeof(WCHAR) && PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'\\') { PathName.Length -= sizeof(WCHAR); PathName.Buffer[PathName.Length / sizeof(WCHAR)] = 0; } InitializeObjectAttributes(&ObjectAttributes, &PathName, OBJ_CASE_INSENSITIVE | OBJ_INHERIT, NULL, NULL); Status = NtCreateFile(&DirectoryHandle, DIRECTORY_ALL_ACCESS, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0); if (NT_SUCCESS(Status)) { NtClose(DirectoryHandle); } RtlFreeUnicodeString(&PathName); return Status; }
/*++ * @name SmpDeleteFile * * The SmpDeleteFile function deletes a specify file. * * @param lpFileName * the name of a file which should be deleted * * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL * othwerwise. * * @remarks * This function is called by SmpMoveFilesQueryRoutine(). * * *--*/ NTSTATUS SmpDeleteFile( IN LPCWSTR lpFileName ) { FILE_DISPOSITION_INFORMATION FileDispInfo; OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; UNICODE_STRING FileNameU; HANDLE FileHandle; NTSTATUS Status; DPRINT("SmpDeleteFile ( %S )\n", lpFileName); if( !lpFileName ) return (STATUS_INVALID_PARAMETER); RtlInitUnicodeString(&FileNameU, lpFileName); InitializeObjectAttributes(&ObjectAttributes, &FileNameU, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = NtCreateFile (&FileHandle, DELETE, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if( !NT_SUCCESS(Status) ) { DPRINT("NtCreateFile() failed (Status %lx)\n", Status); return (Status); } FileDispInfo.DeleteFile = TRUE; Status = NtSetInformationFile( FileHandle, &IoStatusBlock, &FileDispInfo, sizeof(FILE_DISPOSITION_INFORMATION), FileDispositionInformation ); NtClose(FileHandle); return (Status); }
HANDLE IthOpenPipe(LPWSTR name, ACCESS_MASK direction) { UNICODE_STRING us; RtlInitUnicodeString(&us,name); SECURITY_DESCRIPTOR sd={1}; OBJECT_ATTRIBUTES oa={sizeof(oa),0,&us,OBJ_CASE_INSENSITIVE,&sd,0}; HANDLE hFile; IO_STATUS_BLOCK isb; if (NT_SUCCESS(NtCreateFile(&hFile,direction,&oa,&isb,0,0,FILE_SHARE_READ,FILE_OPEN,0,0,0))) return hFile; else return INVALID_HANDLE_VALUE; }
NTSTATUS LogfBackupFile(PLOGFILE LogFile, PUNICODE_STRING BackupFileName) { NTSTATUS Status; LOGFILE BackupLogFile; OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; DPRINT1("LogfBackupFile(%p, %wZ)\n", LogFile, BackupFileName); /* Lock the log file shared */ RtlAcquireResourceShared(&LogFile->Lock, TRUE); InitializeObjectAttributes(&ObjectAttributes, BackupFileName, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = NtCreateFile(&BackupLogFile.FileHandle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE, FILE_WRITE_THROUGH | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (!NT_SUCCESS(Status)) { DPRINT("Cannot create backup file `%wZ' (Status 0x%08lx)\n", BackupFileName, Status); goto Quit; } Status = ElfBackupFile(&LogFile->LogFile, &BackupLogFile.LogFile); Quit: /* Close the backup file */ if (BackupLogFile.FileHandle != NULL) NtClose(BackupLogFile.FileHandle); /* Unlock the log file */ RtlReleaseResource(&LogFile->Lock); return Status; }
static int winfs_symlink(struct mount_point *mp, const char *target, const char *linkpath) { WCHAR wlinkpath[PATH_MAX]; int len = filename_to_nt_pathname(mp, linkpath, wlinkpath, PATH_MAX); if (len <= 0) return -L_ENOENT; UNICODE_STRING pathname; RtlInitCountedUnicodeString(&pathname, wlinkpath, len * sizeof(WCHAR)); IO_STATUS_BLOCK status_block; OBJECT_ATTRIBUTES attr; attr.Length = sizeof(OBJECT_ATTRIBUTES); attr.RootDirectory = NULL; attr.ObjectName = &pathname; attr.Attributes = 0; attr.SecurityDescriptor = NULL; attr.SecurityQualityOfService = NULL; HANDLE handle; NTSTATUS status = NtCreateFile(&handle, SYNCHRONIZE | FILE_WRITE_DATA, &attr, &status_block, NULL, FILE_ATTRIBUTE_SYSTEM, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (!NT_SUCCESS(status)) { if (status == STATUS_OBJECT_NAME_EXISTS || status == STATUS_OBJECT_NAME_COLLISION) { log_warning("File already exists."); return -L_EEXIST; } log_warning("NtCreateFile() failed, status: %x", status); return -L_ENOENT; } DWORD num_written; if (!WriteFile(handle, WINFS_SYMLINK_HEADER, WINFS_SYMLINK_HEADER_LEN, &num_written, NULL) || num_written < WINFS_SYMLINK_HEADER_LEN) { log_warning("WriteFile() failed, error code: %d.", GetLastError()); NtClose(handle); return -L_EIO; } DWORD targetlen = strlen(target); if (!WriteFile(handle, target, targetlen, &num_written, NULL) || num_written < targetlen) { log_warning("WriteFile() failed, error code: %d.", GetLastError()); NtClose(handle); return -L_EIO; } NtClose(handle); return 0; }
NTSTATUS NTAPI NtOpenFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, ULONG ShareAccess, ULONG OpenOptions) { CHECK_POINTER(FileHandle); CHECK_POINTER(IoStatusBlock); #ifdef REDIR_IO { IO_STATUS_BLOCK iob; FILE_STANDARD_INFORMATION fi; DECLAREVARCONV(ObjectAttributesA); HANDLE hFile; NTSTATUS res = ftbl.nt.NtOpenFile(&hFile, DesiredAccess, ObjectAttributes, IoStatusBlock, ShareAccess, OpenOptions); OA2STR(ObjectAttributes); if (res < 0) { Log("ntdll.NtOpenFile(\"%s\", 0x%08x) = 0x%08x\n", ObjectAttributesA, DesiredAccess, res); return res; } __CreateHandle(*FileHandle, HANDLE_FILE, ObjectAttributesA); (*FileHandle)->file.mode = DesiredAccess; (*FileHandle)->file.fh = hFile; res = ftbl.nt.NtQueryInformationFile(hFile, &iob, &fi, sizeof(fi), FileStandardInformation); if (res == STATUS_SUCCESS) (*FileHandle)->file.st.st_size = fi.EndOfFile.QuadPart; else { GET_LENGTH_INFORMATION gli; if ((res = ftbl.nt.NtDeviceIoControlFile(hFile, NULL, NULL, NULL, IoStatusBlock, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &gli, sizeof(GET_LENGTH_INFORMATION))) < 0) { fprintf(stderr, "NtOpenFile() - Unable to get size of %s: 0x%08x\n", ObjectAttributesA, res); abort(); } (*FileHandle)->file.st.st_size = gli.Length.QuadPart; } Log("ntdll.NtOpenFile(\"%s\", 0x%08x)\n", ObjectAttributesA, DesiredAccess); return res; } #else return NtCreateFile(FileHandle, DesiredAccess, ObjectAttributes, IoStatusBlock, NULL, 0, ShareAccess, OPEN_EXISTING, OpenOptions, NULL, 0); #endif }
BOOL BackupWriteAlternateData(HANDLE hFile, BACKUPCONTEXT *pbuc, BACKUPIOFRAME *pbif) { if (pbuc->fStreamStart) { NTSTATUS Status; UNICODE_STRING strName; OBJECT_ATTRIBUTES oa; IO_STATUS_BLOCK iosb; strName.Length = (USHORT) pbuc->head.dwStreamNameSize; strName.MaximumLength = strName.Length; strName.Buffer = pbuc->head.cStreamName; InitializeObjectAttributes( &oa, &strName, OBJ_CASE_INSENSITIVE, hFile, NULL); Status = NtCreateFile( &pbuc->hAlternate, FILE_GENERIC_WRITE, &oa, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT, NULL, 0L); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); pbuc->fAccessError = TRUE; return(FALSE); } pbuc->fStreamStart = FALSE; } if (pbuc->hAlternate == INVALID_HANDLE_VALUE) { pbuc->fAccessError = TRUE; return(FALSE); } return(BackupWriteStream(pbuc->hAlternate, pbuc, pbif)); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Reads or writes specified number of sectors from/to specified buffer. // static BOOL DirectIo( PWCHAR Drive, // drive name to read/write sectors from/to PCHAR Buffer, // bufer to store the data ULONG Length, // size of the buffer ULONG LBASector, // starting LBA sector ULONG Count, // number of sectors to read/write ULONG Flags // variouse operation flags ) { BOOL Ret = FALSE; HANDLE hDrive; NTSTATUS ntStatus; OBJECT_ATTRIBUTES oa = {0}; UNICODE_STRING us; IO_STATUS_BLOCK IoStatus = {0}; LARGE_INTEGER FilePos = {0}; HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); RtlInitUnicodeString(&us, Drive); InitializeObjectAttributes(&oa, &us, OBJ_CASE_INSENSITIVE, NULL, NULL); if ((Count * BIOS_DEFAULT_SECTOR_SIZE) <= Length) { ntStatus = NtCreateFile(&hDrive, GENERIC_WRITE | GENERIC_READ, &oa, &IoStatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0); if (NT_SUCCESS(ntStatus)) { FilePos.QuadPart = ((ULONGLONG)LBASector * BIOS_DEFAULT_SECTOR_SIZE); if (Flags & SCSI_IO_WRITE_SECTOR) ntStatus = NtWriteFile(hDrive, hEvent, NULL, NULL, &IoStatus, Buffer, (Count*BIOS_DEFAULT_SECTOR_SIZE), &FilePos, NULL); else ntStatus = NtReadFile(hDrive, hEvent, NULL, NULL, &IoStatus, Buffer, (Count*BIOS_DEFAULT_SECTOR_SIZE), &FilePos, NULL); if (ntStatus == STATUS_PENDING) WaitForSingleObject(hEvent, INFINITE); NtClose(hDrive); if (NT_SUCCESS(IoStatus.Status)) Ret = TRUE; } // if (hDrive != INVALID_HANDLE_VALUE) } // if ((Count * BIOS_DEFAULT_SECTOR_SIZE) <= Length) return(Ret); }
DWORD AG_XBOX_GetXbeTitleId(const char *xbePath) { HANDLE h; DWORD titleId = -1; DWORD loadAddress; DWORD certLocation; DWORD bytesRead; ANSI_STRING file; OBJECT_ATTRIBUTES attr; IO_STATUS_BLOCK status; if(!xbePath) return -1; RtlInitAnsiString(&file, xbePath); InitializeObjectAttributes(&attr, &file, OBJ_CASE_INSENSITIVE, NULL); if(SUCCEEDED(NtCreateFile(&h, FILE_GENERIC_READ, &attr, &status, NULL, 0, 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT))) { if(SetFilePointer(h, 0x104, NULL, FILE_BEGIN) == 0x104) { ReadFile(h, &loadAddress, sizeof(DWORD), &bytesRead, NULL); if(bytesRead == sizeof(DWORD)) { if(SetFilePointer(h, 0x118, NULL, FILE_BEGIN ) == 0x118) { ReadFile(h, &certLocation, sizeof(DWORD), &bytesRead, NULL); if(bytesRead == sizeof(DWORD)) { certLocation -= loadAddress; certLocation += 8; titleId = 0; if(SetFilePointer(h, certLocation, NULL, FILE_BEGIN) == certLocation) { ReadFile(h, &titleId, sizeof(DWORD), &bytesRead, NULL); if(bytesRead != sizeof(DWORD)) { titleId = 0; } } } } } } CloseHandle(h); } return titleId; }
HANDLE WINAPI InternalOpenDirW(IN LPCWSTR DirName, IN BOOLEAN Write) { UNICODE_STRING NtPathU; OBJECT_ATTRIBUTES ObjectAttributes; NTSTATUS errCode; IO_STATUS_BLOCK IoStatusBlock; HANDLE hFile; if (!RtlDosPathNameToNtPathName_U(DirName, &NtPathU, NULL, NULL)) { WARN("Invalid path\n"); SetLastError(ERROR_BAD_PATHNAME); return INVALID_HANDLE_VALUE; } InitializeObjectAttributes(&ObjectAttributes, &NtPathU, OBJ_CASE_INSENSITIVE, NULL, NULL); errCode = NtCreateFile(&hFile, Write ? FILE_GENERIC_WRITE : FILE_GENERIC_READ, &ObjectAttributes, &IoStatusBlock, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0); RtlFreeHeap(RtlGetProcessHeap(), 0, NtPathU.Buffer); if (!NT_SUCCESS(errCode)) { BaseSetLastNTError(errCode); return INVALID_HANDLE_VALUE; } return hFile; }
__stdcall OpenDeviceByName( UNICODE_STRING *FileName ) { LONG status; VOID* handle; OBJECT_ATTRIBUTES objectAttributes; IO_STATUS_BLOCK isb; objectAttributes.Length = sizeof(OBJECT_ATTRIBUTES); objectAttributes.RootDirectory = NULL; objectAttributes.Attributes = OBJ_CASE_INSENSITIVE; objectAttributes.ObjectName = FileName; objectAttributes.SecurityDescriptor = NULL; objectAttributes.SecurityQualityOfService = NULL; /*status = NtOpenFile( &handle, SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE, &objAttrib, &io_status, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );*/ status = NtCreateFile( &handle, SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE, &objectAttributes, &isb, 0, NULL, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 ); if (!NT_SUCCESS(status)) { return INVALID_HANDLE_VALUE; } return handle; }
NTSTATUS DfsOpen( IN OUT PHANDLE DfsHandle, IN PUNICODE_STRING DfsName OPTIONAL ) { NTSTATUS status; OBJECT_ATTRIBUTES objectAttributes; IO_STATUS_BLOCK ioStatus; PUNICODE_STRING name; if (ARGUMENT_PRESENT(DfsName)) { name = DfsName; } else { name = &LocalDfsName; } InitializeObjectAttributes( &objectAttributes, name, OBJ_CASE_INSENSITIVE, NULL, NULL ); status = NtCreateFile( DfsHandle, SYNCHRONIZE, &objectAttributes, &ioStatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OPEN_IF, FILE_CREATE_TREE_CONNECTION | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (NT_SUCCESS(status)) status = ioStatus.Status; return status; }
/* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */ static HANDLE open_vxd_handle( LPCWSTR name ) { static const WCHAR prefixW[] = {'\\','?','?','\\','u','n','i','x'}; const char *dir = wine_get_server_dir(); int len; HANDLE ret; NTSTATUS status; OBJECT_ATTRIBUTES attr; UNICODE_STRING nameW; IO_STATUS_BLOCK io; len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 ); nameW.Length = sizeof(prefixW) + (len + strlenW( name )) * sizeof(WCHAR); nameW.MaximumLength = nameW.Length + sizeof(WCHAR); if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.MaximumLength ))) { SetLastError( ERROR_NOT_ENOUGH_MEMORY ); return 0; } memcpy( nameW.Buffer, prefixW, sizeof(prefixW) ); MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer + sizeof(prefixW)/sizeof(WCHAR), len ); len += sizeof(prefixW) / sizeof(WCHAR); nameW.Buffer[len-1] = '/'; strcpyW( nameW.Buffer + len, name ); attr.Length = sizeof(attr); attr.RootDirectory = 0; attr.Attributes = 0; attr.ObjectName = &nameW; attr.SecurityDescriptor = NULL; attr.SecurityQualityOfService = NULL; status = NtCreateFile( &ret, 0, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 ); if (status) { ret = 0; SetLastError( RtlNtStatusToDosError(status) ); } RtlFreeUnicodeString( &nameW ); return ret; }
int XCreateDirectory(char *directoryName) { ANSI_STRING xboxFilename; IO_STATUS_BLOCK ioStatusBlock; OBJECT_ATTRIBUTES objectAttributes; NTSTATUS status; int handle; #ifdef DEBUG debugPrint("XCreateDirectory directoryName=%s\n", directoryName); #endif char tmp[200]; int rc = XConvertDOSFilenameToXBOX(directoryName, tmp); if (rc != STATUS_SUCCESS) return rc; RtlInitAnsiString(&xboxFilename, tmp); objectAttributes.RootDirectory = NULL; objectAttributes.Attributes = OBJ_CASE_INSENSITIVE; objectAttributes.ObjectName = &xboxFilename; // Create the directory status = NtCreateFile( (PHANDLE)&handle, GENERIC_READ | SYNCHRONIZE | DELETE, &objectAttributes, &ioStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SUPERSEDE, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT); if (!NT_SUCCESS(status)) return RtlNtStatusToDosError(status); else { NtClose((HANDLE)handle); return STATUS_SUCCESS; } }
int main() { wcscpy(path+4,GetModulePath()); LPWSTR p=path; while (*p) p++; while (*p!=L'\\') p--; p--; while (*p!=L'\\') p--; p++; wcscpy(p,L"version.h"); UNICODE_STRING us; RtlInitUnicodeString(&us,path); OBJECT_ATTRIBUTES oa={sizeof(oa),0,&us,OBJ_CASE_INSENSITIVE,0,0}; HANDLE hFile; IO_STATUS_BLOCK isb; if (!NT_SUCCESS(NtCreateFile(&hFile, FILE_SHARE_READ|FILE_WRITE_DATA|FILE_READ_ATTRIBUTES|SYNCHRONIZE ,&oa,&isb,0,0,FILE_SHARE_READ,FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT|FILE_NON_DIRECTORY_FILE,0,0))) return 1; FILE_BASIC_INFORMATION basic; NtQueryInformationFile(hFile,&isb,&basic,sizeof(basic),FileBasicInformation); int l=strlen(buffer); char* ptr; ptr=buffer+l; LARGE_INTEGER current_time; TIME_FIELDS tf,ctf; NtQuerySystemTime(¤t_time); current_time.QuadPart-=GetTimeBias()->QuadPart; RtlTimeToTimeFields(¤t_time,&tf); RtlTimeToTimeFields(&basic.LastWriteTime,&ctf); if (ctf.wDay!=tf.wDay||ctf.wMonth!=tf.wMonth||ctf.wYear!=tf.wYear){ l+=sprintf(ptr,"%.4d.%.2d.%.2d)\\r\\n\";",tf.wYear,tf.wMonth,tf.wDay); NtWriteFile(hFile,0,0,0,&isb,buffer,l,0,0); } NtClose(hFile); return 0; //IthCloseSystemService(); }
/** * @internal * @brief Opens root directory of the volume. * @param[in] volume_letter the volume letter. * @return File handle, NULL indicates failure. */ static HANDLE OpenRootDirectory(unsigned char volume_letter) { wchar_t rootpath[] = L"\\??\\A:\\"; HANDLE hRoot; NTSTATUS status; UNICODE_STRING us; OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; rootpath[4] = (wchar_t)winx_toupper(volume_letter); RtlInitUnicodeString(&us,rootpath); InitializeObjectAttributes(&ObjectAttributes,&us, FILE_READ_ATTRIBUTES,NULL,NULL); status = NtCreateFile(&hRoot,FILE_GENERIC_READ, &ObjectAttributes,&IoStatusBlock,NULL,0, FILE_SHARE_READ|FILE_SHARE_WRITE,FILE_OPEN,0, NULL,0); if(!NT_SUCCESS(status)){ strace(status,"cannot open %ls",rootpath); return NULL; } return hRoot; }
NTSTATUS LogfCreate(PLOGFILE *LogFile, WCHAR *LogName, PUNICODE_STRING FileName, ULONG ulMaxSize, ULONG ulRetention, BOOL Permanent, BOOL Backup) { OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; PLOGFILE pLogFile; BOOL bCreateNew = FALSE; NTSTATUS Status = STATUS_SUCCESS; pLogFile = (LOGFILE *) HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, sizeof(LOGFILE)); if (!pLogFile) { DPRINT1("Can't allocate heap!\n"); return STATUS_NO_MEMORY; } InitializeObjectAttributes(&ObjectAttributes, FileName, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = NtCreateFile(&pLogFile->hFile, Backup ? (GENERIC_READ | SYNCHRONIZE) : (GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE), &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, Backup ? FILE_OPEN : FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (!NT_SUCCESS(Status)) { DPRINT1("Can't create file %wZ (Status: 0x%08lx)\n", FileName, Status); goto fail; } bCreateNew = (IoStatusBlock.Information == FILE_CREATED) ? TRUE: FALSE; pLogFile->LogName = (WCHAR *) HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, (lstrlenW(LogName) + 1) * sizeof(WCHAR)); if (pLogFile->LogName == NULL) { DPRINT1("Can't allocate heap\n"); Status = STATUS_NO_MEMORY; goto fail; } if (LogName) StringCchCopy(pLogFile->LogName,lstrlenW(LogName) + 1, LogName); pLogFile->FileName = (WCHAR *) HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, (lstrlenW(FileName->Buffer) + 1) * sizeof(WCHAR)); if (pLogFile->FileName == NULL) { DPRINT1("Can't allocate heap\n"); Status = STATUS_NO_MEMORY; goto fail; } StringCchCopy(pLogFile->FileName, lstrlenW(FileName->Buffer) + 1, FileName->Buffer); pLogFile->OffsetInfo = (PEVENT_OFFSET_INFO) HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, sizeof(EVENT_OFFSET_INFO) * 64); if (pLogFile->OffsetInfo == NULL) { DPRINT1("Can't allocate heap\n"); Status = STATUS_NO_MEMORY; goto fail; } pLogFile->OffsetInfoSize = 64; pLogFile->Permanent = Permanent; if (bCreateNew) Status = LogfInitializeNew(pLogFile, ulMaxSize, ulRetention); else Status = LogfInitializeExisting(pLogFile, Backup); if (!NT_SUCCESS(Status)) goto fail; RtlInitializeResource(&pLogFile->Lock); LogfListAddItem(pLogFile); fail: if (!NT_SUCCESS(Status)) { if ((pLogFile->hFile != NULL) && (pLogFile->hFile != INVALID_HANDLE_VALUE)) CloseHandle(pLogFile->hFile); if (pLogFile->OffsetInfo) HeapFree(MyHeap, 0, pLogFile->OffsetInfo); if (pLogFile->FileName) HeapFree(MyHeap, 0, pLogFile->FileName); if (pLogFile->LogName) HeapFree(MyHeap, 0, pLogFile->LogName); HeapFree(MyHeap, 0, pLogFile); } else { *LogFile = pLogFile; } return Status; }
NTSTATUS LogfBackupFile(PLOGFILE LogFile, PUNICODE_STRING BackupFileName) { OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; EVENTLOGHEADER Header; EVENTLOGEOF EofRec; HANDLE FileHandle = NULL; ULONG i; LARGE_INTEGER FileOffset; NTSTATUS Status; PUCHAR Buffer = NULL; DWORD dwOffset, dwRead, dwRecSize; DPRINT1("LogfBackupFile(%p, %wZ)\n", LogFile, BackupFileName); /* Lock the log file shared */ RtlAcquireResourceShared(&LogFile->Lock, TRUE); InitializeObjectAttributes(&ObjectAttributes, BackupFileName, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = NtCreateFile(&FileHandle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE, FILE_WRITE_THROUGH | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (!NT_SUCCESS(Status)) { DPRINT("Can't create backup file %wZ (Status: 0x%08lx)\n", BackupFileName, Status); goto Done; } /* Initialize the (dirty) log file header */ Header.HeaderSize = sizeof(EVENTLOGHEADER); Header.Signature = LOGFILE_SIGNATURE; Header.MajorVersion = MAJORVER; Header.MinorVersion = MINORVER; Header.StartOffset = sizeof(EVENTLOGHEADER); Header.EndOffset = sizeof(EVENTLOGHEADER); Header.CurrentRecordNumber = 1; Header.OldestRecordNumber = 1; Header.MaxSize = LogFile->Header.MaxSize; Header.Flags = ELF_LOGFILE_HEADER_DIRTY; Header.Retention = LogFile->Header.Retention; Header.EndHeaderSize = sizeof(EVENTLOGHEADER); /* Write the (dirty) log file header */ Status = NtWriteFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, &Header, sizeof(EVENTLOGHEADER), NULL, NULL); if (!NT_SUCCESS(Status)) { DPRINT1("Failed to write the log file header (Status: 0x%08lx)\n", Status); goto Done; } for (i = LogFile->Header.OldestRecordNumber; i < LogFile->Header.CurrentRecordNumber; i++) { dwOffset = LogfOffsetByNumber(LogFile, i); if (dwOffset == 0) break; if (SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { DPRINT1("SetFilePointer() failed!\n"); goto Done; } if (!ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL)) { DPRINT1("ReadFile() failed!\n"); goto Done; } if (SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { DPRINT1("SetFilePointer() failed!\n"); goto Done; } Buffer = HeapAlloc(MyHeap, 0, dwRecSize); if (Buffer == NULL) { DPRINT1("HeapAlloc() failed!\n"); goto Done; } if (!ReadFile(LogFile->hFile, Buffer, dwRecSize, &dwRead, NULL)) { DPRINT1("ReadFile() failed!\n"); goto Done; } /* Write the event record */ Status = NtWriteFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, Buffer, dwRecSize, NULL, NULL); if (!NT_SUCCESS(Status)) { DPRINT1("NtWriteFile() failed! (Status: 0x%08lx)\n", Status); goto Done; } /* Update the header information */ Header.EndOffset += dwRecSize; /* Free the buffer */ HeapFree(MyHeap, 0, Buffer); Buffer = NULL; } /* Initialize the EOF record */ EofRec.RecordSizeBeginning = sizeof(EVENTLOGEOF); EofRec.Ones = 0x11111111; EofRec.Twos = 0x22222222; EofRec.Threes = 0x33333333; EofRec.Fours = 0x44444444; EofRec.BeginRecord = sizeof(EVENTLOGHEADER); EofRec.EndRecord = Header.EndOffset; EofRec.CurrentRecordNumber = LogFile->Header.CurrentRecordNumber; EofRec.OldestRecordNumber = LogFile->Header.OldestRecordNumber; EofRec.RecordSizeEnd = sizeof(EVENTLOGEOF); /* Write the EOF record */ Status = NtWriteFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, &EofRec, sizeof(EVENTLOGEOF), NULL, NULL); if (!NT_SUCCESS(Status)) { DPRINT1("NtWriteFile() failed!\n"); goto Done; } /* Update the header information */ Header.CurrentRecordNumber = LogFile->Header.CurrentRecordNumber; Header.OldestRecordNumber = LogFile->Header.OldestRecordNumber; Header.MaxSize = Header.EndOffset + sizeof(EVENTLOGEOF); Header.Flags = 0; /* Write the (clean) log file header */ FileOffset.QuadPart = 0; Status = NtWriteFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, &Header, sizeof(EVENTLOGHEADER), &FileOffset, NULL); if (!NT_SUCCESS(Status)) { DPRINT1("NtWriteFile() failed! (Status: 0x%08lx)\n", Status); } Done: /* Free the buffer */ if (Buffer != NULL) HeapFree(MyHeap, 0, Buffer); /* Close the backup file */ if (FileHandle != NULL) NtClose(FileHandle); /* Unlock the log file */ RtlReleaseResource(&LogFile->Lock); return Status; }
unsigned int XFindFirstFile( char *directoryName, char *mask, PWIN32_FIND_DATA findFileData) { #ifdef DEBUG debugPrint("XFindFirstFile directoryName=%s mask=%s\n", directoryName, mask); #endif IO_STATUS_BLOCK IoStatusBlock; FILE_DIRECTORY_INFORMATION FileInformation; HANDLE handle = NULL; ANSI_STRING FileMask; ANSI_STRING FileName; FILE_INFORMATION_CLASS FileInformationClass = FileDirectoryInformation; OBJECT_ATTRIBUTES Attributes; if (!strcmp(directoryName, ".")) directoryName = getPartitionString(currentDrive); char *directoryBuffer = (char *)malloc(200); int rc = XConvertDOSFilenameToXBOX(directoryName, directoryBuffer); if (rc != STATUS_SUCCESS) return INVALID_HANDLE_VALUE; RtlInitAnsiString(&FileMask, mask); RtlInitAnsiString(&FileName, directoryBuffer); Attributes.RootDirectory = NULL; Attributes.ObjectName = &FileName; Attributes.Attributes = OBJ_CASE_INSENSITIVE; NTSTATUS status = NtCreateFile( &handle, FILE_LIST_DIRECTORY, &Attributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN, FILE_DIRECTORY_FILE); if (!NT_SUCCESS(status)) return INVALID_HANDLE_VALUE; // and now actually do the looping over each file... memset(&FileInformation, 0, sizeof(FILE_DIRECTORY_INFORMATION)); status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &IoStatusBlock, &FileInformation, sizeof(FILE_DIRECTORY_INFORMATION), FileInformationClass, &FileMask, TRUE); if (!NT_SUCCESS(status)) return INVALID_HANDLE_VALUE; /*findFileData->dwFileAttributes = FileInformation.FileAttributes; findFileData->ftCreationTime = FileInformation.CreationTime.QuadPart; findFileData->ftLastAccessTime = FileInformation.LastAccessTime.QuadPart; findFileData->ftLastWriteTime = FileInformation.LastWriteTime.QuadPart; findFileData->nFileSize = FileInformation.AllocationSize.QuadPart;*/ strcpy(findFileData->cFileName, FileInformation.FileName); return (unsigned int)handle; }
int XRenameFile( char *oldFilename, char *newFilename) { ANSI_STRING xboxFilename; IO_STATUS_BLOCK ioStatusBlock; OBJECT_ATTRIBUTES objectAttributes; FILE_RENAME_INFORMATION renameInfo; NTSTATUS status; int handle; #ifdef DEBUG debugPrint("XRenameFile oldFilename=%s newFilename=%s\n", oldFilename, newFilename); #endif char tmp[200]; int rc = XConvertDOSFilenameToXBOX(oldFilename, tmp); if (rc != STATUS_SUCCESS) return rc; RtlInitAnsiString(&xboxFilename, tmp); objectAttributes.RootDirectory = NULL; objectAttributes.Attributes = OBJ_CASE_INSENSITIVE; objectAttributes.ObjectName = &xboxFilename; // Try to open the file // I'm not sure why we have to use these particular flags, but we do. status = NtCreateFile( (PHANDLE)handle, DELETE | SYNCHRONIZE, &objectAttributes, &ioStatusBlock, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT | FILE_NON_DIRECTORY_FILE); if (!NT_SUCCESS(status)) return RtlNtStatusToDosError(status); // Set up the rename information rc = XConvertDOSFilenameToXBOX(newFilename, tmp); if (rc != STATUS_SUCCESS) return rc; RtlInitAnsiString(&renameInfo.FileName, tmp); renameInfo.ReplaceIfExists = TRUE; renameInfo.RootDirectory = NULL; // Try to rename the file status = NtSetInformationFile( (HANDLE)handle, &ioStatusBlock, &renameInfo, sizeof(renameInfo), FileRenameInformation); if (!NT_SUCCESS(status)) { NtClose((HANDLE)handle); return RtlNtStatusToDosError(status); } else { NtClose((HANDLE)handle); return STATUS_SUCCESS; } }