List the contents of a tar archive WITHOUT using TAR commands
frozzen
-
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
Hello,
for a C project, I need to program a tar file extractor. To start, I just want to list the files in my archive, and here an odd phenomenon occurs: if the archive contains only a single folder at the root, my program displays the name of the folder, but if I extract the archive and then recompress it by adding a folder to the source, my program displays the first folder of that archive, then the first subfolder of that folder, and so on.
Here is my code:
int main(int argc, char* argv[]) {
/*OPENING THE TAR ARCHIVE IN READ-ONLY MODE*/
int fd;
fd = open(argv[1],O_RDONLY,0);
if (fd < 3) {
printf("Unable to open the file\n");
exit(1);
} else {
printf("The file %s has been successfully opened\n", argv[1]);
struct header_posix_ustar ma_struct;
int nb_bits_lus=0;
/*READING THE HEADER OF THE FIRST FILE (512-byte block) */
nb_bits_lus=read(fd, &ma_struct, 512);
printf("name of the first file: %s\n", ma_struct.name);
printf("Number of bits read: %d\n", nb_bits_lus);
}
close(fd);
return 0;
}
It should be noted that if I try to go back by removing the folder I added, the anomaly persists even when I return to the base case with one folder at the root.
Best regards,
Configuration: Linux / Chrome 53.0.2785.143
for a C project, I need to program a tar file extractor. To start, I just want to list the files in my archive, and here an odd phenomenon occurs: if the archive contains only a single folder at the root, my program displays the name of the folder, but if I extract the archive and then recompress it by adding a folder to the source, my program displays the first folder of that archive, then the first subfolder of that folder, and so on.
Here is my code:
int main(int argc, char* argv[]) {
/*OPENING THE TAR ARCHIVE IN READ-ONLY MODE*/
int fd;
fd = open(argv[1],O_RDONLY,0);
if (fd < 3) {
printf("Unable to open the file\n");
exit(1);
} else {
printf("The file %s has been successfully opened\n", argv[1]);
struct header_posix_ustar ma_struct;
int nb_bits_lus=0;
/*READING THE HEADER OF THE FIRST FILE (512-byte block) */
nb_bits_lus=read(fd, &ma_struct, 512);
printf("name of the first file: %s\n", ma_struct.name);
printf("Number of bits read: %d\n", nb_bits_lus);
}
close(fd);
return 0;
}
It should be noted that if I try to go back by removing the folder I added, the anomaly persists even when I return to the base case with one folder at the root.
Best regards,
Configuration: Linux / Chrome 53.0.2785.143
1 answer
-
Hi,
you are not providing enough information to understand what exactly your problem is or to reproduce it.
That said, if your code expects a ustar format and you are on Linux with GNU tar, you should create your tar file with the--format=ustar
option; otherwise, your header will not be in GNU tar format.
tar --help
at the end tells you how the version of tar you are using was compiled.
Also seeman tar
Dal