LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2019, 02:48 AM   #1
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Rep: Reputation: Disabled
How to read tar file from buffer in C


Hi,

I created tar file inside buffer by using libarchive API.
archive_write_open_memory(file_handle->a,file_handle->buf,1024,&file_handle->used);
File will be created inside file_handle->buf
Now i want to read data from this buffer in place of file.
For example for read data from tar file we can call read system call by passing fd.
How we can achieve same reading through buffer?
 
Old 01-15-2019, 06:36 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
A continued welcome to LQ,

Of course you can read data from memory, or a structure, or a file using the C language.

What's missing here is any code excepting that one line statement you've shown.

Have you reviewed the documentation for libarchive, https://libarchive.org/?

You should follow the suggestion you received in your other thread, Creating tar in buffer through libarchive and streaming it, and review information about how to use the Linux Questions site as well as how to ask effective questions.

Glad you feel you've succeeded with creating the buffer. Now you're saying you wish to read the buffer. Either this buffer is in memory, or it is a file. Do you know how to reference and read memory using the C language? Do you know how to open and read a file using the C language? Either case, these are the items you'd need to do to read information. It's rather hard to give you an exact statement how to do something when it is unclear what you've tried, and what your programming experience is.
 
1 members found this post helpful.
Old 01-15-2019, 07:23 AM   #3
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Hi,
Its not a simple buffer reading, the buffer will be containing tar data so how to do tar reading from buffer.
Buffer i am refering to inmemory not file
Simple buffer reading will not work since it is a tar stream.
Normal buffer reading will not work since it conatin tar stream.
But if we do read system call from file it works as expected but read need a fd and use for file reading.
 
Old 01-15-2019, 07:30 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
Did you try to read the documentation of libarchive (as it was mentioned)?
https://github.com/libarchive/libarchive/wiki/Examples
 
Old 01-15-2019, 07:57 AM   #5
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Tried everything, there is no example in libarchieve of reading from buffer.I already read all example.
 
Old 01-15-2019, 08:04 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
aside from what the api doing it, buffer is just a memory slot with data in it, like opening a file and putting it contents into a buffer then acting on it. I'd think this to be similar.


List contents of Archive stored in File


List contents of Archive stored in Memory, same page
 
Old 01-15-2019, 08:08 AM   #7
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Hi ,
I have limitation of hardisk i cannot create tar in file.I need to do keep tar in buffer and stream it.
 
Old 01-15-2019, 08:30 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by RohitB View Post
Hi ,
I have limitation of hardisk i cannot create tar in file.I need to do keep tar in buffer and stream it.
List contents of Archive stored in Memory, same page
Code:
struct archive *a = archive_read_new();
archive_read_support_compression_gzip(a);
archive_read_support_format_tar(a);
r = archive_read_open_memory(a, buff, sizeof(buff));

Last edited by BW-userx; 01-15-2019 at 08:31 AM.
 
1 members found this post helpful.
Old 01-17-2019, 08:49 AM   #9
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Hi,

archive_read_open_memory this will again create archieve after reading from buffer.I dont want this i want data to be copied to normal char * buffer that can flush.If you will archieve buffer as normal buffer it will give incalid result
 
Old 01-17-2019, 10:37 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
@OP: Do you happen to have a Minimal Complete, Verifiable Example (mcve) ?
 
Old 01-18-2019, 03:10 AM   #11
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Its simple reading taken from example:-
Code:
char buff[1024];//temp buffer for reading from file.
char archive_buf[2048]//archive will be store in this buffer.
int used ;
a = archive_write_new();
archive_write_add_filter_gzip(a);
archive_write_set_format_pax_restricted(a); // Note 1
archive_write_open_memory(a,archive_buf,1024,&used);
stat(*filename, &st);
entry = archive_entry_new(); 
archive_entry_set_pathname(entry, "sample.xml");
archive_entry_set_size(entry, st.st_size); 
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_perm(entry, 0644);
archive_write_header(a, entry);
fd = open("sample.xml", O_RDONLY);//opening xml file and reading its content so that tar can be prepared.

len = read(fd, buff, sizeof(buff));
while ( len > 0 ) {
 len = read(fd, buff, sizeof(buff));
 archive_write_data(a, buff, len);
}
Creation of archive in buffer is done.
Now how to do reading from this buffer and send it as MIME to Server?

If I have tar as file below method is working as expected.
bytes_read = fread(buf_containing_read_bytes_from_tar_file, sizeof(char), 1024, (FILE*)file pointer to tar file);
Same thing how i can do when data is in buffer??//Note I cannot use buf which i used for creating archive.It is just a place holder.I need to read data from this buffer and write data into streaming buffer.

Last edited by astrogeek; 01-18-2019 at 02:13 PM. Reason: Added CODE tags
 
Old 01-18-2019, 02:15 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

I have fixed that in your previous post as an example.
 
Old 01-18-2019, 09:19 PM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
This isn't a complete program. Please read this: https://stackoverflow.com/help/mcve
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i decompress this tar.tar file? hmmm sounds new.. tar.tar.. help ;) kublador Linux - Software 14 10-25-2016 02:48 AM
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM
How do I un tar a .tar, .tar.z, .tar.gz file vofkid Linux - Newbie 4 03-15-2002 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration