2016-05-25 25 views
0

Ich versuche, den folgenden Code (ein einfaches Sicherungsprogramm) auszuführen, aber aus irgendeinem Grund konnte das Feld tv_sec nicht aufgelöst werden. Ich habe alles versucht, was mir in den Sinn kam - aber keine Hilfe. möchte ich betonen, dass ich die anderen relevanten Fragen auf dieser Website gelesen haben, und auch versucht, die Zeile hinzufügen:Das Feld tv_sec konnte nicht aufgelöst werden

#include <sys/time.h> 

aber - wieder - es hat nicht funktioniert.

Können Sie mir helfen?

Danke euch allen!

Ich habe mich entschieden, das ganze Programm an beliebiger Stelle anzuhängen, damit Sie bei Bedarf das ganze Bild sehen können. Der Code (die problematische Zeile unter der Notiz ist // <> //):

#include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <unistd.h> 
    #include <fcntl.h> 
    #include <utime.h> 
    #include <dirent.h> 
    #include <libgen.h> 

    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) 

    // The header of each backed-up file 
    typedef struct { 
     char name[100]; 
     char mode[8]; 
     char uid[8]; 
     char gid[8]; 
     char size[12]; 
     char mtime[12]; 
     char typeflag; 
    } bkp_header; 

    // Function declarations 
    int backup(char *src, char *dest); 
    int restore(char *src); 
    int backup_abstract(char *src, int out_file); 
    int backup_file(char *name, struct stat buf, int out_file); 
    int backup_dir(char *name, struct stat buf, int out_file); 
    int backup_symlink(char *name, struct stat buf, int out_file); 
    int restore_file(bkp_header *file_header, int bkp_file); 
    int restore_dir(bkp_header *file_header, int bkp_file); 
    int restore_symlink(bkp_header *file_header, int bkp_file); 
    int restore_metadata(bkp_header *file_header); 
    bkp_header* get_file_header(char *name, struct stat buf, char type); 
    int write_header_to_file(char *name, struct stat buf, int out_file, char type); 

    char* backup_dir_name; 

    // Main 
    int main(int argc, char *argv[]) { 
     int result = 1; 

     if (argc < 3) { 
      fprintf(stderr, "Invalid parameters\n"); 
     } 
     else { 
      // Backup 
      if (strncmp(argv[1], "-c", 2) == 0) { 
       if (argc < 4) { 
        fprintf(stderr, "Please insert the path you want to backup\n"); 
       } 
       else { 
        result = backup(argv[3], argv[2]); 
       } 
      } 
      // Restore 
      else if (strncmp(argv[1], "-x", 2) == 0) { 
       result = restore(argv[2]); 
      } 
      else { 
       fprintf(stderr, "Invalid operation\n"); 
      } 
     } 
     exit(result); 
    } 

    // Takes a 'src' path and runs a backup to the 'dest' path 
    int backup(char *src, char *dest) { 
     // Create the backup file 
     int out_file = open(dest, O_TRUNC | O_CREAT | O_WRONLY); 
     if (out_file < 0) { 
      fprintf(stdout, "Failed opening file for writing."); 
      return -1; 
     } 

     // Get the root backup directory 
     char *src_dup = strdup(src); 
     backup_dir_name = dirname(src_dup); 

     // Run the backup based on the type 
     int result = backup_abstract(src, out_file); 
     close(out_file); 
     return result; 
    } 

    // Backup something - a file, a folder or a symbolic link 
    int backup_abstract(char *src, int out_file) { 
     // Get the 'src' lstat 
     struct stat buf; 
     if (lstat(src, &buf) < 0) { 
      fprintf(stderr, "lstat error"); 
      return -1; 
     } 

     if (S_ISDIR(buf.st_mode)) { 
      return backup_dir(src, buf, out_file); 
     } 
     else if (S_ISREG(buf.st_mode)) { 
      return backup_file(src, buf, out_file); 
     } 
     #ifdef S_ISLNK 
     else if (S_ISLNK(buf.st_mode)) { 
      return backup_symlink(src, buf, out_file); 
     } 
     #endif 
     else { 
      fprintf(stderr, "Can only backup a regular file, a directory, or a symlink\n"); 
     } 
     return -1; 
    } 

    // Backup a single file 
    int backup_file(char *name, struct stat buf, int out_file) { 
     if (write_header_to_file(name, buf, out_file, 'r') < 0) { 
      return -1; 
     } 

     // Copy the file right after the header in 8K chunks 
     int in_file = open(name, O_RDONLY); 
     if (in_file < 0) { 
      fprintf(stdout, "Failed opening file for reading."); 
      return -1; 
     } 
     char read_buf[8192]; 
     while (1) { 
      ssize_t result = read(in_file, read_buf, sizeof(read_buf)); 
      if (result < 0) { 
       fprintf(stdout, "Failed reading from file."); 
       return -1; 
      } 
      if (result == 0) { 
       break; /* EOF */ 
      } 
      if (write(out_file, read_buf, (int)result) != (int)result) { 
       fprintf(stdout, "Failed writing to file."); 
       return -1; 
      } 
     } 
     close(in_file); 
     return 0; 
    } 

    // Backup a directory 
    int backup_dir(char *name, struct stat buf, int out_file) { 
     if (write_header_to_file(name, buf, out_file, 'd') < 0) { 
      return -1; 
     } 

     // Backup the contents of this directory 
     DIR *dir; 
     struct dirent *ent; 
     if ((dir = opendir(name)) == NULL) { 
      fprintf(stdout, "Failed opening directory for reading."); 
      return -1; 
     } 
     while ((ent = readdir(dir)) != NULL) { 
      if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) { 
       char full_path[100]; 
       strcpy(full_path, name); 
       strcat(full_path, "/"); 
       strcat(full_path, ent->d_name); 

       if (backup_abstract(full_path, out_file) != 0) { 
        return -1; 
       } 
      } 
     } 
     closedir(dir); 
     return 0; 
    } 

    // Backup a symbolic link 
    int backup_symlink(char *name, struct stat buf, int out_file) { 
     if (write_header_to_file(name, buf, out_file, 'l') < 0) { 
      return -1; 
     } 

     char *link_target = malloc(buf.st_size + 1); 
     if (link_target == NULL) { 
      fprintf(stdout, "Failed allocating memory."); 
      return -1; 
     } 
     ssize_t link = readlink(name, link_target, buf.st_size + 1); 
     if (link < 0) { 
      fprintf(stderr, "lstat error"); 
      return -1; 
     } 
     link_target[buf.st_size] = '\0'; 
     if (write(out_file, link_target, buf.st_size) != buf.st_size) { 
      fprintf(stdout, "Failed writing to file."); 
      return -1; 
     } 
     free(link_target); 
     return 0; 
    } 

    // Get the backup header for a file 
    bkp_header* get_file_header(char *name, struct stat buf, char type) { 
     bkp_header *file_header = (bkp_header*)malloc(sizeof(bkp_header)); 

     int abs_path_start = strncmp(name, backup_dir_name, strlen(backup_dir_name)); 
     if (abs_path_start != 0) { 
      fprintf(stdout, "Warning: Failed converting backup path to relative."); 
     } 
     else { 
      name = &name[strlen(backup_dir_name)+1]; 
     } 

     sprintf(file_header->name, name); 
     sprintf(file_header->mode, "%d", buf.st_mode); 
     sprintf(file_header->uid, "%d", buf.st_uid); 
     sprintf(file_header->gid, "%d", buf.st_gid); 
     sprintf(file_header->size, "%d", (int)buf.st_size); 
//<<PROBLEM!!!>>// 
     sprintf(file_header->mtime, "%d", (int)buf.st_mtim.tv_sec); 
     file_header->typeflag = type; 

     return file_header; 
    } 

    // Create and write the header to the output file 
    int write_header_to_file(char *name, struct stat buf, int out_file, char type) { 
     bkp_header *file_header = get_file_header(name, buf, type); 
     if (write(out_file, file_header, sizeof(bkp_header)) != sizeof(bkp_header)) { 
      fprintf(stdout, "Failed writing to file."); 
      return -1; 
     } 
     return 0; 
    } 

    // Takes a 'src' backup file path and extracts it to the current working dir 
    int restore(char *src) { 
     // Read the backup file 
     int bkp_file = open(src, O_RDONLY); 
     if (bkp_file < 0) { 
      fprintf(stdout, "Failed opening backup file for reading."); 
      return -1; 
     } 
     while (1) { 
      // Read the header 
      char header_buf[sizeof(bkp_header)]; 
      ssize_t h_result = read(bkp_file, header_buf, sizeof(header_buf)); 
      if (h_result < 0) { 
       fprintf(stdout, "Failed reading from backup file."); 
       return -1; 
      } 
      if (h_result == 0) { 
       break; /* EOF */ 
      } 
      bkp_header *file_header = header_buf; 

      // Restore the file/dir/symbolic-link 
      if (file_header->typeflag == 'r') { 
       if (restore_file(file_header, bkp_file) < 0) { 
        return -1; 
       } 
      } 
      else if (file_header->typeflag == 'd') { 
       if (restore_dir(file_header, bkp_file) < 0) { 
        return -1; 
       } 
      } 
      else if (file_header->typeflag == 'l') { 
       if (restore_symlink(file_header, bkp_file) < 0) { 
        return -1; 
       } 
      } 
      else { 
       fprintf(stdout, "Failed restoring, unknown file type found."); 
       return -1; 
      } 
     } 
     close(bkp_file); 
     return 0; 
    } 

    // Restore a single file based on the header and the raw bkp_file 
    int restore_file(bkp_header *file_header, int bkp_file) { 
     // Make sure the output file does not exist 
     int out_file; 
     out_file = open(file_header->name, O_RDONLY); 
     if (out_file != -1) { 
      close(out_file); 
      fprintf(stdout, "Failed restoring, file %s already exists.", file_header->name); 
      return -1; 
     } 

     // Create and open it for writing 
     out_file = open(file_header->name, O_WRONLY | O_CREAT); 
     if (out_file < 0) { 
      fprintf(stdout, "Failed opening backed-up file path for writing."); 
      return -1; 
     } 

     // Restore the file's meta data 
     if (restore_metadata(file_header) != 0) { 
      fprintf(stdout, "Failed restoring backed-up file meta data."); 
      return -1; 
     } 

     // Read the content of the file in 8K chunks, and write them to disk 
     int file_size = atoi(file_header->size); 
     while (file_size) { 
      // Read a chunk from the backup file 
      char file_buf[8192]; 
      ssize_t f_result = read(bkp_file, file_buf, MIN(sizeof(file_buf), file_size)); 
      if (f_result < 0) { 
       fprintf(stdout, "Failed reading from backup file."); 
       return -1; 
      } 
      if (f_result == 0) { 
       break; /* EOF */ 
      } 

      // Write this chunk to the disc 
      if (write(out_file, file_buf, (int)f_result) != (int)f_result) { 
       fprintf(stdout, "Failed writing file to disc."); 
       return -1; 
      } 
      file_size -= f_result; 
     } 
     close(out_file); 
     return 0; 
    } 

    // Restore a directory based on the header and the raw bkp_file 
    int restore_dir(bkp_header *file_header, int bkp_file) { 
     // Create this folder on the disc 
     if (mkdir(file_header->name, atoi(file_header->mode)) != 0) { 
      fprintf(stdout, "Failed creating directory."); 
      return -1; 
     } 

     // Restore the directory's meta data 
     if (restore_metadata(file_header) != 0) { 
      fprintf(stdout, "Failed restoring backed-up directory's meta data."); 
      return -1; 
     } 
     return 0; 
    } 

    // Restore a symbolic link (not the target file) based on the header and the raw bkp_file 
    int restore_symlink(bkp_header *file_header, int bkp_file) { 
     // Make sure the output file does not exist 
     int out_file; 
     out_file = open(file_header->name, O_RDONLY); 
     if (out_file != -1) { 
      close(out_file); 
      fprintf(stdout, "Failed restoring, symlink %s already exists.", file_header->name); 
      return -1; 
     } 

     int path_size = atoi(file_header->size); 
     char *file_buf = malloc(sizeof(char)*path_size); 
     if (file_buf == NULL) { 
      fprintf(stdout, "Failed allocating memory."); 
      return -1; 
     } 

     // Read the content of the file, get the link target 
     ssize_t f_result = read(bkp_file, file_buf, path_size); 
     if (f_result < 0) { 
      fprintf(stdout, "Failed reading from backup file."); 
      return -1; 
     } 

     // Create this symbolic link on the disc 
     if (symlink(file_buf, file_header->name) != 0) { 
      fprintf(stdout, "Failed creating symlink."); 
      return -1; 
     } 

     // Restore the link's meta data 
     if (restore_metadata(file_header) != 0) { 
      fprintf(stdout, "Failed restoring backed-up link meta data."); 
      return -1; 
     } 
     return 0; 
    } 


    // Set the original UID, GID, permissions and modification time 
    int restore_metadata(bkp_header *file_header) { 
     if (lchown(file_header->name, atoi(file_header->uid), atoi(file_header->gid)) != 0) { 
      fprintf(stdout, "chown error"); 
      return -1; 
     } 
     if (file_header->typeflag != 'l') { 
      // No lchmod in my glibc, not restoring mode for symlinks 
      if (chmod(file_header->name, atoi(file_header->mode)) != 0) { 
       fprintf(stdout, "chmod error"); 
       return -1; 
      } 
      struct utimbuf ubuf; 
      ubuf.modtime = atoi(file_header->mtime); 
      if (utime(file_header->name, &ubuf) != 0) { 
       fprintf(stdout, "utime error"); 
       return -1; 
      } 
     } 
     return 0; 
    } 
+0

Anstatt die Fehlermeldung "das Feld tv_sec konnte nicht aufgelöst werden" zu beschreiben, geben Sie die Fehlermeldung ein. – chux

+0

Bitte veröffentlichen Sie das [Minimale, vollständige und überprüfbare Beispiel] (http://stackoverflow.com/help/mcve), das den Fehler anzeigt. –

+0

Ist dies auf einer aktuellen Linux-Distribution, oder etwas anderes? – Dmitri

Antwort

1

buf hat struct stat Typ, bei dem time_t ein st_mtime Feld vom Typ existiert. time_t wird normalerweise als ein arithmetischer Typ definiert, nicht als strukturierter Typ, er hat dann kein Feld.

Verwandte Themen