0

私はCプログラミングに非常に慣れていないため、Cでディレクトリの内容を再帰的にコピーする方法に関する役立つ情報はほとんどありません(C ++、C#、Objective-C、Java、シェル、Python、またはその他のものではありません-これC で実行する必要があります)。はい、これは宿題で、明日の夜に期限が切れます。これを終わらせるのに 2 週間近くかかっています。

特定のディレクトリのバックアップ バージョンを検索しようとしています (たとえば、バックアップ ディレクトリ "/dir.bak" を持つディレクトリ "dir")。存在しない場合は、その「/dir.bak」ディレクトリを作成するか、存在する場合は、代わりに「/dir.bak.MM-DD-YY-HH-MM-SS」という名前のバックアップ ディレクトリを作成します。次に、ソース ディレクトリから宛先ディレクトリにすべてをコピーします。

このコードを実行しようとすると:

#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;  
}

このエラーが発生します:

[my-desktop]@ubuntu:~/Desktop$ ./helpmehelpyou.out ~/Desktop new
Error in creating the output file:: Permission denied

このコードの束は、インストラクターが使用できると言っているサンプル ファイルから取得したものです。なぜこれが正しく機能しないのかわかりませんか?完成した関数は再帰的でなければならず、これがオプション 4 である 4 つの選択肢のプログラムの一部です。C は、私が把握または理解するのが非常に難しく、前述のように、C に関する情報はほとんどありませんが、 C#、C++、Objective C、およびその他すべての言語で過剰に使用されています。また、私が言ったように、この割り当てにシェル コマンドやその他のものを使用してはなりません。

誰か助けてくれませんか?ありがとう!

4

1 に答える 1

2

これでは問題は解決しませんが、コードにエラーの可能性があることを発見しました。再帰が発生しているのがわかりません。

ご覧のとおり、サブフォルダーが1つしかない場合、このコードは正常に機能する可能性があります。しかし、そのサブフォルダーにサブフォルダーがある場合はどうしますか?

再帰的なディレクトリ検索機能が必要です。これは次のようになります。

void SearchDirectory(const char *name) {
    DIR *dir = opendir(name);                //Assuming absolute pathname here.
    if(dir) {
        char Path[256], *EndPtr = Path;
        struct dirent *e;
        strcpy(Path, name);                  //Copies the current path to the 'Path' variable.
        EndPtr += strlen(name);              //Moves the EndPtr to the ending position.
        while((e = readdir(dir)) != NULL) {  //Iterates through the entire directory.
            struct stat info;                //Helps us know about stuff
            strcpy(EndPtr, e->d_name);       //Copies the current filename to the end of the path, overwriting it with each loop.
            if(!stat(Path, &info)) {         //stat returns zero on success.
                if(S_ISDIR(info.st_mode)) {  //Are we dealing with a directory?
                    //Make corresponding directory in the target folder here.
                    SearchDirectory(Path);   //Calls this function AGAIN, this time with the sub-name.
                } else if(S_ISREG(info.st_mode) { //Or did we find a regular file?
                    //Run Copy routine
                }
            }
        }
    }
}

このコードはSearchDirectory、現在のフォルダーにサブディレクトリがある回数だけ関数を呼び出します。アーキテクチャが深すぎると、メモリが不足し、プログラムがクラッシュする可能性があるため、注意してください。

フォルダ内にそれ以上ディレクトリが見つからなくなると(「leaf」フォルダに到達すると)、残りのファイルを繰り返し処理してから関数を終了し、スタックから最後の関数呼び出しをポップしてSearchDirectory、検索を続けます。

結局のところ、完全な再帰コピーができあがります。

PS:ネタバレでごめんなさい、私はタイピングを終えた後にだけあなたのコメントを読みました。

于 2013-03-02T17:53:09.523 に答える