libtar: what is the right way to use libtar with buffers instead of files?
Hi!
I want to handle tgz files that are in memory. At the time I'm working with a tar file that I have read into memory. I create a FILE * with fmemopen, I can read the file content with fgetc successfully on the FILE * created by fmemopen. Now, when I try to use this file with libtar, I get a Bad file descriptor message on th_read() (though tar_fdopen was successful). Code:
FILE* memoryFile = fmemopen(fileContent, fileSize + 1, "rb"); |
Oh, I get it. The problem is that fileno returns -1 which means it's a bad file descriptor.... also from fmemopen's man page:
Code:
There is no file descriptor associated with the file stream returned by these functions (i.e., fileno(3) will return an error if called on the returned stream). |
Got it. Have to set a custom tartype_t when calling tar_fdopen that calls your own handlers for opening/reading/writing/closing.
|
I'm having a lot of fun over here.
I can correctly use my custom function to read the tar file except when I try to get the content of the files with tar_block_read. When I only use th_read/tar_skip_regfile it works like a charm. I can get the filename, the file size of all files, print them to stderr, beautyful!. Now, when I try to run tar_block_read, I get to read the content of the first file (this as a matter of fact is a proxy call to my custom reader) and then libtar goes nuts and can't seem to see the other files. From libtar.h: Code:
#define tar_block_read(t, buf) (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE) So, what I did is replace the call for tar_block_read for a call to t->type->readfunc (in other words, an indirect call to my custom reader) with the actual size of the file I want to extract. That works and I get the contents of the file, however, I think that given that I moved the position of the pointer in the tar file, now calls to th_read fail (it's like something else has to be called after tar_block_read so that libtar gets the pointer at the right location or something like that). Can anybody give me a hand over here? Thanks in advance. |
This is a utterly simple (and un-elegant) hack but it works.
Given that libtar is calling my own writer to get the file content, I created another writer (not related to libtar) that can read from memory _without_ moving the memory pointer for libtar... in other words, libtar leaves the pointer where I need it with th_read and then I read memory (behind libtar's back) to get file content and then I can call tar_skip_regfile() to get to the next file's position. How about that? But there has to be a clean way to do it, don't you think? What's that? |
All times are GMT -5. The time now is 10:17 PM. |