A method for releasing files from usage under Windows
By opening files at the kernel layer and traversing the system's global handle table, matching and closing the occupation of specific files, the problem of excessive impact in the existing technology is solved, and efficient file occupation is eliminated, which is suitable for a variety of Windows systems File occupancy.
Patent Information
- Application Number
- CN202111369958.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-18
- Publication Date
- 2024-11-01
- Estimated Expiration
- 2041-11-18
AI Technical Summary
Existing methods for undoing file occupancy can easily affect other files during processing, and cannot effectively handle paging files or system kernel occupancy, and the FSContext parameter may not be found or the handle cannot be closed directly.
By opening the file with AccessFlag of 0 at the kernel layer, obtaining the FsContext parameter, and traversing the system global handle table to match and close the occupation of a specific file, use functions such as Duplicate_Close_Source and ZwUnmapViewOfSection to unoccupy.
It improves the efficiency of finding matching, avoids the impact on other files, provides a comprehensive processing method for different file occupations, and solves the problem of not finding the FsContext parameter and being unable to directly close the handle.
Smart Images

Figure CN114048176B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of file occupation release, and in particular to a method for releasing file occupation in Windows. Background Art
[0002] Freeing files from being occupied is an important basic pre-operation in the field of file management, and is also used in the field of partition management. In the actual use of the computer, some functions that require exclusive resources cannot be carried out immediately due to a certain file being occupied. They can only be executed after restarting or starting another system, which leads to low efficiency of function execution. In this case, freeing files from being occupied is very necessary.
[0003] Currently, the commonly used method to release the file occupation is to forcibly unmount the file system of the specified volume. This can achieve the purpose of releasing the file occupation, but the impact of the processing is expanded, and other occupied files are also processed.
[0004] Also, if there are paging files or files occupied by the system kernel on the specified volume, the file system of the specified volume cannot be unloaded, and this situation cannot be handled.
[0005] The patent number is "201410549732.6", and the patent name is "Method and device for releasing file occupation", which discloses a method and device for releasing file occupation. The method includes: matching a first parameter and a second parameter, wherein the first parameter is an attribute parameter of a file object to be matched determined by a system process, and the second parameter is an attribute parameter of a file object of a reference file to be released; if the match is successful, the file object to be matched is processed according to a set release strategy to release the occupation of the reference file. The method solves the technical problem that the existing file release technology cannot meet people's growing demand for file release services, optimizes the existing file release technology, and meets people's growing demand for file release services.
[0006] Compared with the technical solution described in this application, the above patent has the following differences and shortcomings:
[0007] 1) Although the patent discloses a method for matching through FSContext parameters, the other party obtains the FSContext parameters from the system process, and the FSContext parameters can only be found when the file object is opened, so the other party's method may not find the FSContext parameters. 2) The patent discloses that the closing of the target Section object handle in "closing the handle of the target Section object to release the occupation of the reference file" is wrong, and Section_Obj cannot directly close the handle. Summary of the invention
[0008] In order to solve the problems existing in the above background technology, the present invention proposes a method for releasing file occupation under Windows, the method comprising:
[0009] Open the target file, obtain the target file reference identifier, and obtain the first FsContext field;
[0010] Traverse the system global handle table and get the second FsContext field;
[0011] If the first FsContext field is the same as the second FsContext field, the target file is closed.
[0012] Further, the target file is opened, the target file reference identifier is obtained, and the first FsContext field is obtained as follows:
[0013] Open the target file in the kernel layer with AccessFlag 0 and share mode 7;
[0014] Get the target file reference identifier and obtain the first FsContext field.
[0015] Furthermore, the target file occupancy type includes file form occupancy and section type occupancy.
[0016] Furthermore, the occupation form exists in the form of File_obj or Section_Obj in the kernel layer.
[0017] Furthermore, the traversal of the system global handle table obtains the second FsContext field as follows:
[0018] Traverse the handles of File_Obj in the system global handle table, correspond the handles to the File_Obj objects through the kernel method, and obtain the second FsContext field.
[0019] Furthermore, the traversal of the system global handle table obtains the second FsContext field as follows:
[0020] The handles of Section_Obj in the system global handle table are traversed, and the handles are mapped to Section_Obj objects through kernel methods to obtain the second FsContext field.
[0021] Furthermore, if the first FsContext field is the same as the second FsContext field, closing the target file is specifically:
[0022] If the first FsContext field is the same as the second FsContext field, call the parameter Duplicate_Close_Source to close the target file.
[0023] Furthermore, if the first FsContext field is the same as the second FsContext field, closing the target file is specifically:
[0024] If the first FsContext field is the same as the second FsContext field, the handle corresponding to the second FsContext field is unoccupied by calling the parameter ZwUnmapViewOfSection.
[0025] Beneficial effects: The present invention can quickly obtain the FsContext parameter by opening the specified file in the kernel layer with AccessFlag as 0, and can greatly improve the efficiency of search and matching by marking the FsContext parameter, and there is no problem of not being able to find the FsContext parameter and there is no problem of not being able to directly close the handle; the occupation of a specific file is released without expanding the processing scope, and the impact on third-party applications is small; in addition, the technical solution recorded in this application provides a comprehensive processing method for several situations of file occupation under the Windows system. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0027] Figure 1 The present invention provides a flowchart of a method for releasing file occupation under Windows according to an embodiment of the present invention. DETAILED DESCRIPTION
[0028] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0029] The following will specifically describe the technical solution of the present application and how the technical solution of the present application solves the above technical problems through embodiments and in conjunction with the accompanying drawings. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments.
[0030] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments.
[0031] In one embodiment, Figure 1 As shown, there are basically three situations in which the Windows system occupies files. First, the file is opened and a handle is generated, but it is not closed. Second, the process occupies it in the form of a module, such as LoadLibrary, and it is not released. Third, the process occupies it in the form of mapped memory and the unmapping is not performed.
[0032] Regardless of any of the above forms, the occupied file exists in the kernel layer in the form of File_obj and Section_Ob. These two types of kernel objects are global objects. The generated objects exist in the system global object table. Each object in the system's global object table can be judged by its type value to which type of object the kernel object belongs.
[0033] Specify the specific file occupancy object:
[0034] Open the specified file in the kernel layer with AccessFlag as 0, thereby obtaining a File_Obj that cannot perform any operations on the specified file. For all File_Obj that have opened the specified file, the FsContext field is the same.
[0035] Specifically, the file to be unlocked is opened with access 0 and share mode 7 to generate a Handle, which is recorded as S1 and passed to the kernel for subsequent processing. At the kernel layer, the passed S1 is converted into a kernel object File_Obj to obtain the first FsContext field. The first FsContext field is marked as S2 for search to obtain all the occupation information of the file.
[0036] Release the file form occupation:
[0037] The handle of File_Obj in the system global system handle table is traversed and matched to the File_Obj object through the kernel method ObReferenceByHandle. The File_Obj object contains the second FsContext field. If the field is the same as S2, it is the corresponding file access object. Then the aforementioned system handle is converted to the processing process by calling DuplicateHandle, and the parameter CLOSE_SOURCE is used in the call to close the original handle.
[0038] The technical solution of the present application is to open the specified file in the kernel with AccessFlag as 0, so there is no problem of not being able to find the FSContext parameter, and the present application can greatly improve the efficiency of searching and matching later by marking the FSContext.
[0039] Release the Section type occupation:
[0040] Open the global handle table, and directly convert the kernel object of Section type into an object of Section_Obj type. The pAccessFile field in the Control_Area data structure records a File_Obj. By matching the FsContext in File_Obj with S2, the matching handle information is collected, among which Section_Obj and process ID are the key. Perform the following operations on the collected occupied Section object information: query the specified virtual address from address 0, the method is ZwQueryVirtualMemory, the parameter used is MemoryBasicInformation, if the type of the specified virtual address is MEM_MAPPED, it is memory mapping, otherwise continue to traverse; query the memory mapped virtual address again, call the parameter MemorySectionName, obtain the mapping file name, query the specified address from address 0, and call the parameter MemoryBasicInformation to traverse the specified address; when the mapping file name is the same as the target file name, close the memory mapping.
[0041] The technical solution of the present application is to query the paging address corresponding to the SectionObj by calling ZwQueryVirtualMemory, and release the occupancy through ZwUnmapViewOfSection after obtaining the corresponding paging address, so there is no problem of not being able to directly close the handle.
[0042] The following example illustrates this:
[0043] The file to be unlocked is d:\123.txt, marked as File1;
[0044] Process A opens File1 using CreateFile, with the access mode set to read and write, and prohibits other sharing.
[0045] The unlocking process is as follows:
[0046] The application opens the specified file with access 0 and share mode 7, obtains the handle, and records it as S1.
[0047] The driver kernel is called with S1 as parameter.
[0048] After the kernel obtains the handle S1, it converts it into a kernel object File_Obj and records the FsContext in it as S2.
[0049] Get the global kernel object handle table and start traversing. If the object type is a file type (the file type is explicitly declared in winddk), directly convert the object to File_Obj, and then directly compare its FsContext and S2. If they match, record the process ID and handle value in the relevant handle information until all handle tables are processed.
[0050] Process the reserved handle information in sequence as follows:
[0051] Duplicates the specified handle to the current process and uses the Duplicate_Close_Source parameter.
[0052] Close the handle generated after copying.
[0053] After all is completed, the file occupation of File1 is released.
[0054] Section object unlocking process:
[0055] Given the file d:\123.txt to be unlocked, process A opens the file and obtains the file handle H1. It passes H1 in through CreateFileMapping to obtain a mapping file object handle H2, and passes H2 in through MapViewOfFile to obtain a memory mapping address. Then CloseHandle closes the handles H1 and H2, but UnMapViewOfFile is never called. At this time, d:\123.txt is occupied by the section object.
[0056] The unlocking process is as follows:
[0057] L1: The application opens the specified file with access 0 and share mode 7, obtains the handle, and records it as S1; calls the driver kernel processing with S1 as a parameter; after the kernel obtains the handle S1, it converts it into the kernel object File_Obj, obtains the first FsContext, and records the first FsContext as S2.
[0058] L2: Get the global kernel object handle table and start traversing. The object type is Section type (Section type and the corresponding SECTION_OBJECT_POINTERS data structure are explicitly declared in winddk). Convert the handle to the data structure SECTION_OBJECT_POINTERS for the next step of comparison. Get the FilePointer in the DataSectionObject (corresponding to the CONTROL_AREA data structure) in SECTION_OBJECT_POINTERS.
[0059] L3: If FilePointer is not NULL, it indicates that the Section object is a file-mapped memory. By comparing the second FsContext of the object with S2, it can be determined whether the kernel object occupies the corresponding kernel object. If so, the process ID corresponding to the handle in the global handle table is used to obtain which process uses this mapping file object (mapping file from the application perspective, section object from the kernel perspective). Start traversing the virtual memory of the process, collect basic information through ZwQueryVirtualMemory to obtain the paging address and length, and then call ZwQueryVirtualMemory again to collect session name information to obtain the mapping file name. After comparing it to 123.txt, confirm the occupied memory mapping address. Pass the above mapping address through ZwUnmapViewOfSection to complete the entire unlocking process.
[0060] All the English "words or phrases" involved in the process of the present invention are not actually specific language descriptions but data structure definitions in NTDDK or a field name or a function name therein, not a language description. The File_Obj and Section_Obj described above are both described in Windows DDK and are not specifically defined in this patent.
[0061] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0062] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0063] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A method for releasing file occupation under Windows, characterized in that: The method comprises: Open the target file, obtain the target file reference identifier, and obtain the first FsContext field; specifically: open the target file in the kernel layer with AccessFlag as 0 and share mode as 7; obtain the target file reference identifier and obtain the first FsContext field; Traverse the system global handle table and get the second FsContext field; If the first FsContext field is the same as the second FsContext field, the target file is closed, including: For the kernel layer occupied in the form of File_obj, the handle corresponding to the second FsContext field is converted to the processing process by calling DuplicateHandle, and the parameter CLOSE_SOURCE is used when calling to close the handle corresponding to the first FsContext field; For the kernel layer occupied in the form of Section_Obj, the handle corresponding to the second FsContext field is released by calling the parameter ZwUnmapViewOfSection.
2. A method for releasing file occupation under Windows according to claim 1, characterized in that: For the occupation of the kernel layer in the form of File_obj, the handles of File_Obj in the system global handle table are traversed, and the handles are mapped to the File_Obj objects through the kernel method to obtain the second FsContext field.
3. The method for releasing a file in Windows according to claim 1, characterized in that: For the kernel layer's occupation in the form of Section_Obj, the handles of Section_Obj in the system global handle table are traversed, and the handles are mapped to the Section_Obj objects through the kernel method to obtain the second FsContext field.
