+void add_file(struct processtop *proc, struct files *file, int fd)
+{
+ struct files *tmp_file;
+ struct processtop *parent;
+ int size;
+ int i;
+
+ size = proc->process_files_table->len;
+ parent = proc->threadparent;
+ if (parent)
+ insert_file(parent, fd);
+ if (size <= fd) {
+ /* Add NULL file structures for undefined FDs */
+ for (i = size; i < fd; i++) {
+ g_ptr_array_add(proc->process_files_table, NULL);
+ }
+ g_ptr_array_add(proc->process_files_table, file);
+ } else {
+ tmp_file = g_ptr_array_index(proc->process_files_table, fd);
+ if (tmp_file == NULL)
+ g_ptr_array_index(proc->process_files_table, fd) = file;
+ else {
+ if (strcmp(tmp_file->name, file->name) != 0) {
+ size = proc->process_files_table->len;
+ g_ptr_array_set_size(proc->process_files_table,
+ size+1);
+ g_ptr_array_index(proc->process_files_table,
+ size) = tmp_file;
+ g_ptr_array_index(proc->process_files_table,
+ fd) = file;
+ } else
+ tmp_file->flag = __NR_open;
+ }
+ }
+ /*
+ * The file may have be created in the parent
+ */
+ if (file->flag == -1) {
+ file->fd = fd;
+ file->flag = __NR_open;
+ lttngtop.nbfiles++;
+ lttngtop.nbnewfiles++;
+ }
+}
+
+/*
+ * Edit the file
+ * Called by handled_statedump_filename
+ */
+void edit_file(struct processtop *proc, struct files *file, int fd)
+{
+ int size = proc->process_files_table->len;
+ struct files *tmpfile;
+
+ if (fd >= size) {
+ add_file(proc, file, fd);
+ } else {
+ tmpfile = g_ptr_array_index(proc->process_files_table, fd);
+ if (tmpfile) {
+ tmpfile->name = strdup(file->name);
+ free(file);
+ } else
+ add_file(proc, file, fd);
+ }
+}
+
+void insert_file(struct processtop *proc, int fd)
+{
+ struct files *tmp;
+ struct files *tmp_parent;
+ struct processtop *parent;
+
+ if (fd < 0)
+ return;
+ if (fd >= proc->process_files_table->len) {
+ tmp = g_new0(struct files, 1);
+ tmp->name = "Unknown";
+ tmp->read = 0;
+ tmp->write = 0;
+ tmp->fd = fd;
+ tmp->flag = -1;
+ add_file(proc, tmp, fd);
+ } else {
+ tmp = g_ptr_array_index(proc->process_files_table, fd);
+ if (tmp == NULL) {
+ tmp = g_new0(struct files, 1);
+ tmp->name = "Unknown";
+ tmp->read = 0;
+ tmp->write = 0;
+ tmp->fd = fd;
+ tmp->flag = -1;
+ add_file(proc, tmp, fd);
+ } else {
+ parent = proc->threadparent;
+ if (parent) {
+ tmp_parent = g_ptr_array_index(
+ parent->process_files_table, fd);
+ if (tmp_parent &&
+ (strcmp(tmp->name, tmp_parent->name)) != 0)
+ tmp->name = strdup(tmp_parent->name);
+ }
+ }
+ }
+}
+
+void close_file(struct processtop *proc, int fd)
+{
+ struct files *file;
+
+ file = get_file(proc, fd);
+ if (file != NULL) {
+ file->flag = __NR_close;
+ lttngtop.nbfiles--;
+ }
+ lttngtop.nbclosedfiles++;
+}
+
+struct files *get_file(struct processtop *proc, int fd)
+{
+ int len;
+ struct files *tmp = NULL;
+
+ len = proc->process_files_table->len;
+
+ /*
+ * It is possible that a file was open before taking the trace
+ * and its fd could be greater than all of the others fd
+ * used by the process
+ */
+ if (fd < len && fd >= 0)
+ tmp = g_ptr_array_index(proc->process_files_table, fd);
+
+ return tmp;
+}
+
+void show_table(GPtrArray *tab)
+{
+ int i;
+ struct files *file;
+
+ for (i = 0 ; i < tab->len; i++) {
+ file = g_ptr_array_index(tab, i);
+ if (file == NULL)
+ fprintf(stderr, "NULL, ");
+ else
+ fprintf(stderr, "%s, ", file->name);
+ }
+ fprintf(stderr, "]\n\n");
+}
+
+void show_history(struct file_history *history)
+{
+ struct file_history *tmp = history;
+
+ while (tmp != NULL) {
+ fprintf(stderr, "fd = %d, name = %s\n", tmp->file->fd,
+ tmp->file->name);
+ tmp = tmp->next;
+ }
+
+}