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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-01-2014, 04:41 PM
|
#1
|
LQ Newbie
Registered: Aug 2014
Posts: 29
Rep: 
|
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.
|
|
|
09-02-2014, 06:39 AM
|
#2
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,940
|
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.
|
|
|
09-03-2014, 06:24 PM
|
#3
|
LQ Newbie
Registered: Aug 2014
Posts: 29
Original Poster
Rep: 
|
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 */
{
}
}
}
|
|
|
09-03-2014, 06:25 PM
|
#4
|
LQ Newbie
Registered: Aug 2014
Posts: 29
Original Poster
Rep: 
|
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.
|
|
|
09-03-2014, 06:35 PM
|
#5
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
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.
|
|
|
All times are GMT -5. The time now is 11:11 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|