src/lib/cache.c
changeset 59 80135bdfd343
parent 56 d5e3089906da
child 60 cb6407844ab5
equal deleted inserted replaced
58:d0295e6deb62 59:80135bdfd343
    41 
    41 
    42     return err;
    42     return err;
    43 }
    43 }
    44 
    44 
    45 /**
    45 /**
       
    46  * Force-clean pt_cache, warn on errors
       
    47  */
       
    48 static void pt_cache_abort (struct pt_cache *cache)
       
    49 {
       
    50     if (cache->file != NULL) {
       
    51         if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size))
       
    52             log_warn_errno("munmap: %p, %zu", cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
       
    53 
       
    54         cache->file = NULL;
       
    55     }
       
    56 
       
    57     if (cache->fd >= 0) {
       
    58         if (close(cache->fd))
       
    59             log_warn_errno("close: %d", cache->fd);
       
    60 
       
    61         cache->fd = -1;
       
    62     }
       
    63 }
       
    64 
       
    65 /**
    46  * Open the cache file as an fd for reading
    66  * Open the cache file as an fd for reading
    47  *
    67  *
    48  * XXX: use some kind of locking?
    68  * XXX: use some kind of locking?
    49  */
    69  */
    50 static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
    70 static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
   179         info->cache_blocks = st.st_blocks;
   199         info->cache_blocks = st.st_blocks;
   180     }
   200     }
   181 }
   201 }
   182 
   202 
   183 /**
   203 /**
   184  * Abort any incomplete open operation, cleaning up
       
   185  */
       
   186 static void pt_cache_abort (struct pt_cache *cache)
       
   187 {
       
   188     if (cache->file != NULL) {
       
   189         munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
       
   190 
       
   191         cache->file = NULL;
       
   192     }
       
   193 
       
   194     if (cache->fd >= 0) {
       
   195         close(cache->fd);
       
   196 
       
   197         cache->fd = -1;
       
   198     }
       
   199 }
       
   200 
       
   201 /**
       
   202  * Open the .tmp cache file as an fd for writing
   204  * Open the .tmp cache file as an fd for writing
   203  */
   205  */
   204 static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
   206 static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
   205 {
   207 {
   206     int fd;
   208     int fd;
   418         return err;
   420         return err;
   419 
   421 
   420     return 0;
   422     return 0;
   421 }
   423 }
   422 
   424 
       
   425 int pt_cache_close (struct pt_cache *cache)
       
   426 {
       
   427     if (cache->file != NULL) {
       
   428         if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size))
       
   429             RETURN_ERROR(PT_ERR_CACHE_MUNMAP);
       
   430 
       
   431         cache->file = NULL;
       
   432     }
       
   433 
       
   434     if (cache->fd >= 0) {
       
   435         if (close(cache->fd))
       
   436             RETURN_ERROR(PT_ERR_CACHE_CLOSE);
       
   437 
       
   438         cache->fd = -1;
       
   439     }
       
   440 
       
   441     return 0;
       
   442 }
       
   443 
   423 void pt_cache_destroy (struct pt_cache *cache)
   444 void pt_cache_destroy (struct pt_cache *cache)
   424 {
   445 {
       
   446     // cleanup
       
   447     pt_cache_abort(cache);
       
   448 
   425     free(cache->path);
   449     free(cache->path);
   426 
       
   427     pt_cache_abort(cache);
       
   428 
       
   429     free(cache);
   450     free(cache);
   430 }
   451 }
   431 
   452