static bool config_file_has_changed() { bool changed = false; if (config_file_exists) { zfile_restat(config_file); if (config_file_last_modified != zfile_modified(config_file)) { const char *new_digest = zfile_digest(config_file); // printf("[D] old digest: %s\n[D] new digest: %s\n", config_file_digest, new_digest); changed = strcmp(config_file_digest, new_digest) != 0; } } return changed; }
static void config_file_init() { config_file = zfile_new(NULL, config_file_name); config_file_last_modified = zfile_modified(config_file); config_file_digest = strdup(zfile_digest(config_file)); }
JNIEXPORT jlong JNICALL Java_org_zeromq_czmq_Zfile__1_1modified (JNIEnv *env, jclass c, jlong self) { jlong modified_ = (jlong) zfile_modified ((zfile_t *) (intptr_t) self); return modified_; }
/// // Return when the file was last modified. If you want this to reflect the // current situation, call zfile_restat before checking this property. time_t QZfile::modified () { time_t rv = zfile_modified (self); return rv; }
/// // Return when the file was last modified. If you want this to reflect the // current situation, call zfile_restat before checking this property. time_t QmlZfile::modified () { return zfile_modified (self); };
zlist_t * zdir_diff (zdir_t *older, zdir_t *newer, const char *alias) { zlist_t *patches = zlist_new (); if (!patches) return NULL; zfile_t **old_files = zdir_flatten (older); zfile_t **new_files = zdir_flatten (newer); int old_index = 0; int new_index = 0; // Note that both lists are sorted, so detecting differences // is rather trivial while (old_files [old_index] || new_files [new_index]) { zfile_t *old_file = old_files [old_index]; zfile_t *new_file = new_files [new_index]; int cmp; if (!old_file) cmp = 1; // Old file was deleted at end of list else if (!new_file) cmp = -1; // New file was added at end of list else cmp = strcmp (zfile_filename (old_file, NULL), zfile_filename (new_file, NULL)); if (cmp > 0) { // New file was created if (zfile_is_stable (new_file)) { int rc = zlist_append (patches, zdir_patch_new (newer->path, new_file, patch_create, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } old_index--; } else if (cmp < 0) { // Old file was deleted if (zfile_is_stable (old_file)) { int rc = zlist_append (patches, zdir_patch_new (older->path, old_file, patch_delete, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } new_index--; } else if (cmp == 0 && zfile_is_stable (new_file)) { if (zfile_is_stable (old_file)) { // Old file was modified or replaced // Since we don't check file contents, treat as created // Could better do SHA check on file here if (zfile_modified (new_file) != zfile_modified (old_file) || zfile_cursize (new_file) != zfile_cursize (old_file)) { int rc = zlist_append (patches, zdir_patch_new (newer->path, new_file, patch_create, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } } else { // File was created over some period of time int rc = zlist_append (patches, zdir_patch_new (newer->path, new_file, patch_create, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } } old_index++; new_index++; } freen (old_files); freen (new_files); return patches; }
zdir_t * zdir_new (const char *path, const char *parent) { zdir_t *self = (zdir_t *) zmalloc (sizeof (zdir_t)); assert (self); if (parent) { if (streq (parent, "-")) { self->trimmed = true; self->path = strdup (path); if (!self->path) { zdir_destroy (&self); return NULL; } } else { self->path = (char *) zmalloc (strlen (path) + strlen (parent) + 2); if (self->path) sprintf (self->path, "%s/%s", parent, path); else { zdir_destroy (&self); return NULL; } } } else { self->path = strdup (path); if (!self->path) { zdir_destroy (&self); return NULL; } } if (self->path) self->files = zlist_new (); if (self->files) self->subdirs = zlist_new (); if (!self->subdirs) { zdir_destroy (&self); return NULL; } #if (defined (WIN32)) // On Windows, replace backslashes by normal slashes char *path_clean_ptr = self->path; while (*path_clean_ptr) { if (*path_clean_ptr == '\\') *path_clean_ptr = '/'; path_clean_ptr++; } // Remove any trailing slash if (self->path [strlen (self->path) - 1] == '/') self->path [strlen (self->path) - 1] = 0; // Win32 wants a wildcard at the end of the path char *wildcard = (char *) zmalloc (strlen (self->path) + 3); if (!wildcard) { zdir_destroy (&self); return NULL; } sprintf (wildcard, "%s/*", self->path); WIN32_FIND_DATAA entry; HANDLE handle = FindFirstFileA (wildcard, &entry); freen (wildcard); if (handle != INVALID_HANDLE_VALUE) { // We have read an entry, so return those values s_win32_populate_entry (self, &entry); while (FindNextFileA (handle, &entry)) s_win32_populate_entry (self, &entry); FindClose (handle); } #else // Remove any trailing slash if (self->path [strlen (self->path) - 1] == '/') self->path [strlen (self->path) - 1] = 0; DIR *handle = opendir (self->path); if (handle) { // readdir_r is deprecated in glibc 2.24, but readdir is still not // guaranteed to be thread safe if the same directory is accessed // by different threads at the same time. Unfortunately given it was // not a constraint before we cannot change it now as it would be an // API breakage. Use a global lock when scanning the directory to // work around it. pthread_mutex_lock (&s_readdir_mutex); struct dirent *entry = readdir (handle); pthread_mutex_unlock (&s_readdir_mutex); while (entry != NULL) { // Beware of recursion. Lock only around readdir calls. s_posix_populate_entry (self, entry); pthread_mutex_lock (&s_readdir_mutex); entry = readdir (handle); pthread_mutex_unlock (&s_readdir_mutex); } closedir (handle); } #endif else { zdir_destroy (&self); return NULL; } // Update directory signatures zdir_t *subdir = (zdir_t *) zlist_first (self->subdirs); while (subdir) { if (self->modified < subdir->modified) self->modified = subdir->modified; self->cursize += subdir->cursize; self->count += subdir->count; subdir = (zdir_t *) zlist_next (self->subdirs); } zfile_t *file = (zfile_t *) zlist_first (self->files); while (file) { if (self->modified < zfile_modified (file)) self->modified = zfile_modified (file); self->cursize += zfile_cursize (file); self->count += 1; file = (zfile_t *) zlist_next (self->files); } return self; }