tags:

views:

483

answers:

4

Let me explain what I'm trying to realize:

I have a encrypted tar file. I can decrypt it in memory, but obviously I can't write the decrypted data back to hard disk as a real file. The decrypted data is structured as a char* buffer in memory; how can I untar it in memory?

I can't find answer with libtar library. I also tried to untar it with execlp("tar", "tar", "-xvO", (void*)0). But it didn't work as I thought.

Anyone can give me a hint of the best solution? Thanks!

+4  A: 

I suspect that libtar is the answer.

Using libtar, you can specify your own functions for opening/closing, reading and writing. From the manpage:

int tar_open(TAR **t, char *pathname, tartype_t *type, int oflags,
             int mode, int options);

The tar_open() function opens a tar archive file corresponding to the filename named by the pathname argument. The oflags argument must be either O_RDONLY or O_WRONLY.

The type argument specifies the access methods for the given file type. The tartype_t structure has members named openfunc(), closefunc(), readfunc() and writefunc(), which are pointers to the functions for opening, closing, reading, and writing the file, respectively. If type is NULL, the file type defaults to a normal file, and the standard open(), close(), read(), and write() functions are used.

gnud
as tar_open function read char* pathname:int tar_open(TAR **t, char *pathname, tartype_t *type, int oflags, int mode, int options);the pathname must be some real file, right?
solotim
See my edit - you can use your own file-access functions, which could just work on memory.
gnud
the openfunc must return a fildes, I can't figure out how to get a fildes from a buffer. I have tried to use fileno(fmemopen(...)), but it just return -1. I'm frustrated.
solotim
A: 

Just untar to in-memory tmpfs using a normal untar operation.

EffoStaff Effo
I think it's unsafe since tmpfs can be accessed by any other process. right?
solotim
Depends on user permissions. Hence my suggestion to use a 'special' user for writing out the temporary file.
gnud
If root wants to read your data, you're screwed either way.
gnud
Root can always dump the in memory contents of your process
Martin Beckett
+2  A: 

You can execute tar utility redirected to stdout. (tar --to-stdout). You should run it using forkpty() or popen() in order to read the output.

eyalm
I'v tried to use "tar -xvO", it read from stdin and write to stdout. In my program, I must use two pipes, one: tardata->stdin, two: untarreddata<-stdout. The difficult point is that, when I put tardata to stdin pipe, since there are some '\0' in the raw tardata, the tar command can't recognize a complete tar from stdin. Do you have any idea about this? thanks
solotim
+2  A: 
sambowry
This is great too. So I can access the data in tar directly by pointer. But, how if the file is tar.gz?
solotim
zlib lets you open a gzip'ed file transparently. It even works the same on files that aren't gzip'ed
Martin Beckett