Compare commits
3 Commits
5be3c7d684
...
e758012948
Author | SHA1 | Date | |
---|---|---|---|
e758012948 | |||
27dac123f4 | |||
912b98d2d7 |
@ -32,7 +32,7 @@ int main(int argc, char** argv)
|
|||||||
if(minitar_read_entry(&mp, &entry) == 0) {
|
if(minitar_read_entry(&mp, &entry) == 0) {
|
||||||
printf("%s\n", entry.metadata.path);
|
printf("%s\n", entry.metadata.path);
|
||||||
} else break;
|
} else break;
|
||||||
} while(true);
|
} while(1);
|
||||||
minitar_close(&mp);
|
minitar_close(&mp);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
24
examples/list.c
Normal file
24
examples/list.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <minitar.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc == 1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [file]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
struct minitar mp;
|
||||||
|
if (minitar_open(argv[1], &mp) != 0)
|
||||||
|
{
|
||||||
|
perror(argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
struct minitar_entry entry;
|
||||||
|
do {
|
||||||
|
if (minitar_read_entry(&mp, &entry) == 0) { printf("%s\n", entry.metadata.path); }
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while (1);
|
||||||
|
minitar_close(&mp);
|
||||||
|
}
|
87
examples/untar.c
Normal file
87
examples/untar.c
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
/* Simple untar example for POSIX systems using minitar. */
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <minitar.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static int untar_file(const struct minitar_entry* entry, const void* buf)
|
||||||
|
{
|
||||||
|
int fd = open(entry->metadata.path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644);
|
||||||
|
if (fd < 0) return 1;
|
||||||
|
|
||||||
|
if (write(fd, buf, entry->metadata.size) < 0) return 1;
|
||||||
|
|
||||||
|
if (fchmod(fd, entry->metadata.mode) < 0) return 1;
|
||||||
|
|
||||||
|
return close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int untar_directory(const struct minitar_entry* entry)
|
||||||
|
{
|
||||||
|
if (mkdir(entry->metadata.path, entry->metadata.mode) < 0) return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc == 1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [file]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
struct minitar mp;
|
||||||
|
if (minitar_open(argv[1], &mp) != 0)
|
||||||
|
{
|
||||||
|
perror(argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
struct minitar_entry entry;
|
||||||
|
do {
|
||||||
|
if (minitar_read_entry(&mp, &entry) == 0)
|
||||||
|
{
|
||||||
|
if (entry.metadata.type == MTAR_DIRECTORY)
|
||||||
|
{
|
||||||
|
int status = untar_directory(&entry);
|
||||||
|
if (status != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to create directory %s: %s\n", entry.metadata.path, strerror(errno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("mkdir %s\n", entry.metadata.path);
|
||||||
|
}
|
||||||
|
else if (entry.metadata.type == MTAR_REGULAR)
|
||||||
|
{
|
||||||
|
char* ptr = (char*)malloc(entry.metadata.size);
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
perror("malloc");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
minitar_read_contents(&mp, &entry, ptr, entry.metadata.size);
|
||||||
|
|
||||||
|
int status = untar_file(&entry, ptr);
|
||||||
|
|
||||||
|
free(ptr);
|
||||||
|
|
||||||
|
if (status != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to extract file %s: %s\n", entry.metadata.path, strerror(errno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("untar %s\n", entry.metadata.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while (1);
|
||||||
|
minitar_close(&mp);
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
#ifndef MINITAR_H
|
#ifndef MINITAR_H
|
||||||
#define MINITAR_H
|
#define MINITAR_H
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ extern "C"
|
|||||||
int minitar_find_by_name(struct minitar* mp, const char* name, struct minitar_entry* out);
|
int minitar_find_by_name(struct minitar* mp, const char* name, struct minitar_entry* out);
|
||||||
int minitar_find_by_path(struct minitar* mp, const char* path, struct minitar_entry* out);
|
int minitar_find_by_path(struct minitar* mp, const char* path, struct minitar_entry* out);
|
||||||
int minitar_find_any_of(struct minitar* mp, enum minitar_file_type type, struct minitar_entry* out);
|
int minitar_find_any_of(struct minitar* mp, enum minitar_file_type type, struct minitar_entry* out);
|
||||||
size_t minitar_read_contents(struct minitar* mp, struct minitar_entry* entry, char* buf, size_t max);
|
size_t minitar_read_contents(struct minitar* mp, const struct minitar_entry* entry, char* buf, size_t max);
|
||||||
int minitar_close(struct minitar* mp);
|
int minitar_close(struct minitar* mp);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -111,7 +111,7 @@ int minitar_find_any_of(struct minitar* mp, enum minitar_file_type type, struct
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t minitar_read_contents(struct minitar* mp, struct minitar_entry* entry, char* buf, size_t max)
|
size_t minitar_read_contents(struct minitar* mp, const struct minitar_entry* entry, char* buf, size_t max)
|
||||||
{
|
{
|
||||||
if (!max) return 0;
|
if (!max) return 0;
|
||||||
if (!entry->metadata.size) return 0;
|
if (!entry->metadata.size) return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user