Demo entry 6780793

Johnson

   

Submitted by anonymous on Dec 29, 2018 at 07:06
Language: C. Code size: 1.8 kB.

// 在原来的位置上创建一个文件
STATUS
FatCreateNewFile(
	IN PDEVICE_OBJECT DeviceObject,
	IN PCSTR FileName,
	IN OUT PFILE_OBJECT FileObject
	)
{
	STATUS Status;
	PFCB Fcb;
	
	//
	// 打开文件
	//
	Status = FatOpenFile( (PVCB)DeviceObject->DeviceExtension,
						  FileName,
						  &Fcb );
	if (!EOS_SUCCESS(Status)) {

		if (STATUS_FILE_NOT_FOUND == Status && FileObject->FlagsAndAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			return STATUS_PATH_NOT_FOUND;
		}

		return Status;
	}
	//
	// 检查打开的文件类型是否符合要求。
	//
	if (FileObject->FlagsAndAttributes & FILE_ATTRIBUTE_DIRECTORY) {

		if (!Fcb->AttrDirectory) {
			FatCloseFile(Fcb);
			return STATUS_PATH_NOT_FOUND; // 目录文件不存在。
		}

	} else {

		if (Fcb->AttrDirectory) {
			FatCloseFile(Fcb);
			return STATUS_ACCESS_DENIED; // 拒绝以数据方式打开目录文件。
		}
	}
	
	if (Fcb->AttrReadOnly && FileObject->WriteAccess) {
		FatCloseFile(Fcb);
		return STATUS_ACCESS_DENIED; // 拒绝对只读文件的写请求。
	}
	
	//
	// 如果文件是第一次被打开则设置共享权限,否则检查是否共享冲突。
	//
	if (1 == Fcb->OpenCount) {

		Fcb->SharedRead = FileObject->SharedRead;
		Fcb->SharedWrite = FileObject->SharedWrite;

	} else {

		if (FileObject->ReadAccess && !Fcb->SharedRead ||
			FileObject->WriteAccess && !Fcb->SharedWrite ||
			FileObject->SharedRead != Fcb->SharedRead ||
			FileObject->SharedWrite != Fcb->SharedWrite) {
			FatCloseFile(Fcb);
			return STATUS_SHARING_VIOLATION;
		}
	}
	
	if(Fcb->FileSize > 0){
		USHORT Cluster = 0;
	
		Fcb->FileSize = 0;
		FileObject->FsContext = Fcb;
		
		Cluster = FatGetFatEntryValue((PVCB)DeviceObject->DeviceExtension, Fcb->FirstCluster);
		FatSetFatEntryValue((PVCB)DeviceObject->DeviceExtension, Cluster, 0xFF8);
		
		IoDeleteFile(FileName);
	}	
	else {
		FileObject->FsContext = Fcb;
	}
	
	return STATUS_SUCCESS;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).