diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-20 17:17:36 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-20 17:17:36 +0200 |
| commit | 0fa3308034c7776b6c078e493f3b758f0a0bf7e6 (patch) | |
| tree | e4f2ea5954009bfa3fcd3caf6196a4b4a9f4dc87 /src/fs.c | |
| parent | 95e1552a898078b4dec1eae3fcf975b392cfad6d (diff) | |
| download | tar-0fa3308034c7776b6c078e493f3b758f0a0bf7e6.tar.gz tar-0fa3308034c7776b6c078e493f3b758f0a0bf7e6.tar.bz2 tar-0fa3308034c7776b6c078e493f3b758f0a0bf7e6.zip | |
Added recursion for directory, not complete ustar format
Diffstat (limited to 'src/fs.c')
| -rw-r--r-- | src/fs.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/fs.c b/src/fs.c new file mode 100644 index 0000000..78ef25a --- /dev/null +++ b/src/fs.c @@ -0,0 +1,85 @@ +#include "tar.h" + +int file_content_write(int fd, int file_fd, struct stat *statbuf) +{ + char *content; + + if ((content = malloc(statbuf->st_size)) == NULL) + { + perror("file_content_write"); + close(file_fd); + return -1; + } + if (read(file_fd, content, statbuf->st_size) == -1) + { + perror("file_content_write"); + close(file_fd); + free(content); + return -1; + } + int ret = record_write(fd, content, statbuf->st_size); + close(file_fd); + free(content); + return (ret); +} + +int file_write(int fd, char file_name[PATH_MAX]) +{ + int file_fd; + struct stat statbuf; + + fprintf(stderr, "|%s|\n", file_name); + if (stat(file_name, &statbuf) == -1) + { + perror("file_write stat"); + return -1; + } + if (header_write(fd, file_name, &statbuf) == -1) + return -1; + + switch (statbuf.st_mode & S_IFMT) + { + case S_IFDIR: + return directory_write(fd, file_name); + default: + if ((file_fd = open(file_name, O_RDONLY)) == -1) + { + perror("file_write open"); + return -1; + } + return file_content_write(fd, file_fd, &statbuf); + } + return (0); +} + +int directory_write(int fd, char dir_name[PATH_MAX]) +{ + DIR *dir; + struct dirent *entry; + size_t len; + + if ((dir = opendir(dir_name)) == NULL) + { + perror("directory_write"); + return -1; + } + len = strlen(dir_name); + strcat(dir_name, "/"); + len++; + while ((entry = readdir(dir)) != NULL) + { + if (strcmp(entry->d_name, ".") == 0 || + strcmp(entry->d_name, "..") == 0) + continue; + strcat(dir_name, entry->d_name); + if (file_write(fd, dir_name) == -1) + { + closedir(dir); + return -1; + } + dir_name[len] = '\0'; + } + dir_name[len - 1] = '\0'; + closedir(dir); + return 0; +} |
