Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileMonitoringManager does not detect modified files when they're overwritten by new added files #81

Open
fxdeniz opened this issue Jul 16, 2022 · 2 comments
Assignees
Labels
bug Something isn't working file monitor subsystem

Comments

@fxdeniz
Copy link
Owner

fxdeniz commented Jul 16, 2022

Assume a case where we have a file called file.xlsx and it's updated by user. In this case, excel will create a temporary file such as 0A9C1FDA.tmp and this file be the new file.xlsx. (.tmp file be written over .xlsx file by the excel)

This was a known and handled case. But, I think I missed something in the logic. The case I described sometimes detected correctly sometimes not detected.

This bug happens in FileMonitoringManager::slotOnMoveEventDetected() here is the logic:

QFileInfo info(_dir + fileName);
if (info.isFile())
{
    bool isOldFileExist = this->mddb.isFileExistInDir(oldFileName, dir);
    bool isNewFileExist = this->mddb.isFileExistInDir(fileName, dir);

    if(isOldFileExist && !isNewFileExist)
    {
            auto currentState = this->mddb.stateOfFileInDir(fileName, dir);

          // Do not count renames (moves) if files is new added.
          if(currentState != MonitoredDirDb::MonitoredItemState::NewAdded)     <------- Check 1
          {
              // Here categorize files as Moved or MovedAndModified        
          }

          this->mddb.updateEventTimestampOfFileInDir(fileName, dir, QDateTime::currentDateTime());
    }
    else if(isOldFileExist && isNewFileExist) // Ghost file cleanup
    {
        this->mddb.removeFileFromDir(oldFileName, _dir);     <-------- Normally, this part should be working
    }
}

And, I solved this bug by adding an else case to Check 1. Here is the new logic:

QFileInfo info(_dir + fileName);
if (info.isFile())
{
    bool isOldFileExist = this->mddb.isFileExistInDir(oldFileName, dir);
    bool isNewFileExist = this->mddb.isFileExistInDir(fileName, dir);

    if(isOldFileExist && !isNewFileExist)
    {
            auto currentState = this->mddb.stateOfFileInDir(fileName, dir);

          // Do not count renames (moves) if files is new added.
          if(currentState != MonitoredDirDb::MonitoredItemState::NewAdded)     <------- Check 1
          {
              // Here categorize files as Moved or MovedAndModified
          }
          else      <--------- Solution is this else case
          {
              // If newly added file overwrites existing file then, consider that file modified
              this->mddb.scheduleFileInDirAs(fileName, dir, MonitoredDirDb::MonitoredItemState::Modified);
          }

          this->mddb.updateEventTimestampOfFileInDir(fileName, dir, QDateTime::currentDateTime());
    }
    else if(isOldFileExist && isNewFileExist) // Ghost file cleanup
    {
        this->mddb.removeFileFromDir(oldFileName, _dir);     <-------- Normally, this part should be working
    }
}

As I said, I considered this edge case. Previous code had a handler. But it looks like, it is not enough. Adding else case solved it completely. But, I need more time to understand why else case solved the issue.

Without else case existing file file.xlsx signaled as new added file by FileMonitoringManager even though it wass added to mddb previously. I'm sure that, it happens when new created file overwrites an existing file. Because, only place where code is marked as NewAdded is FileMonitoringManager::slotOnAddEventDetected(). And debugging confirmed it.

@fxdeniz fxdeniz added this to the 1.0.0 milestone Aug 17, 2022
fxdeniz added a commit that referenced this issue Mar 17, 2023
fxdeniz added a commit that referenced this issue Mar 19, 2023
@fxdeniz fxdeniz added bug Something isn't working file monitor subsystem labels Mar 23, 2023
@fxdeniz fxdeniz modified the milestones: 1.0.0, 2.0.0 Mar 23, 2023
@fxdeniz fxdeniz self-assigned this Mar 23, 2023
@fxdeniz fxdeniz mentioned this issue Jun 30, 2023
2 tasks
@fxdeniz fxdeniz modified the milestones: 2.0.0, 1.0.0 Oct 24, 2023
fxdeniz added a commit that referenced this issue Nov 26, 2023
This issue is being transferred. Timeline may not be complete until it finishes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working file monitor subsystem
Projects
None yet
Development

No branches or pull requests

1 participant