每当在Linux中使用inode创建新文件时,如何获取文件名以及文件的绝对路径? [英] how to get the filename along with absolute path to the file, whenever a new file is created using inode in linux?

查看:558
本文介绍了每当在Linux中使用inode创建新文件时,如何获取文件名以及文件的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux OS(CentOS)上进行了一些实验,我想跟踪在相同环境下创建的所有工具日志,工具生成用于跟踪这些更改的相应日志(.log extn),我写了一个perl watcher,实际上监视我设置的目录,并在创建新文件时将其显示在输出中,但这会消耗大量内存和CPU利用率,因为我将2sec设置为睡眠时间。

I doing some experiments with my linux OS (CentOS) and I want to track all the tool logs created under the same environment, tool generates the respective logs (.log extn) for tracking these changes I wrote a perl watcher which actually monitoring the directory that I set and when the new file is created it will show at the output but This is consuming a lot of memory and CPU utilization as i have set 2sec as the sleep period.

我的问题还有更好的方法吗?我想到了使用inode表来跟踪系统中的所有更改。这可以解决我的问题吗?如果可以,那么可以让我们知道针对相同解决方案的方法吗?

My QUESTION "Is there any better of way doing this ?" I thought of using inode table for tracking all the changes in the system. can this solve my issue ? and if yes then could please let us know the solution towards the same ?

推荐答案

似乎您想监视目录变化。这是一项复杂的工作,但是有很好的模块。最简单的推荐方法可能是 Linux :: Inotify2

It seems that you want to monitor a directory for changes. This is a complex job, but for which there are good modules. The easiest one to recommend is probably Linux::Inotify2


该模块实现了与Linux 2.6.13和更高版本的Inotify文件/目录更改通知系统的接口。

This module implements an interface to the Linux 2.6.13 and later Inotify file/directory change notification system.

这似乎与您想要的一致。

This seems to be along the lines of what you wanted.

任何此类监视器都需要其他事件处理。本示例使用 AnyEvent

Any such monitor needs additional event handling. This example uses AnyEvent.

use warnings;
use strict;
use feature 'say';

use AnyEvent;
use Linux::Inotify2;

my $dir = 'dir_to_watch';

my $inotify = Linux::Inotify2->new  or die "Can't create inotify object: $!";

$inotify->watch( $dir, IN_MODIFY | IN_CREATE, sub {
    my $e = shift;
    my $name = $e->fullname;
    say "$name modified" if $e->IN_MODIFY;    # Both show the new file
    say "$name created"  if $e->IN_CREATE;    # but see comments below
});

my $inotify_w = AnyEvent->io (
    fh => $inotify->fileno, poll => 'r', cb => sub { $inotify->poll }
);

1 while $inotify->poll;

如果只关心 new 文件,则只需要一个常量即可。
对于两种类型的事件, $ name 都具有新文件的名称。从系统上的 man inotify

If you only care about new files then you only need one constant above. For both types of events the $name has the name of the new file. From man inotify on my system


... inotify_event 结构中的> name 字段标识目录中文件的名称。

... the name field in the returned inotify_event structure identifies the name of the file within the directory.

inotify_event 结构由 Linux :: Inotify2 :: Watcher 对象。

使用 IN_CREATE 似乎是针对您目的的明显解决方案。我通过创建两个文件进行了测试,其中两个重定向的 echo 命令在同一命令行上用分号分隔,还用 touch -文件。写入的文件被检测为单独的事件, touch -ed文件也被检测为。

Using IN_CREATE seems to be an obvious solution for your purpose. I tested by creating two files, with two redirected echo commands separated by semi-colon on the same command line, and also by touch-ing a file. The written files are detected as separate events, and so is the touch-ed file.

使用 IN_MODIFY 也可以工作,因为它监视(在 $ dir 中)

Using IN_MODIFY may also work since it monitors (in $dir)


...被监视对象(始终是目录)中的任何文件系统对象,即文件,目录,符号链接,设备节点等。...

... any file system object in the watched object (always a directory), that is files, directories, symlinks, device nodes etc. ...

对于测试,上述由 echo 编写的两个文件均作为单独的事件报告。但是没有报告 修改过的 touch 版本文件,因为数据没有更改(未写入文件)。

As for tests, both files written by echo as above are reported, as separate events. But a touch-ed file is not reported, since data didn't change (the file wasn't written to).

哪个更适合您的需要取决于细节。例如,某个工具可能会在启动时打开日志文件,直到以后再写入日志文件。在这种情况下,以上两种方式的行为将有所不同。所有这些都应在您的特定条件下进行仔细研究。

Which is better suited for your need depends on details. For example, a tool may open a log file as it starts, only to write to it much later. The two ways above will behave differently in that case. All this should be investigated carefully under your specific conditions.

我们可能会想到竞争条件,因为在执行代码的同时,其他文件也可能会进入。该模块远胜于此,它在处理程序完成后会报告新的更改。我通过在代码运行(和休眠)并创建报告时创建文件进行了测试。

We may think of a race condition, since while the code executes other file(s) could slip in. But the module is far better than that and it does report new changes after the handler completes. I tested by creating files while that code runs (and sleeps) and they are reported.

事件驱动编程的其他一些值得注意的框架是 POE IO :: Async

Some other notable frameworks for event-driven programming are POE and IO::Async.

The File :: Monitor 可以完成这种工作也是如此。

The File::Monitor does this kind of work, too.

这篇关于每当在Linux中使用inode创建新文件时,如何获取文件名以及文件的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