我想确定一个特定的名称更改的编辑器一样texstudio或geany。我使用inotify
#include<stdio.h>
#include<sys/inotify.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<fcntl.h> // library for fcntl function
//inotify_02.c
#define MAX_EVENTS 1024 /* Maximum number of events to process*/
#define LEN_NAME 1024 /* Assuming that the length of the filename
won't exceed 16 bytes*/
#define EVENT_SIZE ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME ))
/*buffer to store the data of events*/
int file_descriptor,watch_descriptor;
void sig_handler(int sig){
/* Step 5. Remove the watch descriptor and close the inotify instance*/
inotify_rm_watch( file_descriptor, watch_descriptor );
close( file_descriptor );
exit( 0 );
}
int main(int argc, char **argv){
char *path_to_be_watched;
signal(SIGINT,sig_handler);
path_to_be_watched = argv[1];
/* Step 1. Initialize inotify */
file_descriptor = inotify_init();
if (fcntl(file_descriptor, F_SETFL, O_NONBLOCK) < 0) // error checking for fcntl
exit(2);
/* Step 2. Add Watch */
watch_descriptor = inotify_add_watch(file_descriptor,path_to_be_watched,IN_MODIFY | IN_CREATE | IN_DELETE);
if(watch_descriptor==-1)
printf("Could not watch : %s\n",path_to_be_watched);
else
printf("Watching : %s\n",path_to_be_watched);
while(1)
{
int i=0,length;
char buffer[BUF_LEN];
/* Step 3. Read buffer*/
length = read(file_descriptor,buffer,BUF_LEN);
/* Step 4. Process the events which has occurred */
while(i<length)
{
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if(event->len)
{
if ( event->mask & IN_CLOSE )
/* IN_CLOSE Equates to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
* File or directory not opened for writing was closed. or
* File opened for writing was closed.
*/
{
if ( event->mask & IN_ISDIR )
{
printf( "File or directory %s not opened for writing was closed.\n", event->name );
}
else
{
printf( "File %sopened for writing was closed.\n", event->name );
}
}
/*-------*/
else if ( event->mask & IN_CREATE )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was created.\n", event->name );
}
else
{
printf( "The file %s was created.\n", event->name );
}
}
/*-------*/
else if ( event->mask & IN_DELETE )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was deleted.\n", event->name );
}
else
{
printf( "The file %s was deleted.\n", event->name );
}
}
/*-------*/
else if ( event->mask & IN_MODIFY )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was modified.\n", event->name );
}
else
{
printf( "The file %s was modified.\n", event->name );
}
}
}
i += EVENT_SIZE + event->len;
}
}
}
字符串
当我使用这个软件与arg中给定的目录,我得到这个,因为文件更改与编辑器((inotify_02.c)
Watching : /home/francis/.../inotify
The file .goutputstream-V0EIG2 was created.
The file .goutputstream-V0EIG2 was modified.
型
显然. goutputstream-V0 EIG 2就像进程名而不是文件名。
当我复制文件(inotify_02.c)我有新文件的名称重复
Watching : /home/francis/.../inotify
The file .goutputstream-V0EIG2 was created.
The file .goutputstream-V0EIG2 was modified.
The file inotify_02 (copie).c was created.
The file inotify_02 (copie).c was modified.
型
我发现了这个:但是,要小心文件编辑器,他们通常不在站点上编辑文件,而是覆盖它(删除然后重写)。因此监视IN_MODIFY事件将没有用。在这种情况下,最相关的是监视IN_CLOSE_WRITE / IN_MOVED_FROM(取决于编辑器的工作方式),这使得只有在文件稳定时才能触发操作,而不是在每次写入时()
默认情况下,因为我独自一人与我的PC工作,我现在是什么。但当乳胶编译发生了很多变化。我的程序只需要识别特定文件的变化。
有没有办法得到真正的名字?
1条答案
按热度按时间vyswwuz21#
一个mwe为了显示stat给给予足够的数据文件:名称,日期,权限等一个.
如果一个手表文件的日期改变,这对我有好处
字符串