mardi 4 août 2015

Data compare of files is wrong. Why?

So, the thing is, this program checks if there are duplicate files in a directory. It uses mostly the windows.h library. To spare code, all the file paths are stored in the buffer finalizedList. The list is always correct, don't worry about it.

To show you the problem, here are the files in the directory:

  • duplicate_delete.exe = The program itself
  • hello.txt = The original text file
  • hello - Copy.txt = A duplicate of "hello.txt". They are exactly the same.
  • hello.cpp = A file that has the exact same name with the original text file, except its extension. (AKA "The Bad Guy")

Now, anytime I run the program while "hello.cpp" is in the directory, this is the output of the program:

Creating list...
Finalizing list...
Starting compare procedure...
Checking: duplicate_delete.exe vs. hello - Copy.txt --> DIFFERENT
Checking: duplicate_delete.exe vs. hello.cpp --> DIFFERENT
Checking: duplicate_delete.exe vs. hello.txt --> DIFFERENT
Checking: hello - Copy.txt vs. hello.cpp --> DIFFERENT
Checking: hello - Copy.txt vs. hello.txt --> DIFFERENT
Checking: hello.cpp vs. hello.txt --> DIFFERENT
Finished.
Press any key to continue . . .

But, when I delete that file the output is this:

Creating list...
Finalizing list...
Starting compare procedure...
Checking: duplicate_delete.exe vs. hello - Copy.txt --> DIFFERENT
Checking: duplicate_delete.exe vs. hello.txt --> DIFFERENT
Checking: hello - Copy.txt vs. hello.txt --> DUPLICATE
Finished.
Press any key to continue . . .

Now... Why does that happen? Or to put it in a better way, how can that happen?

Here are parts of the code:

This is the function that initiates the compare procedure between all the files:

void startCompare()
{
    WCHAR currentFile[1024], comparedFile[1024];
    DWORD i, j;
    i = 0;

    for (j = 0; j < fileListP; ++j)
    {
        lstrcpy(currentFile, finalizedList[j]);
        if (!openFile1(currentFile)) wprintf(L"File \"%ls\" couldn not be opened for comparing.\n", currentFile);
        else
        {
            for (i = j + 1; i < fileListP; ++i)
            {
                lstrcpy(comparedFile, finalizedList[i]);
                if (!openFile2(comparedFile)) wprintf(L"File \"%ls\" couldn not be compared with file \"%ls\".\n", currentFile, comparedFile);
                else
                {
                    if (!compare())
                    {
                        wprintf(L"Checking: %ls vs. %ls --> DUPLICATE\n", currentFile, comparedFile);
                        lstrcpy(deleteList[deleteListP], currentFile);
                        ++deleteListP;
                    }
                    else
                    {
                        wprintf(L"Checking: %ls vs. %ls --> DIFFERENT\n", currentFile, comparedFile);
                    }
                    CloseHandle(file2);
                }
            }
            CloseHandle(file1);
        }
    }
}

The handles are internally set by the functions "openFile1" and "openFile2". If the two last functions return 0, it means that the handles weren't set successfully.

This function actually compares the files "file1" and "file2".

DWORD compare()
{
    DWORD a, b;
    BYTE byte1, byte2;

    while (1)
    {
        ReadFile(file1, &byte1, 1, &a, NULL);
        ReadFile(file2, &byte2, 1, &b, NULL);
        //a = fread(&byte1, 1, 1, file1);
        //b = fread(&byte2, 1, 1, file2);
        if (a != b) return 1;
        if (byte2 != byte1) return 1;
        if (a == 0 && b == 0) break;
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire