Skip to content

fix: Enhance file monitoring capabilities#361

Merged
deepin-bot[bot] merged 2 commits intolinuxdeepin:masterfrom
Kakueeen:master
Aug 13, 2025
Merged

fix: Enhance file monitoring capabilities#361
deepin-bot[bot] merged 2 commits intolinuxdeepin:masterfrom
Kakueeen:master

Conversation

@Kakueeen
Copy link
Copy Markdown
Contributor

@Kakueeen Kakueeen commented Aug 12, 2025

  • Added functionality to monitor newly created directories and handle pending directories.
  • Implemented parent directory monitoring for non-existing paths.
  • Improved logging for directory changes and monitoring status.

Log: Enhance file monitoring capabilities
Bug: https://pms.uniontech.com/bug-view-327673.html

Summary by Sourcery

Enhance file monitoring by adding support for parent-directory watchers for non-existent paths, auto-detecting and watching newly created subdirectories, and improving logging and state cleanup across the monitoring lifecycle.

New Features:

  • Monitor parent directories for non-existing paths and track pending directories until they are created
  • Automatically detect and add newly created subdirectories under watched directories

Enhancements:

  • Improve logging to include detailed path information for directory changes, watch additions, and pending directory events
  • Extend the clear() method to reset pending and parent watcher state
  • Simplify FileInotifyGroup by delegating path validation and watch setup directly to FileInotify

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Aug 12, 2025

Reviewer's Guide

This PR refactors FileInotify to support monitoring of not-yet-existing directories by tracking parent directories and pending targets, refines addWatcher to separate existing vs non-existing paths, enriches logging for monitoring events, and improves cleanup logic; it also simplifies FileInotifyGroup to delegate path validation.

Sequence diagram for monitoring non-existing directories via parent watcher

sequenceDiagram
    participant FileInotifyGroup
    participant FileInotify
    participant QFileSystemWatcher
    actor User

    User->>FileInotifyGroup: startWatch(paths, album, UID)
    FileInotifyGroup->>FileInotify: addWather(paths, album, UID)
    FileInotify->>FileInotify: Separate existing/non-existing paths
    FileInotify->>QFileSystemWatcher: addPaths(existingPaths)
    FileInotify->>FileInotify: For each nonExistingPath
    FileInotify->>QFileSystemWatcher: addPath(parentPath)
    FileInotify->>FileInotify: Track pendingDirs and parentToChildren
Loading

Class diagram for updated FileInotify monitoring logic

classDiagram
    class FileInotify {
        - bool m_running
        - QStringList m_newFile
        - QStringList m_deleteFile
        - QStringList m_currentDirs
        - QStringList m_pendingDirs
        - QStringList m_parentDirs
        - QMap<QString, QStringList> m_parentToChildren
        - QString m_currentAlbum
        - int m_currentUID
        - QStringList m_Supported
        + void checkNewPath()
        + void checkPendingDirectories(const QString &changedPath)
        + void addParentWatcher(const QString &parentPath, const QString &targetChild)
        + void addWather(const QStringList &paths, const QString &album, int UID)
        + void clear()
    }

    FileInotify --> QFileSystemWatcher : uses
    FileInotify --> QTimer : uses
Loading

File-Level Changes

Change Details Files
Implement pending and parent directory monitoring
  • Separate input paths into existing and non-existing lists
  • Track non-existing targets in a pending list
  • Register parent directories for non-existing paths
  • Promote pending targets to direct monitoring upon creation
src/src/fileMonitor/fileinotify.cpp
src/src/fileMonitor/fileinotify.h
Add new helper methods for pending directory management
  • Create addParentWatcher to register and map parent-to-child relationships
  • Create checkPendingDirectories to detect and handle newly created directories
src/src/fileMonitor/fileinotify.cpp
src/src/fileMonitor/fileinotify.h
Enhance logging for directory events and monitoring status
  • Log the specific path on directory change events
  • Report when pending directories are found and parent watchers added/removed
  • Detail added direct and parent monitoring actions
src/src/fileMonitor/fileinotify.cpp
Improve cleanup of monitoring state
  • Clear pending, parent, and mapping lists in clear()
  • Safely stop and delete timer only if active
src/src/fileMonitor/fileinotify.cpp
Delegate path validation to FileInotify in the group watcher
  • Remove upfront existence checks in startWatch
  • Pass raw paths to addWatcher for centralized handling
src/src/fileMonitor/fileinotifygroup.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Kakueeen - I've reviewed your changes - here's some feedback:

  • In clear(), besides resetting the member lists, explicitly call m_watcher.removePaths on m_currentDirs and m_parentDirs so the QFileSystemWatcher isn’t left monitoring stale directories.
  • FileInotifyGroup.startWatch now always creates a FileInotify even if no paths can be monitored—consider re-adding the pre-filter step or an early return when there are no existing or parent-watchable directories to avoid idle watchers.
  • The timer intervals (500ms, 1500ms) are hard-coded in several places—extract them into named constants or configuration parameters to make the timing logic clearer and easier to tweak.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In clear(), besides resetting the member lists, explicitly call m_watcher.removePaths on m_currentDirs and m_parentDirs so the QFileSystemWatcher isn’t left monitoring stale directories.
