检测文件删除在fopen [英] Detecting file deletion after fopen

查看:465
本文介绍了检测文件删除在fopen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯在一个code,它在文件中检测(一个日志文件)的变化工作那么其过程的帮助fseek的FTELL和变化。但是如果文件被删除,改变了(与logrotate的)程序停止而不是死了,因为它无法检测到更多的变化(即使文件被重新创建)。 fseek的不显示错误和eiter FTELL。
我怎么可以检测文件删除?也许一种方式重新与其他FILE * var和比较文件描述符的文件。但我怎么能做到这一点。 ?

im working in a code that detects changes in a file (a log file) then its process the changes with the help of fseek and ftell. but if the file get deleted and changed (with logrotate) the program stops but not dies, because it not detect more changes (even if the file is recreated). fseek dont show errors and eiter ftell. how i can detect that file deletion? maybe a way to reopen the file with other FILE *var and comparing file descriptor. but how i can do that. ?

推荐答案

当一个文件被删除,它不一定是从磁盘删除。在你的情况下,程序仍然有一个处理旧文件。旧的文件句柄不会得到你有关其删除或替换另一个文件中的任何信息。

When a file gets deleted, it is not necessarily erased from your disk. In your case the program still has a handle to the old file. The old file handle will not get you any information about its deletion or replacement with another file.

一个简单的方法来检测文件删除和娱乐使用STAT(2)和FSTAT(2)。他们给你一个结构统计,其中包含的文件索引节点。当一个文件被重新创建(现在仍然打开)中的文件(旧开放和重建)是不同的,因此,索引节点是不同的。 i节点字段是st_ino中。是的,你需要的,除非你希望使用的Linux功能,如inotify的轮询这一点。

An easy way to detect file deletion and recreation is using stat(2) and fstat(2). They give you a struct stat which contains the inode for the file. When a file is recreated (and still open) the files (old open and recreated) are different and thus the inodes are different. The inode field is st_ino. Yes, you need to poll this unless you wish to use Linux-features like inotify.

这篇关于检测文件删除在fopen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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