English 中文(简体)
如何知道档案是否正在复制?
原标题:How to know if a file is being copied?
  • 时间:2011-05-14 17:17:53
  •  标签:
  • c
  • file

I am currently trying to check wether the copy of a file from a directory to another is done. I would like to know if the target file is still being copied.

So I would like to get the number of file descriptors openned on this file. I use C langage and don t really find a way to resolve that problem.

问题回答

如果你能够控制,我建议使用doing<>>><>>>上的复印件。 复印件:

cp file1 otherdir/.file1.tmp
mv otherdir/.file1.tmp otherdir/file1

The mv just changes some filesystem entries and is atomic and very fast compared to the copy.

如果你能够重新打开书面档案,那么,监督厅就很有可能完成复印件,并放下了锁。 然而,在这方面,不同的操作系统可能不同。

Another approach is to open both the source and destination files for reading and compare their sizes. If they re of identical size, the copy has very likely finished. You can use fseek() and ftell() to determine the size of a file in C:

fseek(fp, 0L, SEEK_END);
sz = ftell(fp);

在轴心中,试着lsof的指令,该指令列出了所有公开档案。

编辑 1 The only C 语有:fstat。 您或许能够利用“结构指示”栏(t)_mtime(最后修改时间)领域——一旦这一价值停止变化(例如,10秒),你可以假定文件复印业务已经停止。

edit 2: also, on linux, you could traverse /proc/[pid]/fd to see which files are open. The files in there are symlinks, but C s readlink() function could tell you its path, so you could see whether it is still open. Using getpid(), you would know the process ID of your program (if you are doing a file copy from within your program) to know where to look in /proc.

我认为,你的基本错误是试图使一个C方案与一个不希望同步的手法/外部方案同步。 如果您对复印件的方案/文字有一定程度的控制,则您应加以修改,在目标文档中以某种方式(最好是<代码>fcntl为基础)进行咨询锁。 然后,你的其他方案只能阻碍获得锁。

If you don t have any control over the program performing the copy, the only solutions depend on non-portable hacks like lsof or Linux inotify API.

(这一答案使人大而大的假设是,这种情况将持续在含水层上。)

The C source code of lsof, a tool that tells which programs currently have an open file descriptor to a specific file, is freely available. However, just to warn you, I couldn t make any sense out of it. There are references to reading kernel memory, so to me it s either voodoo or black magic.

尽管如此,没有任何东西阻止你操作<<>tlof。 通过您自己的方案。 从你自己的方案中执行第三方方案,通常是你试图避免的几种原因,例如安全(如果恶意方案使用人改动<<>lsof<>code>,则会利用你的方案特权,并可能产生catastrophic consequences)但检查<<>lsof。 来源代码,我得出结论,没有公开的APIC,以确定哪些方案已经开放。 如果你不害怕在<代码>/usr/sbin上改变方案的人,你可以考虑这样做。

int isOpen(const char* file)
{
    char* command;
    // BE AWARE THAT THIS WILL NOT WORK IF THE FILE NAME CONTAINS A DOUBLE QUOTE
    // OR IF IT CAN SOMEHOW BE ALTERED THROUGH SHELL EXPANSION
    // you should either try to fix it yourself, or use a function of the `exec`
    // family that won t trigger shell expansion.
    // It would be an EXTREMELY BAD idea to call `lsof` without an absolute path
    // since it could result in another program being run. If this is not where
    // `lsof` resides on your system, change it to the appropriate absolute path.
    asprintf(&command, "/usr/sbin/lsof "%s"", file);
    int result = system(command);

    free(command);
    return result;
}

如果你还需要知道贵国的档案(推定cp?),那么你也可以以类似方式使用<代码>popen<>/code>读到。 <代码>popen descriptors behave such as f open descriptors, so all You need to do is fread them and see You can seek their program s name. 在我的机器上,<代码>lsof输出情况如下:

$ lsof document.pdf 
COMMAND PID  USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
SomeApp 873 felix  txt    REG   14,3   303260 5165763 document.pdf

如上文所述,“ fstat()”职能可给你目前的修改时间。 但是,数字还给你档案的大小。

当我监测由各种方案复制的档案时,我一直没有控制我:

  1. Waited until the target file size was >= the source size, and
  2. Waited until the target modification time was at least N seconds older than the current time. N being a number such a 5, and set larger if experience showed that was necessary. Yes 5 seconds seems extreme, but it is safe.

如果你不了解目标文件是什么,那么你唯一真正的选择是第2号,但使用较大的N号,以允许情况恶化的网络和当地CPU的拖延,同时要有健康的安全因素。

利用增量平衡解决问题

boost::filesystem::fstream fileStream(filePath, std::ios_base::in | std::ios_base::binary);

if(fileStream.is_open())
    //not getting copied
else
    //Wait, the file is getting copied




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...