create.c

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.5 kB
1
Indexable
Never
  1 #include "create.h"
  2 #include "header.h"
  3 #include "write.h"
  4 int create_archive(char *tarfile){
  5     int tar_fd;
  6
  7     /* Open tar_file with read write user permissions */
  8     tar_fd = open(tarfile, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
  9
 10     if(tar_fd == -1){
 11         perror("open");
 12         exit(EXIT_FAILURE);
 13     }
 14
 15     return tar_fd;
 16 }
 17
 18 void archive_file(int fd, char *parent_path, char *file_name){
 19     struct stat file_sb;
 20     struct passwd *file_pwd;
 21     struct group *file_group;
 22     struct dirent *dir_entry;
 23     DIR *directory;
 24     char *full_path;
 25     header *header;
 26     /* Stat current file and exit if it failed */
 27     if(lstat(file_name, &file_sb) == -1){
 28         perror("lstat");
 29         exit(EXIT_FAILURE);
 30     }
 31
 32     /* Get user password and group fro user name and group name */
 33     file_group = getgrgid(file_sb.st_gid);
 34
 35     if(file_group == NULL){
 36         perror("get group name");
 37         exit(EXIT_FAILURE);
 38     }
 39
 40     file_pwd = getpwuid(file_sb.st_uid);
 41
 42     if(file_pwd == NULL){
 43         perror("get user name");
 44         exit(EXIT_FAILURE);
 45     }
 46
 47     /* Create full path up to this point */
 48     full_path = create_full_path(parent_path, file_name, file_sb.st_mode);
 49     header = create_header(file_sb, full_path);
 50     write_header(fd, header);
"create.c" 145L, 3989C
Leave a Comment