How to detect directory resizing in Perl

I am trying to find a way to monitor directories in Perl, in particular the size of the directory, and also, when a change in the size of the directory is detected, take a specific action.
The problem I am facing is large files requiring a significant amount of time to copy to this directory, i.e. > 100 MB. What happens (on Windows, not on Unix), the system reserves enough disk space for the entire file, although the file is still being copied. This causes problems for me because my script will try to take action on this file that has not finished copying yet. I can easily detect directory size changes on Unix through "du", but "du" on Windows does not behave the same.

Are there any exact methods for detecting directory size changes in Perl?

Edit: some points to clarify: - My Perl script controls only a specific directory, and when it detects a new file or new directory, it takes action on this new file or directory. It does not copy files; users on the network will copy the files to a directory that I control. - The problem occurs when a new file or directory is displayed (copied, not moved), which is significantly large (> 100 MB, but usually a couple of GB) and my program runs before this copy ends   - On Unix, I can easily "du" see that the file / directory in question grows in size and takes appropriate action - On Windows, the size is static, so I cannot detect this change - opendir / readdir / closedir is not possible since some of the directories displayed may contain thousands of files, and I want to avoid overhead

Ideally, I would like my program to work when it changes, but I don’t know how to do it. At the moment, he is busy until he finds a change. Resizing a file / directory is not in my control.

+3
source share
5 answers

, , , - . , , ?

+3

, . , . , , ?

:

1) flock, , (, , Perl).

2) LockFile Windows. , OS .

3) .

+1

- , , Perl. du 15 , :

  • glob, opendir / readdir / closedir
  • filetest (-f file, -d file ..),
  • stat -s file
0

File:: Monitor, , , , stat. .

http://metacpan.org/pod/File::Monitor

, , ,

$monitor->watch( {
    name        => 'somedir',
    recurse     => 1,
    callback    => {
        files_created => sub {
            my ($name, $event, $change) = @_;
            # Do stuff
        }
    }
} );

If you need to go deeper than one level, just do it at any level that you need. Once this is done and it finds new files, you can run the application to do what you want in the files.

0
source

Source: https://habr.com/ru/post/1748892/


All Articles