LinuxQuestions.org
Review your favorite Linux distribution.
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, 05: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, 07:39 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,147
Blog Entries: 13

Rep: Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563Reputation: 3563
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, 07: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, 07: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, 07:35 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
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 07:40 PM.
 
  


Reply

Tags
code, copying, embedded, linux, usb


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

All times are GMT -5. The time now is 08:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration