The following code is intended to restore windows file attributes (including file times). The file names and WIN32_FIND_DATA structures are stored in a file using fwrite (separate program). This program is intended to restore attributes and times for all files that match the input path. I have simplified as much as I can and still let you see what is going on.
The FindFirstFile appears to succeed (doesn't report an error to GetLastError), but the SetFileTime fails (return 0) and GetLastError shows 6 (invalid handle). What could cause the SetFileTime to fail if the FindFirstFile succeeded?
Any suggestions will be greatly appreciated.
Thanks,
Leon
Code:#include <stdio.h> #include <windows.h> int result; FILE *attrFilePtr; FILETIME ft; WIN32_FIND_DATA FindFileData; struct file_attrs { char filename[500]; WIN32_FIND_DATA attr_struct; } file_data; HANDLE hFile = INVALID_HANDLE_VALUE; main(int argc, char *argv[]) { if (argc != 3) { printf("usage is: %s <attrsfilename> <path>\n", argv[0]); exit(1); } attrFilePtr = fopen(argv[1], "r"); if (attrFilePtr == NULL) { printf("can't open file: %s\n", argv[1]); exit(2); } while ( (fread(&file_data, sizeof(struct file_attrs), 1, attrFilePtr)) != NULL) { if(strncmp(file_data.filename, argv[2], strlen(argv[2]) ) == 0) { printf("match on: %s\n", file_data.filename); SetFileAttributes(file_data.filename, file_data.attr_struct.dwFileAttributes); hFile = FindFirstFile(file_data.filename, &FindFileData); DWORD dw = GetLastError(); printf("GetLastError on findFirstFile returned: %d\n", dw); if (hFile == INVALID_HANDLE_VALUE) { printf("invalid handle\n"); exit(3); } SetFileTime(hFile, &file_data.attr_struct.ftCreatsionTime, &file_data.attr_struct.ftLastAccessTime, &file_data.attr_struct.ftLastWriteTime); dw = GetLastError(); printf("GetLastError on setfiletime returned: %d\n", dw); } } }



LinkBack URL
About LinkBacks


