简体   繁体   English

递归pyinotify监视停止删除子目录

[英]Recursive pyinotify watch stops sub-directories from being removed

This is a strange one and strace is not giving me any useful information. 这是一个奇怪的问题,strace没有提供任何有用的信息。 I am using pyinotify 0.9.6 to watch a directory recursively so I can commit changes about it to a MySQL database. 我正在使用pyinotify 0.9.6递归地查看目录,因此可以将有关它的更改提交到MySQL数据库。 The problem is when I have pyinotify running (daemonized or not), i can remove files in the sub-directories but not the sub-directories themselves. 问题是当我运行了pyinotify(是否进行守护进程)时,我可以删除子目录中的文件,但不能删除子目录本身。 rmdir exits with status 0 and everything looks right on the system level, but the sub directory is still there just chilling. rmdir退出,状态为0,一切看起来都在系统级,但是子目录仍然很冷。 I may just be being stupid as I am new to the framework, but here is an example of how I am initializing the watch: 我可能只是因为刚接触框架而变得愚蠢,但这是一个如何初始化手表的示例:

wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
wm.add_watch('/path/to/dir', pyinotify.IN_CLOSE_WRITE, rec=True, auto_add=True,\
        proc_fun=CommitFunction() # I can supply this if you think it's relevant
notifier.loop(daemonize=True, callback=None,\
        stdout='/path/to/log.log, stderr='/path/to/log.log')

So in this example if there was a file '/path/to/dir/file.txt' I could run a rm on 'file.txt' and it gets deleted, but if there was a sub-directory '/path/to/dir/subdir' running rm -r on 'subdir' exits cleanly but the directory doesn't actually get deleted. 因此,在此示例中,如果存在文件“ /path/to/dir/file.txt”,则可以在“ file.txt”上运行rm并删除它,但是如果存在子目录“ / path / to”在'subdir'上运行rm -r的/ dir / subdir'干净退出,但实际上并未删除该目录。

Furthermore my logs aren't getting written, but I am pretty sure that's my fault somewhere. 而且我的日志没有写,但是我很确定那是我的错。

EDIT: 编辑:

Here is an example CommitFunction: 这是一个示例CommitFunction:

class CommitFunction(pyinotify.ProcessEvent):
    def process_default(self, event):
        dir = event.path
        file = event.name
        print "%s is the new file" % os.path.join(dir, file)

EDIT2: Actually my logs probably aren't getting written because I don't call a log or print function anywhere during the commit. EDIT2:实际上我的日志可能没有被写入,因为在提交过程中我没有在任何地方调用日志或打印功能。 I just write straight to my MySQL database 我只是直接写到我的MySQL数据库

EDIT3: Ok here goes. EDIT3:好了。 Hopefully I am not delving too deep. 希望我不会钻研太深。 Here is what I try on the command line: 这是我在命令行上尝试的方法:

bash$ cd /var/www
bash$ mkdir subdir
bash$ rmdir subdir
bash$ ls
subdir

Here is the actual commit function: 这是实际的提交功能:

class CommitFunction(pyinotify.ProcessEvent):
    def process_default(self, event):
        dir = event.path
        file = event.name
        dbaccess.commit(dir, file) # dir corresponds to a project name

I can keep going deeper if you want, but dbaccess has all my functions for committing and querying the database (nothing really touching the fs) and it further draws from a 'models' file that defines my tables. 如果您愿意,我可以继续深入,但是dbaccess具有我提交和查询数据库的所有功能(实际上并没有碰到fs),并且它进一步从定义我的表的“模型”文件中提取。 It's a flask/uwsgi app if that helps at all 这是一个烧瓶/ uwsgi应用程序,如果有帮助的话

Using this code: 使用此代码:

import pyinotify
import os


class CommitFunction(pyinotify.ProcessEvent):
    def process_default(self, event):
        dir = event.path
        file = event.name
        print "%s is the new file" % os.path.join(dir, file)


wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
wm.add_watch('/tmp/testdir', pyinotify.IN_CLOSE_WRITE, rec=True,
             auto_add=True, proc_fun=CommitFunction())
notifier.loop(daemonize=False, callback=None)

I can create and delete subdirectories in the target directory without error: 我可以在目标目录中创建和删除子目录,而不会出现错误:

bash-4.3$ pwd
/tmp/testdir
bash-4.3$ mkdir subdir
bash-4.3$ rm -r subdir
bash-4.3$ ls
bash-4.3$ 

In response to the above session, the code logged the following on stdout: 作为对以上会话的响应,代码在stdout上记录了以下内容:

/tmp/testdir/subdir is the new file
/tmp/testdir/subdir/ is the new file

Can you confirm whether the above exhibits the behavior you have described in your environment? 您能否确认上述内容是否表现出您在环境中描述的行为? If not, can you update your question with a complete example that does demonstrate the problem? 如果不是,您是否可以使用一个完整的示例来更新您的问题,以证明该问题?

OK so the problem I was having is that IN_CLOSE_WRITE was also getting called on removal of a sub-directory, which in turn was firing a function I had to make sure a directory for the "project" existed when uploaded through the web portal. 好的,所以我遇到的问题是,删除子目录时也会调用IN_CLOSE_WRITE,这反过来又触发了我必须确保通过Web门户上载时“项目”目录存在的功能。

I am still confused, but this particular question is solved 我仍然很困惑,但是这个特殊的问题已经解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM