gpt4 book ai didi

c - 在收到更改通知后读取文件会带来旧值

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:55 25 4
gpt4 key购买 nike

我在使用 Inotify 时遇到了一些问题。我正在查看配置文件,希望在修改时重新加载配置。可以毫无问题地检测到修改,但是当我读取配置值时,我得到了旧值。假设我有

attrib=value 

然后我修改了它

attrib =value1

然后,当我再次读取文件时,我会得到“value”而不是“value1”。如果我再次修改 attrib,读取时我会得到“value1”。

编辑:PATH_FLDR_CONFIG 指向文件本身。

代码如下:

int length, i = 0;
int inotify;
char buffer[EVENT_BUF_LEN];

if((inotify = inotify_init())<0){
pthread_exit(NULL);
}

inotify_add_watch( inotify, PATH_FLDR_CONFIG , IN_MODIFY );

while (true){
i=0;
length = read( inotify, buffer, EVENT_BUF_LEN );
if (length<0) continue;

while ( i < length ) {
read( inotify, buffer, EVENT_BUF_LEN );
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->mask & IN_MODIFY ) {
readConfigFile();
}
i += EVENT_SIZE + event->len;
}

}

在 readConfigFile() 中我做了一个 configCreate + 各种读取;

t_config *config_create(char *path) {
t_config *config = malloc(sizeof(t_config));

config->path = strdup(path);
config->properties = dictionary_create();

struct stat stat_file;
stat(path, &stat_file);
FILE* file = NULL;

file = fopen(path, "r");

if (file != NULL) {
char* buffer = calloc(1, stat_file.st_size + 1);
fread(buffer, stat_file.st_size, 1, file);

char** lines = string_split(buffer, "\n");

void add_cofiguration(char *line) {
if (!string_starts_with(line, "#")) {
char** keyAndValue = string_split(line, "=");
dictionary_put(config->properties, keyAndValue[0], keyAndValue[1]);
free(keyAndValue[0]);
free(keyAndValue);
}
}
string_iterate_lines(lines, add_cofiguration);
string_iterate_lines(lines, (void*) free);

free(lines);
free(buffer);
fclose(file);
}

return config;
}

char *config_get_string_value(t_config *self, char *key) {
return dictionary_get(self->properties, key);
}

最佳答案

我不确定,但我认为值得检查你的修改通知是发生在你的配置文件还是另一个文件上。

许多编辑器通过先写入一个临时文件来对文件进行更改。例如,如果您的文件名为 config.txt,则可能是通过编写 .config.txt.new#1234 然后重命名为 .config.txt。 new#1234config.txt

由于 inotify 在一个目录上,它可能会看到临时文件的创建和修改,触发通知,您的程序会在它被新文件替换之前读取配置。

另一个可能的问题是一旦对文件调用 write() 就会发生 MODIFY 事件。如果文件是在多个写入调用中写入的,您的程序可能会在配置文件完全写入之前打开并读取它。在您的示例中,配置字典可能包含旧值,如果它们没有被新值覆盖的话。

尝试使用 CLOSE_WRITE 而不是 MODIFY 事件。

关于c - 在收到更改通知后读取文件会带来旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17711255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com