Kopiowanie katalogu rekurencyjnego w C
Jestem bardzo nowy w programowaniu w C i jest niewiele pomocnych informacji gdziekolwiek można zrobić rekurencyjne kopiowanie zawartości katalogu w C (nie C ++, C #, Objective-C, Java, shell, Python lub cokolwiek innego - to MUSI być wykonane w C). Tak, to jest praca domowa i jest już jutro, a ja utknąłem przez prawie dwa tygodnie, próbując to zrobić.
Próbuję wyszukać dowolne wersje kopii zapasowej danego katalogu (np. Katalog „dir” z katalogiem kopii zapasowej „/dir.bak”). Jeśli nie, utwórz katalog „/dir.bak” lub, jeśli istnieje, utwórz zamiast tego katalog kopii zapasowej o nazwie „/dir.bak.MM-DD-YY-HH-MM-SS”. Następnie skopiuj wszystko z katalogu źródłowego do katalogu docelowego.
Kiedy próbuję uruchomić ten kod:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#define BUFSIZE 256
int main (int argc, char* argv[])
{
DIR *dir;
struct dirent *dentry;
struct stat statbuff;
char buffer[BUFSIZE];//holds time and date suffix
ssize_t count;
int fdin, fdout;
mode_t perms = 740;//perms for file I/O ops
char *cwdNamePntr;
long maxpath;
if ( argc != 3 ) //if incorrect number of args passed in...
{
printf( "Usage: %s Directory-Path-Name Destination-Folder\n ", argv[0] );
exit(1);
}
char buffer2[BUFSIZE];//buffer for file I/O ops
char* datetime;
int retval;
time_t clocktime;
struct tm *timeinfo;
time (&clocktime);
timeinfo = localtime( &clocktime );
strftime(buffer, BUFSIZE, "%b-%d-%Y-%H-%M-%S", timeinfo);//store time and date suffix in case we need it
chdir(argv[1]);
char *path = argv[2];//source path for I/O ops
char *newpath = malloc(strlen(path) + 26);//destination paths for I/O ops
strcpy(newpath,path);
strcat(newpath,".bak");//test name for backup directory
if(chdir(newpath)==0)
{//if our test name is already a directory
chdir("..");
strcat(newpath,".");
strcat(newpath,buffer);//add time and date suffix onto the name of new backup directory
}
if ( (mkdir(newpath, perms)) == 0 )//if we successfully made a new backup directory
{
chdir(path);
dir = opendir(".");//move into source directory
if ( dir == 0 )
{//if open directory fails
fprintf (stderr, "Error in opening directory: %s\n", path );
perror( "Could not open directory");
exit(2);
}
dentry = readdir (dir);//load directory
while (dentry != 0)//while we are reading a directory
{
char *filename = dentry->d_name;//get name of file to be copied
if( (strcmp(filename,".")!=0) && (strcmp(filename,"..")!=0) )
{
if ( (fdin = open ( filename, O_RDONLY)) == -1)
{//if cannot open input file
perror ( "Error in opening the input file:");
exit (2);
}
chdir("..");//go back to parent directory
chdir(newpath);//move to destination directory
opendir(".");
if ( (fdout = open (filename, (O_WRONLY | O_CREAT), perms)) == -1 )
{//if cannot create output file...
perror ( "Error in creating the output file:");
exit (3);
}
while ( (count=read(fdin, buffer2, BUFSIZE)) > 0 )
{
if ( write (fdout, buffer2, count) != count )
{//if cannot write
perror ("Error in writing" );
exit(3);
}
}
if ( count == -1 )
{//if cannot read
perror ( "Error while reading the input file: ");
exit(4);
}
close(fdin);
close(fdout);
}
chdir("..");//back to parent directory again
dir = opendir(".");
if ( dir == 0 )//if open directory fails
{
fprintf (stderr, "Error in opening directory: %s\n", path );
perror( "Could not open directory");
exit(666);
}
dentry = readdir (dir);//reload directory
}
}
else
{
perror("Error in directory creation");
exit(2);
}
return 0;
}
Dostaję ten błąd:
[my-desktop]@ubuntu:~/Desktop$ ./helpmehelpyou.out ~/Desktop new
Error in creating the output file:: Permission denied
Kilka tego kodu pochodzi z przykładowych plików, które według naszych instruktorów możemy wykorzystać. Nie rozumiem, dlaczego to nie działa prawidłowo? Skończona funkcja musi być rekurencyjna i jest częścią programu czterokrotnego wyboru, przy czym jest to opcja 4. C jest dla mnie bardzo trudne do zrozumienia lub zrozumienia, i tak jak powiedziałem, jest bardzo mało informacji na ten temat dla C, ale nadmiar tego dla C #, C ++, Objective C i każdego innego języka. I tak jak powiedziałem, NIE MOŻEMY używać poleceń powłoki ani niczego innego do tego zadania.
Czy ktoś może mi pomóc? Dzięki!