static int squat_uidlist_map(struct squat_uidlist *uidlist) { const struct squat_uidlist_file_header *mmap_hdr = uidlist->mmap_base; int ret; if (mmap_hdr != NULL && !uidlist->building && uidlist->hdr.block_list_offset == mmap_hdr->block_list_offset) { /* file hasn't changed */ return 1; } if ((uidlist->trie->flags & SQUAT_INDEX_FLAG_MMAP_DISABLE) == 0) { if (mmap_hdr == NULL || uidlist->building || uidlist->mmap_size < mmap_hdr->used_file_size) { if (squat_uidlist_mmap(uidlist) < 0) return -1; } if (!uidlist->building) { memcpy(&uidlist->hdr, uidlist->mmap_base, sizeof(uidlist->hdr)); } } else if (uidlist->building) { /* we want to update blocks mapping, but using the header in memory */ } else { ret = pread_full(uidlist->fd, &uidlist->hdr, sizeof(uidlist->hdr), 0); if (ret <= 0) { if (ret < 0) { i_error("pread(%s) failed: %m", uidlist->path); return -1; } i_error("Corrupted %s: File too small", uidlist->path); return 0; } uidlist->data = NULL; uidlist->data_size = 0; } if (uidlist->file_cache == NULL && (uidlist->trie->flags & SQUAT_INDEX_FLAG_MMAP_DISABLE) != 0) uidlist->file_cache = file_cache_new(uidlist->fd); return squat_uidlist_map_header(uidlist); }
struct file_cache * file_cache_add (struct file_cache *tree, const char *filename, const char *key, file_size_t size, const char *ip, int port) { /* bias determines if we go left or right in the binary tree */ int bias = 0; struct seeder *s; // If the tree is empty, we create it if (!tree) { tree = file_cache_new (filename, key, size, ip, port); if (!tree) { log_failure (log_file, "file_cache_add (): Unable to file_cache_new ()"); return NULL; } } // If the tree isn't empty, recursive call to find the right place else { bias = strcmp (key, tree->key); if (bias < 0) { tree->left = file_cache_add (tree->left, filename, key, size, ip, port); } else if (bias > 0) { tree->right = file_cache_add (tree->right, filename, key, size, ip, port); } /* If a node with the same key already exists */ else { /* Search if the seeder is already registered, if he is, leave */ sem_wait (&tree->seeders_lock); for (s = tree->seeders; s; s = s->next) if (strcmp (s->ip, ip) == 0 && s->port == port) return tree; sem_post (&tree->seeders_lock); /* If the seeder isn't registered, we must add him */ s = (struct seeder *)malloc (sizeof (struct seeder)); if (!s) { log_failure (log_file, "file_cache_add (): Unable to allocate seeder"); return tree; } strcpy (s->ip, ip); s->port = port; sem_wait (&tree->seeders_lock); s->next = tree->seeders; tree->seeders = s; sem_post (&tree->seeders_lock); } } return tree; }