Programming Practice OO "synchronize folders" - to achieve (2)

Yesterday achieve the two functions

1. source folder has changed, it returns the results need to be updated.

2. dest the file does not exist, the same needs to be updated.

Today will consider the problem folder and file backup for the title.

 

[Folder Detection]

Detection folder also has two kinds of cases, one folder in dest there, then the need for testing of its subfolders. Second, dest folder does not exist, this process is relatively simple, direct return needs to be updated, the entire folder to copy over all.

To not consider the contents of subfolders can be considered as long as the dest folder exists, then there is no need to update, so the test:

    Folder source("sourceFolder");
    Folder dest("destFolder");
    SynchronousSystem SS(&source,&dest);

    CPPUNIT_ASSERT(SS.isNeedUpdate("FolderNotExist"));
    CPPUNIT_ASSERT(SS.isNeedUpdate("FolderExist") == false);

The test is not passed, the code rewrite Folder.

    bool isExist(const std::string& filename)
    {
        if(filename == "FileNotExist" || filename == "FolderNotExist")
            return false;
        else
            return true;
    }

Amount, and so on, a little problem. Treatment in the form of a folder or file to process the files, folders and file processing logic is not the same.

If I changed Folder in getModifyTime, the test results will not pass.

I will time the folder dest moved forward, the result is not right.

    if (filename == "FolderExist" && itsPath == "destFolder")
            return FileModifyTime(std::time(NULL)-1000);

In modified SynchronousSystem isNeedUpdate code as follows:

    bool isNeedUpdate(const std::string& filename)
    {
        if(!itsDestFolder->isExist(filename))
            return true;
        if(!itsSourceFolder->isDirectory(filename) && itsSourceFolder->getFileModifyTime(filename).isNewerThan(itsDestFolder->getFileModifyTime(filename)) )
            return true;
        return false;
    }

Add isDirectory code in Folder

    bool isDirectory(const std::string& filename)
    {
        return (filename == "FolderNotExist" || filename == "FolderExist");
    }

Test passed.

 

[Old file backup]

Amount, almost forgot to be backed up before the copy. The task to be accomplished dest's Folder on the line.

Folder Backup function added

    //operator
    void backUp(const std::string& filename)
    {
        std::cout << "Backup "<< filename << std::endl;
    }

But the backup function where call it? copy need to call it before? Need not need to, there are two cases, one is turned over to the backup function, it needs to determine whether the backup (file or folder does not exist is not required to back up).

The second is whether the judge in charge of the SynchronousSystem to be backed up.

These two actually are, first with the first to achieve it:

    //operator
    void backUp(const std::string& filename)
    {
        if(isExist(filename))
           std::cout << "Backup \""<< filename << "\"" << std::endl;
    }

 

Thus backUp function to get below to the copying operation.

I hope SynchronousSystem of Synchronous function code like this:

    void Synchronous(const std::string& filename)
    {
        if (isNeedUpdate(filename))
        {
            itsDestFolder->backUp(filename);
            itsSourceFolder->copyFileTo(filename,itsDestFolder);
        }
    }

If the second way, in the judgment in Synchronous, the code should be this:

    void Synchronous(const std::string& filename)
    {
        if (isNeedUpdate(filename))
        {
            if(itsDestFolder->isExist(filename))
                itsDestFolder->backUp(filename);
            itsSourceFolder->copyFileTo(filename,itsDestFolder);
        }
    }

This difference between the two do not play, I'm not very good decide where to put it.

Folder in class

    void copyFileTo(const std::string& filename, Folder* destFolder)
    {
        std::cout << "Copy File \"" << filename << "\"" << std::endl; 
    }

Written test to see if the output is not correct

    Folder source("sourceFolder");
    Folder dest("destFolder");
    SynchronousSystem SS(&source,&dest);
    std::cout<<std::endl;
    SS.Synchronous("FileSourceIsNew");
    SS.Synchronous("FileDestIsNew");
    SS.Synchronous("FileNotExist");
    SS.Synchronous("FolderNotExist");
    SS.Synchronous("FolderExist");

Output:

Backup "FileSourceIsNew"
Copy File "FileSourceIsNew"
Copy File "FileNotExist"
Copy File "FolderNotExist"

Ah, the output is right. You need to back up only FileSourceIsNew.

Reproduced in: https: //www.cnblogs.com/zhangyonghugo/archive/2012/06/29/2569631.html

Guess you like

Origin blog.csdn.net/weixin_33895604/article/details/93946725