1

I'm trying to create a basic version of the linux "tar" command using C, i used perror to see if there are any errors during execution, and i got this

./tar
Error2: Bad file descriptor

and this is what i did so far

#include <stdio.h>
#include <libtar.h>
#include <fcntl.h>

int main(void)
{
   TAR *pTar;
   char *prefix = ".";
   char *filename = "file.tar";

   if ((tar_open(&pTar, filename, NULL, O_WRONLY, 0644, TAR_GNU)) == -1)
     perror("Error1");
   else if ((tar_extract_all(pTar, prefix)) == -1)
     perror("Error2");
   else if ((tar_close(pTar)) == -1)
     perror("Error3");
}

Thanks in advance:)

James
  • 69
  • 3
  • 10
  • Show the complete error message and/or run it in debug mode to show us the line where the error happens. – Alex Mar 03 '17 at 20:43

2 Answers2

4

you're opening your tar file in O_WRONLY mode, so it truncates the existing file instead of opening it for reading.

When you try to extract from the file you get an error (probably when reading the header), that's expected because file contents are clobbered by the previous (successul) call.

Check working examples here:

To sum it up, my fix: replace

if ((tar_open(&pTar, filename, NULL, O_WRONLY, 0644, TAR_GNU)) == -1)

by

if ((tar_open(&pTar, filename, NULL, O_RDONLY, 0644, TAR_GNU)) == -1)

(I don't think all parameters are useful in read mode, like permissions or tar type, but that should work, it's difficult to find proper examples for that library)

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I tried to use the O_RDONLY mode, now instead of "Bad file descriptor" error it show "Invalid argument" error. – James Mar 03 '17 at 21:00
  • The file size hasn't changed, tried the normal tar to extract its content and everything is still safe – James Mar 03 '17 at 21:10
  • I made sure that the file is in the same directory as the executable and replaced the given paths with absolute paths, but still it shows the same error. – James Mar 03 '17 at 21:34
  • seems that you're not alone finally: http://stackoverflow.com/questions/17267372/extract-file-with-libtar. Looks like the same issue. – Jean-François Fabre Mar 03 '17 at 21:48
  • can you try `if ((tar_open(&pTar, filename, NULL, O_RDONLY, 0, TAR_VERBOSE)) == -1)` (no TAR_GNU, and add verbose to get debug output) – Jean-François Fabre Mar 03 '17 at 21:51
  • I got this "-rw-r--r-- root root 90 Mar 3 21:26 2017 ./PaxHeaders.15360/tar.c" – James Mar 03 '17 at 21:53
  • are you using root ? if not that would at least explain why the file hasn't been overwritten. I guess it's still not working then. oh: can you check if you can create a directory here? – Jean-François Fabre Mar 03 '17 at 22:02
  • you could try adding `TAR_IGNORE_EOT|TAR_IGNORE_MAGIC` to `TAR_VERBOSE`, the .tar file may not be compatible. And create another tar file yourself using `tar cvf`(this one may have issues that libtar cannot handle). As you see in the link I posted, another person had that issue, the answer is accepted, but problem not solved, so acceptance doesn't really count! – Jean-François Fabre Mar 03 '17 at 22:10
  • I'll try to do that and yes i've noticed, sorry for the bother and i really really appreciate it :) – James Mar 03 '17 at 22:12
  • i managed to create another directory yes, so i'm not really root but if i create the file with 0644 permessions, doesn't that change something ? – James Mar 03 '17 at 22:13
  • 644 is OK: file can be read. BTW can you delete it? have you tried my flags. And it doesn't bother me. Got to deserve the upvotes :) I suspect that this library hasn't been much used and is limited/not very robust. – Jean-François Fabre Mar 03 '17 at 22:16
  • well i began with removing the file, right after i did that my source file got corrupted – James Mar 03 '17 at 22:19
  • so i guess i should be looking for an alternative method instead of the libtar.h – James Mar 03 '17 at 22:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137181/discussion-between-jean-francois-fabre-and-james). – Jean-François Fabre Mar 03 '17 at 22:23
0

For any one referencing to this in the future,

while creating the tar - say abc.tar if you have appended the abc.tar (it is also in the same directory and append_tree appends all file, including the one), your abc.tar file, on tar_extract_all, this .tar file (with the same name) is also extracted overwriting the original tar file.

atleast this was what causing the mysterious "Invalid argument" for me. I fixed it by renaming the original file before extracting.