Blame src/fs/ntapi_tt_istat.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dd89bb
/*  Copyright (C) 2013,2014,2015  Z. Gilboa             */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/nt_fsctl.h>
dd89bb
#include <ntapi/nt_mount.h>
dd89bb
#include <ntapi/nt_istat.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_istat(
dd89bb
	__in	void *			hfile,
dd89bb
	__in	void *			hroot	__optional,
dd89bb
	__in	nt_unicode_string *	path,
dd89bb
	__out	nt_istat *		istat,
dd89bb
	__out	uintptr_t *		buffer,
dd89bb
	__in	uint32_t		buffer_size,
dd89bb
	__in	uint32_t		open_options,
dd89bb
	__in	uint32_t		flags)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
dd89bb
	nt_oa			oa;
dd89bb
	nt_iosb			iosb;
dd89bb
	nt_unicode_string *	sdev;
dd89bb
	uint32_t		hash;
dd89bb
	wchar16_t *		wch;
dd89bb
	wchar16_t *		wch_mark;
dd89bb
dd89bb
	/* validaton */
dd89bb
	if (!hfile && !path)
dd89bb
		return NT_STATUS_INVALID_HANDLE;
dd89bb
dd89bb
	/* hfile */
dd89bb
	if (!hfile) {
dd89bb
		/* oa */
dd89bb
		oa.len = sizeof(nt_oa);
dd89bb
		oa.root_dir = hroot;
dd89bb
		oa.obj_name = path;
dd89bb
		oa.obj_attr = 0;
dd89bb
		oa.sec_desc = 0;
dd89bb
		oa.sec_qos = 0;
dd89bb
dd89bb
		/* open file/folder */
dd89bb
		status = __ntapi->zw_open_file(
dd89bb
			&hfile,
dd89bb
			NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES | NT_FILE_READ_ACCESS,
dd89bb
			&oa,
dd89bb
			&iosb,
dd89bb
			NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
dd89bb
			open_options | NT_FILE_SYNCHRONOUS_IO_ALERT);
dd89bb
dd89bb
		if (status != NT_STATUS_SUCCESS)
dd89bb
			return status;
dd89bb
dd89bb
		istat->flags_out = NT_STAT_NEW_HANDLE;
dd89bb
	}
dd89bb
dd89bb
	istat->hfile    = hfile;
dd89bb
	istat->flags_in = flags;
dd89bb
dd89bb
	/* file index number */
dd89bb
	status = __ntapi->zw_query_information_file(
dd89bb
		hfile,
dd89bb
		&iosb,
dd89bb
		&istat->fii,
dd89bb
		sizeof(istat->fii),
dd89bb
		NT_FILE_INTERNAL_INFORMATION);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	/* attributes & reparse tag information */
dd89bb
	status = __ntapi->zw_query_information_file(
dd89bb
		hfile,
dd89bb
		&iosb,
dd89bb
		&istat->ftagi,
dd89bb
		sizeof(istat->ftagi),
dd89bb
		NT_FILE_ATTRIBUTE_TAG_INFORMATION);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	/* TODO: consolidate with statfs */
dd89bb
	/* system-unique device name */
dd89bb
	iosb.info = 0;
dd89bb
	status = __ntapi->zw_query_object(
dd89bb
		hfile,
dd89bb
		NT_OBJECT_NAME_INFORMATION,
dd89bb
		buffer,
dd89bb
		buffer_size,
dd89bb
		(uint32_t *)&iosb.info);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	sdev = (nt_unicode_string *)buffer;
dd89bb
dd89bb
	if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
dd89bb
		return NT_STATUS_INVALID_HANDLE;
dd89bb
dd89bb
	hash = __ntapi->tt_buffer_crc32(
dd89bb
		0,
dd89bb
		sdev->buffer,
dd89bb
		__DEVICE_PATH_PREFIX_LEN);
dd89bb
dd89bb
	if (hash != __DEVICE_PATH_PREFIX_HASH)
dd89bb
		return NT_STATUS_INVALID_HANDLE;
dd89bb
dd89bb
	wch_mark = sdev->buffer + __DEVICE_PATH_PREFIX_LEN/sizeof(wchar16_t);
dd89bb
	wch = wch_mark;
dd89bb
	while (*wch != '\\') wch++;
dd89bb
	istat->dev_name_strlen = (uint16_t)((wch - sdev->buffer) * sizeof(uint16_t));
dd89bb
dd89bb
	istat->dev_name_hash = __ntapi->tt_buffer_crc32(
dd89bb
		hash,
dd89bb
		wch_mark,
dd89bb
		(uintptr_t)wch - (uintptr_t)wch_mark);
dd89bb
dd89bb
	return status;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_validate_fs_handle(
dd89bb
	__in	void *		hfile,
dd89bb
	__in	uint32_t	dev_name_hash,
dd89bb
	__in	nt_fii		fii,
dd89bb
	__out	uintptr_t *	buffer,
dd89bb
	__in	uint32_t	buffer_size)
dd89bb
{
dd89bb
	int32_t		status;
dd89bb
	nt_istat	istat;
dd89bb
dd89bb
	status = __ntapi->tt_istat(
dd89bb
		hfile,
dd89bb
		(void *)0,
dd89bb
		(nt_unicode_string *)0,
dd89bb
		&istat,
dd89bb
		buffer,
dd89bb
		buffer_size,
dd89bb
		0,
dd89bb
		NT_ISTAT_DEFAULT);
dd89bb
dd89bb
	if (status) return status;
dd89bb
dd89bb
	if (istat.fii.index_number.quad != fii.index_number.quad)
dd89bb
		return NT_STATUS_CONTEXT_MISMATCH;
dd89bb
	else if (istat.dev_name_hash != dev_name_hash)
dd89bb
		return NT_STATUS_CONTEXT_MISMATCH;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}