void node_create_children_snapshot(node_t *f, gint created_event, gboolean emit) { GDir *dir; GError *err = NULL; FN_W ("%s %s [0x%p]\n", __func__, NODE_NAME(f), f); dir = g_dir_open (NODE_NAME(f), 0, &err); if (dir) { const char *basename; node_t *child = NULL; while ((basename = g_dir_read_name (dir))) { node_t* data; GList *idx; child = node_get_child (f, basename); if (child == NULL) { gchar *filename; child = node_new (f, basename); children_add (f, child); } if (f->dir_subs) { /* We need monitor the new children, or the existed child which * is in the DELETED mode. */ if (!NODE_HAS_STATE(child, NODE_STATE_ASSOCIATED) && node_lstat(child) == 0 && port_add(child) == 0) { if (emit) { /* Emit the whatever event for the new found file. */ node_emit_one_event(child, child->dir_subs, NULL, created_event); node_emit_one_event(child, child->subs, NULL, created_event); node_emit_one_event(child, f->dir_subs, NULL, created_event); node_emit_one_event(child, f->subs, NULL, created_event); } } /* else ignore, because it may be deleted. */ } } g_dir_close (dir); /* We have finished children snapshot. Any other new added subs should * directory iterate the snapshot instead of scan directory again. */ NODE_SET_FLAG(f, NODE_FLAG_SNAPSHOT_UPDATED); } else { FN_W (err->message); g_error_free (err); } }
/* add a child to the list */ static int server_add_child(server_t *s, pid_t pid, backend_t *be) { child_t *child = NULL; dbg_err_if (s == NULL); dbg_err_if (be == NULL); dbg_err_if(child_create(pid, be, &child)); dbg_err_if(children_add(s->children, child)); be->nchild++; return 0; err: return ~0; }
node_t* node_find(node_t* node, const gchar* filename, gboolean create_on_missing) { gchar* str; gchar* token; gchar* lasts; node_t* parent; node_t* child; g_assert (filename && filename[0] == '/'); if (node == NULL) { node = ROOT; } FN_W ("%s %s\n", __func__, filename); parent = child = node; str = g_strdup (filename); for (token = strtok_r (str, G_DIR_SEPARATOR_S, &lasts); token != NULL && child != NULL; token = strtok_r (NULL, G_DIR_SEPARATOR_S, &lasts)) { FN_W ("%s %s + %s\n", __func__, NODE_NAME(parent), token); child = node_get_child(parent, token); if (child) { parent = child; } else if (create_on_missing) { child = node_new (parent, token); if (child) { children_add (parent, child); parent = child; continue; } else { FN_W ("%s create %s failed", __func__, token); } } else { break; } } g_free (str); return child; }