Disclosure of Invention
The invention aims to provide a method for migrating a Windows system by a file copy mode, which aims to solve the problem that the prior art proposal provided in the background art cannot efficiently migrate the Windows system to other disks.
In order to solve the technical problems, the invention adopts the following technical scheme:
A method for migrating a Windows system by means of file copying, comprising the steps of:
step S1, firstly judging whether the backup system environment is a PE system or not, if so, not creating a snapshot, and if not, firstly creating a snapshot for a partition of a source system;
step S2, traversing all files or folders in the source system partition;
Step S3, judging whether the file name in the traversal return value is a special character, if so, skipping and jumping to step S6, and if not, entering step S4;
Step S4, judging whether the file attribute is a folder, if so, entering step S5, if so, copying the document to a target partition and jumping to step S6;
S5, judging whether the folder is a redirection folder, if so, configuring the redirection folder in a target partition so that the redirection folder keeps the original direction, if not, creating the folder in the target partition, setting file attributes in the source folder into the folder created in the target partition, and acquiring ownership and ACL information of the source folder and setting the ownership and ACL information into the newly created folder;
Step S6, searching for the rest files or folders continuously until all files or folders are copied;
and step S7, repairing MountedDevices and repairing system guidance to finish migration.
According to the above technical solution, in step S1, when a snapshot of the source system is created, the snapshot is created through create in the ISn apshotMgr module, and after the snapshot is created successfully, the sign link of the snapshot is obtained by using GetSymbolByDriveLetter function in the ISnap shotMgr module.
According to the above technical solution, in step S2, the windows api function FINDFIRSTFILE is used to search the file from the source partition root directory.
According to the technical scheme, in the step S3, whether the FILE ATTRIBUTE is the folder body is judged, namely whether the FILE ATTRIBUTE is the folder is judged according to the FILE ATTRIBUTE in the traversal return value, if dwFileAttributes & file_attribute_direction condition is true, the FILE is indicated to be the folder, and otherwise, the FILE is the FILE.
According to the technical scheme, in the step S5, whether the folder is redirected is judged to be a common folder or not if dwFileAttributes & file_ATTRIBUTE_REPARSE_POIN condition is true, which indicates that the folder is redirected to other folders, and if false.
According to the above technical solution, in step S5, the configuration of the redirection folder includes creating the same folder, setting the folder attribute, obtaining the redirection directory, and setting the redirection directory.
According to the technical scheme, the same folder is created in the target partition, and the folder attribute is set by using windowsAPICreateDirectory to create the redirection folder in the target partition, and setting the dwFileAttributes value read from the source file into the newly created folder by using an API function SetFileAttributes.
According to the technical scheme, the redirection catalog is obtained from the source folder, specifically, an API function CreateFile function is used for opening a handle of the source folder, a DeviceIoControl function is used, and an FSCTL_GET_REPARSE_POINT control code is used for obtaining redirection data in the source folder;
Judging the type of the data according to the link type in the redirected data, wherein the judging method is REPARSETAG = = IO_REPARSE_TAG_MOUNT_POINT, if the condition is true, the obtained data is mounting POINT data, a redirected folder address is contained in the obtained data, and otherwise, the obtained data is not contained;
Further, the method comprises the steps of, the data type includes IO_REPARSE tag_MOUNT_POINT tag_mobile POINT. Wherein the type of IO_REPARSE_TAG_MOUNT_POINT corresponds to MountPointReparseBuffer fabric in the REPARSE_DATA_BUFFER's complex, the required redirection folder is inside PathBuffer in MountPointReparseBuffer fabric.
According to PathBuffer in the REPARSE_DATA_BUFFER structure, the path is the path of the redirection folder, and the address of the redirection folder is saved so as to be set in the newly built folder of the target partition.
According to the technical scheme, the redirection catalog is set for the newly created folder in the target partition, specifically, a windowsAPI function CreateFile function is used for opening a handle of the folder created in the target partition, and data of a REPARSE structure is initialized, wherein REPARSETAG is equal to the length of IO_REPARSE_TAG_MOUNT_ POINT, reparseDataLength and calculated according to the actual length of the redirection catalog;
Next, the complex in the structure of reparse_data_buffer uses the structure of MountPointReparseBuffer, the DATA in MountPointReparseBuffer is populated according to the acquired DATA of the redirection folder, and then the REPARSE DATA of the folder in the target partition is set using the windowsAPI function DeviceIoControl function and using the fsctl_get_reparse_point control code.
According to the technical scheme, ownership and ACL information of the source folder are set into the newly created folder of the target partition:
The size of the source file or folder security attribute is obtained using windowsAPI function GetFileSecurity, a piece of memory space of the obtained size is allocated using HeapAlloc function and the space data is all initialized to 0, the security attribute of the source file or folder is obtained using windows API function GetFileSecurity, then the obtained security attribute is set to the file or folder of the target partition using windowsAPI function SetFileSecurity, and finally the allocated memory space is released.
Compared with the prior art, the invention has the following beneficial effects:
According to the scheme, all data in the source system, including system files, application program files, user data and the like, can be completely migrated to the target partition through traversing and copying file by file and folder. When copying the folder, the attributes (such as authority, ownership, access Control List (ACL), etc.) of the source folder are copied to the target folder, so that the user authority and the file security are ensured to be consistent. Special handling of the redirection folders can keep the original pointing of these folders, such as "my documents", "desktop", etc., ensuring that the user's settings and habits remain unchanged after migration. By repairing MountedDevices and system guiding, the new system can be ensured to be started normally, and the starting problem caused by migration is avoided. By skipping special character files and flexibly processing different types of files, the user friendliness of the migration process is enhanced, and the possibility of manual intervention and operation errors of a user is reduced. The method is suitable for migration of various Windows systems, whether data migration of individual users or migration of systems in enterprise environments.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
Example 1
As shown in fig. 1, a method for migrating a Windows system by means of file copying includes the following steps:
step S1, firstly judging whether the backup system environment is a PE system or not, if so, not creating a snapshot, and if not, firstly creating a snapshot for a partition of a source system;
Step S2, traversing all files in the source system partition;
Step S3, judging whether the file name in the traversal return value is a special character, if so, skipping and jumping to step S6, and if not, entering step S4;
Step S4, judging whether the file attribute is a folder, if so, entering a step S5, and if so, copying the file to a target partition and jumping to a step S6;
S5, judging whether the folder is a redirection folder, if so, configuring the redirection folder in a target partition so that the redirection folder keeps the original direction, if not, creating the folder in the target partition, setting file attributes in the source folder into the folder created by the target partition, and acquiring ownership and ACL information of the source folder and setting the ownership and ACL information into the newly created folder;
Step S6, searching for the rest files or folders continuously until all files or folders are copied;
and step S7, repairing MountedDevices and repairing system guidance to finish migration.
According to the scheme, all data in the source system, including system files, application program files, user data and the like, can be completely migrated to the target partition through traversing and copying file by file and folder. When copying the folder, the attributes (such as authority, ownership, access Control List (ACL), etc.) of the source folder are copied to the target folder, so that the user authority and the file security are ensured to be consistent. Special handling of the redirection folders can keep the original pointing of these folders, such as "my documents", "desktop", etc., ensuring that the user's settings and habits remain unchanged after migration. By repairing MountedDevices and system guiding, the new system can be ensured to be started normally, and the starting problem caused by migration is avoided. By skipping special character files and flexibly processing different types of files, the user friendliness of the migration process is enhanced, and the possibility of manual intervention and operation errors of a user is reduced. The method is suitable for migration of various Windows systems, whether data migration of individual users or migration of systems in enterprise environments.
Example two
The present example provides a specific implementation.
And step 1, when the partition is migrated under the non-pe system, firstly, taking a snapshot of the partition of the source system, creating the snapshot by using the existing module ISnapshotMgr, calling the create in the module to create the snapshot, and obtaining the symbolic link of the snapshot by using the GetSymbolByDriveLetter function in the module after the snapshot is successfully created. The snapshot step is skipped under pe, directly using the source-and-path.
Step 2, traversing all files/folders in the source partition. Files/folders are looked up starting from the source partition and directory using the windows api function FINDFIRSTFILE, and the file names are compared using the _ tcsicmp function to skip special character files according to the file names in the return value of FINDFIRSTFILE function parameter 2, including the "," $recycle. Bin "and" SyetemVolume Information "directories (which are available for each partition, skipped to increase traversal speed).
Further, parameter 2 of FINDFIRSTFILE is a structure of WIN32_FIND_DATA, which is defined by Microsoft, and contains some basic information of files, including information of attributes, names, dates, and the like of the files.
Further, the file name is a member of the WIN32_FIND_DATA structure and is consistent with the file name exposed in the windows file browser.
The next file is looked up using FindnextFileAPI functions (beginning traversal inside each folder is using FINDFIRSTFILE, looking up the remaining files is using FindnextFile, and traversing through the api to all documents under a folder is accomplished using a combination of both apis).
If the FILE name returned in the parameter 2 is not the FILE to be skipped, judging whether the FILE is a folder according to dwFileAttributes FILE ATTRIBUTEs in the parameter 2 structure, wherein the judging method is dwFileAttributes & file_attribute_direction, if the condition is true, the FILE is indicated to be the folder, otherwise, the FILE is the FILE.
Specifically, dwFileAttributes is the first parameter in WIN32_FIND_DATA (Structure defined by Microsoft), which is used as a parameter for FINDFIRSTFILE or FindNextFile. Types dwFileAttributes include FILE_ATTRIBUTE_HIDDEN、FILE_ATTRIBUTE_SYSTEM、FILE_ATTRIBUTE_DIRECTORY、FILE_ATTRIBUTE_NORMAL、FILE_ATTRIBUTE_REPARSE_POINT (these types are defined by microsoft), so that dwFileAttributes and file_attribute_direction in the WIN32_find_data DATA obtained in FINDFIRSTFILE or FindNextFile are used to determine whether a FILE is a folder.
If it is a folder:
Step 201, it is determined whether the folder is a redirection folder, and the determination method is dwFileAttributes & file_attrip_reparse_point, if the condition is true, it indicates that the folder is redirected to another folder, when the folder is accessed, it is actually the location of the accessed redirection folder, and if the condition is false, it indicates that it is a common folder.
If it is a redirect folder, the condition is true:
in step 2011, the folder is created in the target partition using windows APICreateDirectory, and the value of dwFileAttributes read from the source file is set into the newly created folder using API function SetFileAttributes.
In step 2012, the folder to which the folder is redirected is obtained from the source folder, namely, a handle of the source folder is opened by using an API function CreateFile function, and redirection data in the source folder is obtained by using a DeviceIoControl function and using an FSCTL_GET_REPARSE_POINT control code. According to the link type judgment in the redirection DATA, the judgment method is REPARSETAG = = IO_REPARSE_TAG_MOUNT_POINT, if the condition is true, the obtained DATA is the mounting POINT DATA, a redirection folder address is contained inside, and according to the structure of the REPARSE_DATA_BUFFER (the redirection folder is saved in PathBuffer in MountPointReparseBuffer in the REPARSE_DATA_BUFFER structure, wherein the REPARSE_DATA_BUFFER is a structure defined by a user.
Since "\??\C:\xxxx", is only needed after "C: \" in PathBuffer, 4 is needed after PathBuffer in wcscpy _s (PATH, MAX_PATH, pReparse- > MountPointReparteBuffer.PathBuffer+4) is used at the time of copying, wherein PATH is the PATH for redirecting folders, and the PATH is needed when the PATH is set to the folder corresponding to the target partition.
And step 2013, saving the address of the redirected folder so as to be set in the newly-built folder of the target partition later. Because the source folder is redirected to other folders, when the source folder is opened, the redirected directory is actually accessed, and the target partition needs to create the same folder as the source folder and has the characteristics, the newly created folder needs to be provided with the same redirected directory, that is, the characteristics of the folders in the source folder need to be all set in the corresponding folder of the target partition.
The structure of the REPARSE_DATA_BUFFER is as follows:
In step 2014, creating a redirection directory for the newly created folder in the target partition, opening a handle of the folder created in the target partition by using a windows API function create function, initializing DATA of a REPARSE structure, wherein REPARSETAG is equal to the length of IO_REPARSE_TAG_MOUNT_ POINT, reparseDataLength and calculated according to the actual length of the redirection directory, fixing the value of Reserved to be equal to 2, then using a MountPointReparseBuffer structure by a union in the REPARSE_DATA_BUFFER structure, filling DATA in MountPointReparseBuffer according to the acquired DATA of the redirection folder, wherein PathBuffer under filling MountPointReparseBuffer needs to be filled "\??\" before copying the redirection address saved above to the back, initializing the DATA in detail as follows, and then setting the REPARSE DATA of the folder in the target partition by using a windowsAPI function DeviceIorol function and using an FSCTL_GET_REPAR_POINT control code. Where parameter 1 in CreateMountPoint is the folder created in the target partition and parameter 2 is the redirect folder obtained in step 2012 above.
Initialization code of REPARSE_DATA_BUFFER structure:
In step 2015, the ownership and ACL INFORMATION of the source folder are set to the newly created folder of the target partition, a windowsAPI function GetFileSecurity is used to acquire the size of the source file/folder SECURITY attribute, wherein the second parameter needs to contain three INFORMATION of OWNER, group and authority corresponding to the source file/folder, the size is returned through the last parameter of the function, heapAlloc function is used to allocate a section of memory space with acquired size and initialize all space data to 0, windows API function GetFileSecurity is used to acquire the SECURITY attribute of the source file/folder, the second parameter needs to contain macros corresponding to the OWNER, group and authority, and then windows API function SetFileSecurity is used to set the acquired SECURITY attribute to the file/folder of the target partition, wherein the second parameter also needs to contain the OWNERs, group and finally the allocated memory space with the corresponding macros.
Step 2016, the next file/folder is continued to be looked up using FindNextFile API functions.
Step 202, if the folder is only a normal folder:
In step 2021, the folder is created in the target partition using the windows API function CreateDirectory, and the file attributes dwFileAttributes in the source folder are set into the folder created in the target partition using the windows API function SetFileAttributes.
Step 2022, the ownership and ACL information of the source folder is obtained and set into the newly created folder.
Step 2023, go back to step 2022 to recursively refer to other files/folders under the current directory.
And 3, if the file is not a folder, copying the file from the source directory to the corresponding directory in the target partition by using a windows API function copyfile.
In step 301, dwFileAttributes attributes in the source file are set to the file corresponding to the target partition using windowsAPI function SetFileAttributes.
Step 302, continue searching for the next file using FindNextFileAPI functions. Steps 301 through 302 are then repeated until FindNextFile returns to FALSE.
At step 303, repair System under file MountedDevices, mount the SYSTEM registry file in the Windows\System32\config directory in the registry using the regLoadKey API function, delete MountedDevices all values in the mounted registry directory computer\HKEY_local_MACHINE\xxx MountedDevices (where xxx is the name of the SYSTEM file mounted in the registry). Preparing new binary data, wherein if the disk of the target partition is MBR, the data is 4-byte disk signature+8-byte starting position of the target partition, and if the disk of the target partition is GPT, the data is DMIO:ID+GUID of the partition. Then, a numerical name of DosDevices \C is created under MountedDevices, and the numerical data is the value of the binary data so as to achieve the aim of repairing the target partition system DosDevices, thereby avoiding the problem of always displaying a black screen or a splash screen after the power-on.
And step 4, repairing the system guidance of the target partition by using the existing module, and ensuring that the system can be normally booted.
Technical term analysis:
Redirecting folders-is the directing or "redirecting" of the contents of one folder to another location. When a user accesses the redirect directory, the system will automatically direct them to access the contents of the target location, rather than the actual path of the redirect directory itself.
Ownership and ACL information of the folder are two important rights management concepts in a file system, and are mainly used for controlling access rights of users and groups to files or folders.
GPT/MBR is a type of two disks, differing in the manner of management of the partition that maximally supports the size of the disk.
Registry is an important database in microsoft windows for storing system and application settings information.
It is noted that relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus.
It should be noted that the above-mentioned embodiments are merely preferred embodiments of the present invention, and the present invention is not limited thereto, but may be modified or substituted for some of the technical features thereof by those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.