- FileInotifyGroup.startWatch now always creates a FileInotify even if no paths can be monitored—consider re-adding the pre-filter step or an early return when there are no existing or parent-watchable directories to avoid idle watchers.
- The timer intervals (500ms, 1500ms) are hard-coded in several places—extract them into named constants or configuration parameters to make the timing logic clearer and easier to tweak.

## Individual Comments

### Comment 1
<location> `src/src/fileMonitor/fileinotify.cpp:102` </location>
<code_context>
+    QStringList existingPaths;
+    QStringList nonExistingPaths;
+
+    for (const QString &path : paths) {
+        QFileInfo info(path);
+        if (info.exists() && info.isDir()) {
</code_context>

<issue_to_address>
No deduplication of paths before adding to watcher.

Deduplicate input paths to prevent redundant monitoring and improve performance.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH

    // 分类路径:存在的和不存在的
    QStringList existingPaths;
    QStringList nonExistingPaths;

    for (const QString &path : paths) {
        QFileInfo info(path);
        if (info.exists() && info.isDir()) {
            existingPaths.append(path);
        } else {
            nonExistingPaths.append(path);
            qDebug() << "Path does not exist, will try parent monitoring:" << path;
        }
    }
=======
    // 分类路径:存在的和不存在的
    QStringList existingPaths;
    QStringList nonExistingPaths;

    // Deduplicate input paths
    QStringList dedupedPaths = QStringList(paths.toSet().toList());

    for (const QString &path : dedupedPaths) {
        QFileInfo info(path);
        if (info.exists() && info.isDir()) {
            existingPaths.append(path);
        } else {
            nonExistingPaths.append(path);
            qDebug() << "Path does not exist, will try parent monitoring:" << path;
        }
    }
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/src/fileMonitor/fileinotify.cpp:323` </location>
<code_context>
+    for (const QString &foundDir : foundDirs) {
</code_context>

<issue_to_address>
Timer is started for each newly created directory in a loop.

Start the timer once after processing all directories to prevent redundant restarts.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

lzwind
lzwind previously approved these changes Aug 12, 2025
@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot bot commented Aug 13, 2025

TAG Bot

New tag: 6.0.41
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #363

- Added functionality to monitor newly created directories and handle pending directories.
- Implemented parent directory monitoring for non-existing paths.
- Improved logging for directory changes and monitoring status.

Log: Enhance file monitoring capabilities
Bug: https://pms.uniontech.com/bug-view-327673.html
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

代码审查意见:

  1. 代码重复:在FileInotify::checkNewPathFileInotify::addWather中,对路径的检查和分类逻辑是重复的。建议将这部分逻辑提取到一个单独的函数中,以减少代码重复和提高可维护性。

  2. 未使用的变量:在FileInotify::FileInotify构造函数中,m_Supported被赋值了两次,第一次赋值后没有使用,第二次赋值是正确的。应该删除第一次赋值。

  3. 未使用的宏Q_UNUSED(path)FileInotify::FileInotify构造函数中被注释掉了,如果这个变量确实不需要使用,应该删除它以避免混淆。

  4. 日志信息:在FileInotify::checkPendingDirectoriesFileInotify::addParentWatcher中,日志信息应该更加详细,包括时间戳和更具体的上下文信息,以便于调试和问题追踪。

  5. 性能优化:在FileInotify::checkPendingDirectories中,m_pendingDirs.removeAll(foundDir);可能会在m_pendingDirs很大时导致性能问题。考虑使用QSet来存储m_pendingDirs,这样删除操作的时间复杂度可以降低到O(1)。

  6. 内存管理:在FileInotify::~FileInotify中,m_timer被删除后没有设置为nullptr。这可能会导致悬挂指针问题。应该添加m_timer = nullptr;来避免这个问题。

  7. 代码风格:在FileInotify::addWatherFileInotify::checkNewPath中,添加了大量的qDebug()qInfo()日志,这可能会在生产环境中产生大量的日志输出。建议在生产环境中禁用这些日志,或者使用条件编译来控制日志的输出。

  8. 错误处理:在FileInotify::addWather中,当路径不存在时,只是打印了警告信息,但没有采取任何措施来处理这种情况。建议添加错误处理逻辑,比如跳过这些路径或者通知调用者。

  9. 代码注释:在FileInotify::checkPendingDirectoriesFileInotify::addParentWatcher中,添加了一些注释来解释代码的功能。建议在代码中添加更多的注释,特别是对于复杂的逻辑和关键步骤,以提高代码的可读性和可维护性。

  10. 安全性:没有看到代码中涉及到安全性相关的改动,比如文件路径的验证和防止路径遍历攻击。建议在处理文件路径时,进行充分的验证和检查,确保代码的安全性。

以上是针对代码审查意见的总结,希望能够对您有所帮助。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Kakueeen, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Kakueeen
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot bot commented Aug 13, 2025

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 8299cc4 into linuxdeepin:master Aug 13, 2025
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants