aboutsummaryrefslogtreecommitdiff
path: root/src/archive.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/archive.c')
-rw-r--r--src/archive.c77
1 files changed, 40 insertions, 37 deletions
diff --git a/src/archive.c b/src/archive.c
index a3addd3..48fa55d 100644
--- a/src/archive.c
+++ b/src/archive.c
@@ -1,59 +1,28 @@
#include "tar.h"
-int archive_write(char *archive_file_name, char **files)
+int archive_create(int archive_fd, char **files)
{
- int fd;
char file_name[PATH_MAX];
struct stat root_statbuf;
- if (archive_file_name == NULL)
- fd = STDOUT_FILENO;
- else
- {
- fd = open(archive_file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd == -1)
- {
- perror("achive_write");
- return -1;
- }
- }
for (; *files != NULL; files++)
{
bzero(file_name, PATH_MAX);
strcpy(file_name, *files);
- file_write(fd, file_name);
+ file_write(archive_fd, file_name);
}
if (stat("/", &root_statbuf) == -1)
{
perror("achive_write");
- close(fd);
return -1;
}
- if (record_write_blank(fd, 2 + (2 * root_statbuf.st_blksize) / RECORD_SIZE) == -1)
- {
- close(fd);
+ if (record_write_blank(archive_fd, 2 + (2 * root_statbuf.st_blksize) / RECORD_SIZE) == -1)
return -1;
- }
- if (fd != STDOUT_FILENO)
- close(fd);
return 0;
}
-int archive_read(char *archive_file_name)
+int archive_extract(int archive_fd)
{
- int archive_fd;
-
- if (archive_file_name == NULL)
- archive_fd = STDIN_FILENO;
- else
- {
- if ((archive_fd = open(archive_file_name, O_RDONLY)) == -1)
- {
- perror("achive_write");
- return -1;
- }
- }
-
char record[RECORD_SIZE];
t_header header;
int fd;
@@ -80,7 +49,6 @@ int archive_read(char *archive_file_name)
case '5':
if (mkdir(header.file_name, statbuf.st_mode) == -1)
{
- close(archive_fd);
perror(NULL);
return -1;
}
@@ -106,6 +74,41 @@ int archive_read(char *archive_file_name)
return -1;
}
}
- close(archive_fd);
+ return 0;
+}
+
+int archive_get_fd(t_args *args)
+{
+ int fd;
+ bool is_read;
+
+ is_read = args->action == ACTION_DIFF ||
+ args->action == ACTION_LIST ||
+ args->action == ACTION_EXTRACT;
+ if (args->archive_name == NULL)
+ return is_read ? STDIN_FILENO : STDOUT_FILENO;
+ if (is_read)
+ fd = open(args->archive_name, O_RDONLY);
+ else
+ fd = open(args->archive_name, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_UMASK);
+ if (fd == -1)
+ {
+ perror("archive fd");
+ return -1;
+ }
+ return fd;
+}
+
+int archive_dispatch_action(int archive_fd, t_args *args)
+{
+ switch (args->action)
+ {
+ case ACTION_CREATE:
+ return archive_create(archive_fd, args->files);
+ case ACTION_EXTRACT:
+ return archive_extract(archive_fd);
+ default:
+ return -1;
+ }
return 0;
}