LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-01-2014, 04:41 PM   #1
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Rep: Reputation: Disabled
Linux C Code to copy directories and files from mass storage device to folder in FS


Hello,

I have an embedded linux kit where I can plug in a USB Flash Drive.

The USB Flash Drive has multiple folders, sub-folders and files.

I would like to create some C code to copy these folders and files from the mass storage device to the file system on my kit.

I would want to do this somewhat in multiple copies so I could check to see if there was still room in the file system I am copying to.

Any ideas as to how to go about this would be greatly appreciated.
 
Old 09-02-2014, 06:39 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You can use the rename(2) function, or you can fork() and exec() and perform a shell mv call.

I've done both, but prefer using the mv method.
 
Old 09-03-2014, 06:24 PM   #3
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
I found the following and was trying to use it. Unfortunately it does not keep keep the directory structure on the USB drive intact when copying over.

For example if I tried the following: Copy(/media/sda1/foo/foo.txt, "/home/Usb_Files");

I see foo.txt at /home/Usb_Files/foo.txt instead of /home/Usb_Files/foo/foo.txt

Any idea on how to do this?

I am really stuck.


int Copy(char *source, char *dest)
{
int childExitStatus;
pid_t pid;
int status;
if (!source || !dest) {
/* handle as you wish */
}

pid = fork();

if (pid == 0) { /* child */
execl("/bin/cp", "/bin/cp", "-R", source, dest, (char *)0);
}
else if (pid < 0) {
/* error - couldn't start process - you decide how to handle */
}
else {
/* parent - wait for child - this has all error handling, you
* could just call wait() as long as you are only expecting to
* have one child process at a time.
*/
pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
if (ws == -1)
{ /* error - handle as you wish */
}

if( WIFEXITED(childExitStatus)) /* exit code in childExitStatus */
{
status = WEXITSTATUS(childExitStatus); /* zero is normal exit */
/* handle non-zero as you wish */
}
else if (WIFSIGNALED(childExitStatus)) /* killed */
{
}
else if (WIFSTOPPED(childExitStatus)) /* stopped */
{
}
}
}
 
Old 09-03-2014, 06:25 PM   #4
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Also if I try Copy(/media/sda1/foo/foo.txt, "/home/Usb_Files/foo");

It says the cp cannot stat no such file or directory

Any help would be greatly appreciated.
 
Old 09-03-2014, 06:35 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You have to create the foo destination directory first, then copy foo.txt into it

Code:
/bin/mkdir /home/Usb_Files/foo
/bin/cp /media/sda1/foo/foo.txt /home/Usb_Files/foo
In a more refined program, you could check if destination directory exists before atempting to copy a file into it

[edit]

or use -p option for mkdir

man mkdir
Code:
       -p, --parents
              no error if existing, make parent directories as needed

Last edited by keefaz; 09-03-2014 at 06:40 PM.
 
  


Reply

Tags
code, copying, embedded, linux, usb



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
[SOLVED] How to copy a folder with sub directories using scp sanjay87 Linux - Server 5 05-24-2012 06:40 AM
[SOLVED] Retrofitting a Device File for a Mass Storage Device? devdol Linux - Hardware 4 07-10-2010 02:22 PM
how to get the device ID of a USB mass storage device ebinjose Linux - Embedded & Single-board computer 1 10-30-2008 05:58 AM
SCSI error: return code = 0x10000 on USB Mass Storage device gringer Linux - Hardware 4 05-14-2007 06:43 AM
Accessing USB Mass storage device in Linux..?? kvsmani Linux - Hardware 0 02-11-2004 12:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:51 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