Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

tar - how to untar file in memory (c programming)?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...