// we can only get md5 of an object containing 1 range // XXX: move code to separate thread gboolean cache_mng_get_md5 (CacheMng *cmng, fuse_ino_t ino, gchar **md5str) { struct _CacheEntry *entry; unsigned char digest[MD5_DIGEST_LENGTH]; MD5_CTX md5ctx; ssize_t bytes; unsigned char data[1024]; char path[PATH_MAX]; size_t i; gchar *out; FILE *in; entry = g_hash_table_lookup (cmng->h_entries, GUINT_TO_POINTER (ino)); if (!entry) return FALSE; if (range_count (entry->avail_range) != 1) { LOG_debug (CMNG_LOG, INO_H"Entry contains more than 1 range, can't take MD5 sum of such object !", INO_T (ino)); return FALSE; } cache_mng_file_name (cmng, path, sizeof (path), ino); in = fopen (path, "rb"); if (in == NULL) { LOG_debug (CMNG_LOG, INO_H"Can't open file for reading: %s", INO_T (ino), path); return FALSE; } MD5_Init (&md5ctx); while ((bytes = fread (data, 1, 1024, in)) != 0) MD5_Update (&md5ctx, data, bytes); MD5_Final (digest, &md5ctx); fclose (in); out = g_malloc (33); for (i = 0; i < 16; ++i) sprintf (&out[i*2], "%02x", (unsigned int)digest[i]); *md5str = out; return TRUE; }
// retrieve file buffer from local storage // if success == TRUE then "buf" contains "size" bytes of data void cache_mng_retrieve_file_buf (CacheMng *cmng, fuse_ino_t ino, size_t size, off_t off, cache_mng_on_retrieve_file_buf_cb on_retrieve_file_buf_cb, void *ctx) { struct _CacheContext *context; struct _CacheEntry *entry; context = cache_context_create (size, ctx); context->cb.retrieve_cb = on_retrieve_file_buf_cb; entry = g_hash_table_lookup (cmng->h_entries, GUINT_TO_POINTER (ino)); if (entry && range_contain (entry->avail_range, off, off + size)) { int fd; ssize_t res; char path[PATH_MAX]; if (ino != entry->ino) { LOG_err (CMNG_LOG, "Requested inode doesn't match hashed key!"); if (context->cb.retrieve_cb) context->cb.retrieve_cb (NULL, 0, FALSE, context->user_ctx); cache_context_destroy (context); return; } cache_mng_file_name (cmng, path, sizeof (path), ino); fd = open (path, O_RDONLY); context->buf = g_malloc (size); res = pread (fd, context->buf, size, off); close (fd); context->success = (res == (ssize_t) size); if (!context->success) { g_free (context->buf); context->buf = NULL; } // move entry to the front of q_lru g_queue_unlink (cmng->q_lru, entry->ll_lru); g_queue_push_head_link (cmng->q_lru, entry->ll_lru); } else { LOG_debug (CMNG_LOG, "Entry isn't found or doesn't contain requested range: %"INO_FMT, INO ino); } context->ev = event_new (application_get_evbase (cmng->app), -1, 0, cache_read_cb, context); // fire this event at once event_active (context->ev, 0, 0); event_add (context->ev, NULL); }
// removes file from local storage void cache_mng_remove_file (CacheMng *cmng, fuse_ino_t ino) { struct _CacheEntry *entry; char path[PATH_MAX]; entry = g_hash_table_lookup (cmng->h_entries, GUINT_TO_POINTER (ino)); if (entry) { cmng->size -= range_length (entry->avail_range); g_queue_delete_link (cmng->q_lru, entry->ll_lru); g_hash_table_remove (cmng->h_entries, GUINT_TO_POINTER (ino)); cache_mng_file_name (cmng, path, sizeof (path), ino); unlink (path); LOG_debug (CMNG_LOG, INO_H"Entry is removed", INO_T (ino)); } else { LOG_debug (CMNG_LOG, INO_H"Entry not found", INO_T (ino)); } }
// store file buffer into local storage // if success == TRUE then "buf" successfuly stored on disc void cache_mng_store_file_buf (CacheMng *cmng, fuse_ino_t ino, size_t size, off_t off, unsigned char *buf, cache_mng_on_store_file_buf_cb on_store_file_buf_cb, void *ctx) { struct _CacheContext *context; struct _CacheEntry *entry; ssize_t res; int fd; char path[PATH_MAX]; guint64 old_length, new_length; guint64 range_size; time_t now; range_size = (guint64)(off + size); // limit the number of cache checks now = time (NULL); if (cmng->check_time < now && now - cmng->check_time >= 10) { // remove data until we have at least size bytes of max_size left while (cmng->max_size < cmng->size + size && g_queue_peek_tail (cmng->q_lru)) { entry = (struct _CacheEntry *) g_queue_peek_tail (cmng->q_lru); cache_mng_remove_file (cmng, entry->ino); } cmng->check_time = now; } context = cache_context_create (size, ctx); context->cb.store_cb = on_store_file_buf_cb; cache_mng_file_name (cmng, path, sizeof (path), ino); fd = open (path, O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) { LOG_err (CMNG_LOG, INO_H"Failed to create / open file for writing! Path: %s", INO_T (ino), path); if (context->cb.store_cb) context->cb.store_cb (FALSE, context->user_ctx); cache_context_destroy (context); return; } res = pwrite(fd, buf, size, off); close (fd); entry = g_hash_table_lookup (cmng->h_entries, GUINT_TO_POINTER (ino)); if (!entry) { entry = cache_entry_create (ino); g_queue_push_head (cmng->q_lru, entry); entry->ll_lru = g_queue_peek_head_link (cmng->q_lru); g_hash_table_insert (cmng->h_entries, GUINT_TO_POINTER (ino), entry); } old_length = range_length (entry->avail_range); range_add (entry->avail_range, off, range_size); new_length = range_length (entry->avail_range); if (new_length >= old_length) cmng->size += new_length - old_length; else { LOG_err (CMNG_LOG, INO_H"New length is less than the old length !: %"G_GUINT64_FORMAT" <= %"G_GUINT64_FORMAT, INO_T (ino), new_length, old_length); } // update modification time entry->modification_time = time (NULL); context->success = (res == (ssize_t) size); LOG_debug (CMNG_LOG, INO_H"Written [%"OFF_FMT":%zu] bytes, result: %s", INO_T (ino), off, size, context->success ? "OK" : "Failed"); context->ev = event_new (application_get_evbase (cmng->app), -1, 0, cache_write_cb, context); // fire this event at once event_active (context->ev, 0, 0); event_add (context->ev, NULL); }