1. Purpose of the experiment
When understanding the file system of Windows, how to manage information stored on storage media such as disks and CDs. And through the various APIs provided by the file system, the files are operated, and the functions and functions of the Windows file system are deeply understood, and the functions of the system calls of the file system are understood.
2. Experimental content
Complete a directory copy command mycp, including files and subdirectories under the directory.
Under Windows: Call CreateFile(), ReadFile(), WriteFile(), CloseHandle() and other functions to complete file copying.
(It is required that not only the read and write permissions of each file are consistent, but also the time attribute is consistent.)
3. Program design and implementation
1. Experimental environment
Operating system: Windows10
Development environment: Dev-Cpp Red Panda.C++.0.14.2.64-bit.10.3.installation version
2. Design ideas
① Structure analysis
WIN32_FIND_DATA structure:
typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes; //File attributes
FILETIME ftCreationTime; // file creation time
FILETIME ftLastAccessTime; // file last access time
FILETIME ftLastWriteTime; // file last modification time
DWORD nFileSizeHigh; // The file length is 32 bits high
DWORD nFileSizeLow; // file length low 32 bits
DWORD dwReserved0; // system reserved
DWORD dwReserved1; // system reserved
TCHAR cFileName[MAX_PATH]; // file name
TCHAR cAlternateFileName[ 14 ]; // Format file name
} WIN32_FIND_DATA, *PWIN32_FIND_DATA;
② Function function
FindFirstFile() function
HANDLE FindFirstFile(
LPCTSTR lpFileName, // directory name
LPWIN32_FIND_DATA lpFindFileData// file information structure);
FindNextFile() function
BOOL CreateDirectory(
LPCTSTR lpPathName, //file path
LPSECURITY_ATTRIBUTES lpSecurityAttributes//常为NULL);
CreateDirectory() function
BOOL CreateDirectory(
LPCTSTR lpPathName, //file path
LPSECURITY_ATTRIBUTES lpSecurityAttributes//常为NULL);
HANDLE CreateFile() function
HANDLE CreateFile(
LPCTSTR lpFileName, // pointer to the file name
DWORD dwDesiredAccess, // access mode (write/read)
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, //Security attributes
DWORD dwCreationDisposition, //Creation method
DWORD dwFlagsAndAttributes,//file attributes
HANDLE hTemplateFile//used to copy the file handle
);
SetFileTime() function
Long SetFileTime(
Long hFile, // file handle
FILETIME lpCreationTime, //File creation time
FILETIME lpLastAccessTime,//The last access time of the file
FILETIME lpLastWriteTime//The last modification time of the file
);
SetFileAttributes() function
BOOL SetFileAttributes(
LPCTSTR lpFileName, //file path
DWORD dwFileAttributes // file attributes);
BOOL WriteFile() function
BOOL WriteFile(
HANDLE hFile, // file handle
LPCVOID lpBuffer, // data buffer pointer
DWORD nNumberOfBytesToWrite, // number of bytes written
LPDWORD lpNumberOfBytesWritten, // Point to the actual number of bytes written
LPOVERLAPPED lpOverlapped // structure pointer, generally NULL);
BOOL ReadFile() function
BOOL ReadFile(
HANDLE hFile, //The handle of the file
LPVOID lpBuffer, //A buffer used to save the read data
DWORD nNumberOfBytesToRead, //Number of bytes to read
LPDWORD lpNumberOfBytesRead, //The pointer to the actual number of bytes read
LPOVERLAPPED lpOverlapped //generally set to NULL);
③ Program design and implementation
The design and implementation of the main function
In the main function, three parameters will be received from the command line, the first (argv[0]) is the path of the executable program to be executed, and the second (argv[1]) is the path of the file waiting to be copied ( hereinafter collectively referred to as the old path), and the third (argv[2]) is the target path to which the old file will be copied (hereinafter uniformly referred to as the new path).
The main function (main function) design process is as follows:
(1) In the main function, first judge whether the input parameters are 3, if so, go to (2), otherwise exit the execution.
(2) Call FindFIrstFile(argv[1], &lpFindData) to open the old file under the argv[1] path, return the handle hFindFile, and save its information into the lpFindNewData structure. If it is successfully opened, go to (3), otherwise exit the execution.
(3) Call FindFIrstFile(argv[2], &lpFindNewData) to check whether there is a file whose path is argv[2], if it exists, exit the execution, otherwise call CreateDirectory(argv[2], NULL) to create on the path of argv[2] new document.
(4) Call the MyCp() recursive function to copy the subfiles in the old folder.
(5) After the recursive function returns, call the CreateFile() function to open a new file on the argv[2] path, return the handle hDirFile, and call the SetFileTime() function to modify its time attribute, and call the SetFileAttributes() function to set the file attribute.
(6) Close the handles hFindFile and hDirFile, and set their values to INVALID_HANDEL_VALUE.
Design and Implementation of MyCp(char* OldPath, char* NewPath) Function
In this function, the incoming parameter OldPath is the old file path (source file), and NewPath is the new file path (target file). In addition, set the parameters new_path and old_path of char* type, which are used to concatenate the subfile paths in the directory. WIN32_FIND_DATA type structure lpFindData, handle hFindData, BOOL type parameter hFound. The MyCp() function flow is as follows:
(1) Define parameters old_path, new_path, structure lpFindData, handle hFindData, hFound.
(2) Assign values to old_path and new_path through the lstrcpy (str1, str2) interface, and the values are OldPath and NewPath respectively.
(3) Call FinFirstFile(old_path,&lpFindData) to find the file under the old_path path and return the handle hFindData, and store the file information in lpFindData.
(4) If the value of hFindData is INVALID_HANDLE_VALUE, it means that the file search fails, and the program exits; otherwise, go to (5).
(5) Call hFIndData(hFinData,&lpFindData) to find the next file in the folder on the OldPath path, and return hFound (indicating whether the search is successful), if successful, store the file information in lpFindData and go to (6), otherwise Indicates that the last file in the current folder has been traversed.
(6) The relative path of the file to be copied is stored in lpFindData.cFilename, and lstrcpy() is called.
(7) Call IsChildDir(lpFindData) to judge whether the current file is a subdirectory file.
(8) If it is not a directory file, but other files, call the CopyFile() function to complete the file copy.
(9) Close the handle hFindData, set its value to INVALID_HANDLE_VALUE, execute and return after completion.
Design and Implementation of CopyFile(char* OldPath, char* NewPath)
(1) Define the handle hOldFile (handle pointing to the old file), hNewFile (pointing to the handle of the new file), WIN32_FIND_DATA type parameter lpFindData (store file information).
(2) Call CreateFile() to open the old file under the OldPath path, and return the handle hOldFile.
(3) Call CreateFile() to create a new file under the NewPath path, and return the handle hNewFile.
(4) If hOldFile and hNewFile are not invalid values, go to (5), otherwise exit the execution.
(5) Circularly call ReadFile() to read from the old file and call WriteFile() to write to the new file, set the size BUFSIZE of each read and write to 1024, and record the size dwxfer of each actual read and write, when BUFSIZE is not equal to When dwxfer, it means that the copying is over and the loop is exited.
(6) Call the SetFileTime() function to set the file time attribute, and call the SetFileAttributes() function to set the file attribute.
(7) Close the file handles hNewFile and hOldFile, and set their values to INVALID_HANDLE_VALUE.
3. Write and compile code
Write C++ code in Dev-Cpp Red Panda.C++.0.14.2.64-bit.10.3.installation version to realize the corresponding file copy function.
4. Run to get the result
4. Experimental results and analysis
Through this experiment, I mastered the use of system calls for file and directory operations under the Windows operating system, understood its principle, and successfully realized the file copy function. Understand and master the implementation functions of the file system.
In the process of completing the experiment, there is an additional point that needs attention, that is, after obtaining the new file, we need to call the SetFileAttributes() function to complete the setting of the file access permission, and modify the permission of the new file for subsequent operations.
5. Experimental harvest and experience
"File copy" is a very important part of operating system knowledge. Through this experiment, I further deepened my understanding of the file system and system calls in the operating system, and deeply understood the process of file copying.
Through this experiment, I have mastered the relevant knowledge about the Windows file system, learned how to use the system call function of the file system, and at the same time have a deeper understanding of how the file system manages information stored on storage media such as disks and CDs. .
During the hands-on experiment, I gained a deeper understanding of the knowledge points in this chapter, and also reviewed the knowledge of programming. This experiment has taught me a lot!
Project source code and experiment report: https://github.com/YourHealer/OS-File-Copy.git