/*{{{ retrieve_file_buf */ static void cache_read_cb (G_GNUC_UNUSED evutil_socket_t fd, G_GNUC_UNUSED short flags, void *ctx) { struct _CacheContext *context = (struct _CacheContext *) ctx; if (context->cb.retrieve_cb) context->cb.retrieve_cb (context->buf, context->size, context->success, context->user_ctx); cache_context_destroy (context); }
/*{{{ store_file_buf */ static void cache_write_cb (G_GNUC_UNUSED evutil_socket_t fd, G_GNUC_UNUSED short flags, void *ctx) { struct _CacheContext *context = (struct _CacheContext *) ctx; if (context->cb.store_cb) context->cb.store_cb (context->success, context->user_ctx); cache_context_destroy (context); }
// 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); }
// 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); }