src/lib/cache.c
changeset 2 05de54150a4c
parent 1 f3cde3db1fef
child 3 da7c6dcafb43
equal deleted inserted replaced
1:f3cde3db1fef 2:05de54150a4c
     4 #include <unistd.h>
     4 #include <unistd.h>
     5 #include <sys/types.h>
     5 #include <sys/types.h>
     6 #include <sys/stat.h>
     6 #include <sys/stat.h>
     7 #include <fcntl.h>
     7 #include <fcntl.h>
     8 #include <sys/mman.h>
     8 #include <sys/mman.h>
       
     9 #include <errno.h>
     9 
    10 
    10 
    11 
    11 
    12 
    12 static int pt_cache_new (struct pt_cache **cache_ptr, const char *path)
    13 static int pt_cache_new (struct pt_cache **cache_ptr, const char *path)
    13 {
    14 {
    48     *cache_ptr = cache;
    49     *cache_ptr = cache;
    49 
    50 
    50     return 0;
    51     return 0;
    51 }
    52 }
    52 
    53 
    53 int pt_cache_stale (struct pt_cache *cache, const char *orig_path)
    54 int pt_cache_stale (struct pt_cache *cache, const char *img_path)
    54 {
    55 {
    55     // TODO: stat + mtime
    56     struct stat st_img, st_cache;
    56     return false;
    57     
       
    58     // test original file
       
    59     if (stat(img_path, &st_img) < 0)
       
    60         return -1;
       
    61     
       
    62     // test cache file
       
    63     if (stat(cache->path, &st_cache) < 0) {
       
    64         // always stale if it doesn't exist yet
       
    65         if (errno == ENOENT)
       
    66             return 1;
       
    67         else
       
    68             return -1;
       
    69     }
       
    70 
       
    71     // compare mtime
       
    72     return (st_img.st_mtime > st_cache.st_mtime);
    57 }
    73 }
    58 
    74 
    59 /**
    75 /**
    60  * Abort any incomplete open operation, cleaning up
    76  * Abort any incomplete open operation, cleaning up
    61  */
    77  */
   159 /**
   175 /**
   160  * Create a new cache file, open it, and write out the header.
   176  * Create a new cache file, open it, and write out the header.
   161  */
   177  */
   162 static int pt_cache_open_create (struct pt_cache *cache, struct pt_cache_header *header)
   178 static int pt_cache_open_create (struct pt_cache *cache, struct pt_cache_header *header)
   163 {
   179 {
       
   180     // no access
       
   181     if (!(cache->mode & PT_IMG_WRITE)) {
       
   182         errno = EPERM;
       
   183         return -1;
       
   184     }
       
   185 
   164     // open
   186     // open
   165     if (pt_cache_open_fd(cache, &cache->fd))
   187     if (pt_cache_open_fd(cache, &cache->fd))
   166         return -1;
   188         return -1;
   167 
   189 
   168     // calculate data size
   190     // calculate data size