terom@0: #include "cache.h" terom@4: #include "shared/util.h" terom@6: #include "shared/log.h" // only LOG_DEBUG terom@17: #include "error.h" terom@0: terom@1: #include terom@177: #include terom@1: #include terom@1: #include terom@1: #include terom@1: #include terom@1: #include terom@2: #include terom@6: #include terom@1: terom@1: terom@9: int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode) terom@0: { terom@0: struct pt_cache *cache; terom@17: int err; terom@0: terom@1: // alloc terom@0: if ((cache = calloc(1, sizeof(*cache))) == NULL) terom@17: RETURN_ERROR(PT_ERR_MEM); terom@0: terom@1: if ((cache->path = strdup(path)) == NULL) terom@17: JUMP_SET_ERROR(err, PT_ERR_MEM); terom@1: terom@1: // init terom@1: cache->fd = -1; terom@3: cache->mode = mode; terom@1: terom@1: // ok terom@1: *cache_ptr = cache; terom@1: terom@1: return 0; terom@1: terom@1: error: terom@1: // cleanup terom@1: if (cache) terom@1: pt_cache_destroy(cache); terom@1: terom@17: return err; terom@1: } terom@1: terom@56: /** terom@59: * Force-clean pt_cache, warn on errors terom@59: */ terom@59: static void pt_cache_abort (struct pt_cache *cache) terom@59: { terom@59: if (cache->file != NULL) { terom@59: if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size)) terom@59: log_warn_errno("munmap: %p, %zu", cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size); terom@59: terom@59: cache->file = NULL; terom@59: } terom@59: terom@59: if (cache->fd >= 0) { terom@59: if (close(cache->fd)) terom@59: log_warn_errno("close: %d", cache->fd); terom@59: terom@59: cache->fd = -1; terom@59: } terom@59: } terom@59: terom@59: /** terom@56: * Open the cache file as an fd for reading terom@56: */ terom@56: static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr) terom@56: { terom@56: int fd; terom@56: terom@56: // actual open() terom@56: if ((fd = open(cache->path, O_RDONLY)) < 0) terom@56: RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES); terom@56: terom@56: // ok terom@56: *fd_ptr = fd; terom@56: terom@56: return 0; terom@56: } terom@56: terom@56: /** terom@56: * Read in the cache header from the open file terom@56: */ terom@56: static int pt_cache_read_header (int fd, struct pt_cache_header *header) terom@56: { terom@56: size_t len = sizeof(*header); terom@56: char *buf = (char *) header; terom@56: terom@56: // seek to start terom@56: if (lseek(fd, 0, SEEK_SET) != 0) terom@56: RETURN_ERROR(PT_ERR_CACHE_SEEK); terom@56: terom@56: // write out full header terom@56: while (len) { terom@56: ssize_t ret; terom@56: terom@56: // try and write out the header terom@56: if ((ret = read(fd, buf, len)) <= 0) terom@56: RETURN_ERROR(PT_ERR_CACHE_READ); terom@56: terom@56: // update offset terom@56: buf += ret; terom@56: len -= ret; terom@56: } terom@56: terom@56: // done terom@56: return 0; terom@56: } terom@56: terom@56: /** terom@56: * Read and return the version number from the cache file, temporarily opening it if needed terom@56: */ terom@56: static int pt_cache_version (struct pt_cache *cache) terom@56: { terom@56: int fd; terom@56: struct pt_cache_header header; terom@56: int ret; terom@56: terom@56: // already open? terom@56: if (cache->file) terom@56: return cache->file->header.version; terom@56: terom@56: // temp. open terom@56: if ((ret = pt_cache_open_read_fd(cache, &fd))) terom@56: return ret; terom@56: terom@56: // read header terom@56: if ((ret = pt_cache_read_header(fd, &header))) terom@56: JUMP_ERROR(ret); terom@56: terom@56: // ok terom@56: ret = header.version; terom@56: terom@56: error: terom@56: // close terom@56: close(fd); terom@56: terom@56: return ret; terom@56: } terom@56: terom@8: int pt_cache_status (struct pt_cache *cache, const char *img_path) terom@0: { terom@2: struct stat st_img, st_cache; terom@56: int ver; terom@2: terom@2: // test original file terom@2: if (stat(img_path, &st_img) < 0) terom@17: RETURN_ERROR(PT_ERR_IMG_STAT); terom@2: terom@2: // test cache file terom@2: if (stat(cache->path, &st_cache) < 0) { terom@2: // always stale if it doesn't exist yet terom@2: if (errno == ENOENT) terom@8: return PT_CACHE_NONE; terom@2: else terom@17: RETURN_ERROR(PT_ERR_CACHE_STAT); terom@2: } terom@2: terom@2: // compare mtime terom@8: if (st_img.st_mtime > st_cache.st_mtime) terom@8: return PT_CACHE_STALE; terom@56: terom@56: // read version terom@56: if ((ver = pt_cache_version(cache)) < 0) terom@56: // fail terom@56: return ver; terom@8: terom@56: // compare version terom@56: if (ver != PT_CACHE_VERSION) terom@56: return PT_CACHE_INCOMPAT; terom@56: terom@56: // ok, should be in order terom@56: return PT_CACHE_FRESH; terom@0: } terom@0: terom@56: void pt_cache_info (struct pt_cache *cache, struct pt_image_info *info) terom@10: { terom@54: struct stat st; terom@17: terom@56: if (cache->file) terom@56: // img info terom@56: pt_png_info(&cache->file->header.png, info); terom@10: terom@54: // stat terom@54: if (stat(cache->path, &st) < 0) { terom@54: // unknown terom@54: info->cache_mtime = 0; terom@54: info->cache_bytes = 0; terom@54: info->cache_blocks = 0; terom@54: terom@54: } else { terom@54: // store terom@56: info->cache_version = pt_cache_version(cache); terom@54: info->cache_mtime = st.st_mtime; terom@54: info->cache_bytes = st.st_size; terom@54: info->cache_blocks = st.st_blocks; terom@54: } terom@10: } terom@10: terom@76: static int pt_cache_tmp_name (struct pt_cache *cache, char tmp_path[], size_t tmp_len) terom@66: { terom@66: // get .tmp path terom@76: if (path_with_fext(cache->path, tmp_path, tmp_len, ".tmp")) terom@66: RETURN_ERROR(PT_ERR_PATH); terom@66: terom@66: return 0; terom@66: } terom@66: terom@0: /** terom@71: * Compute and return the full size of the .cache file terom@71: */ terom@71: static size_t pt_cache_size (size_t data_size) terom@71: { terom@71: assert(sizeof(struct pt_cache_file) == PT_CACHE_HEADER_SIZE); terom@71: terom@71: return sizeof(struct pt_cache_file) + data_size; terom@71: } terom@71: terom@71: /** terom@4: * Open the .tmp cache file as an fd for writing terom@4: */ terom@6: static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr) terom@4: { terom@4: int fd; terom@4: char tmp_path[1024]; terom@66: int err; terom@66: terom@4: // get .tmp path terom@76: if ((err = pt_cache_tmp_name(cache, tmp_path, sizeof(tmp_path)))) terom@66: return err; terom@4: terom@66: // open for write, create, fail if someone else already opened it for update terom@66: if ((fd = open(tmp_path, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_OPEN_TMP); terom@4: terom@4: // ok terom@4: *fd_ptr = fd; terom@4: terom@4: return 0; terom@4: } terom@4: terom@4: terom@4: /** terom@71: * Mmap the pt_cache_file using pt_cache_size(data_size) terom@1: */ terom@60: static int pt_cache_open_mmap (struct pt_cache *cache, struct pt_cache_file **file_ptr, size_t data_size, bool readonly) terom@1: { terom@1: int prot = 0; terom@1: void *addr; terom@1: terom@1: // determine prot terom@6: prot |= PROT_READ; terom@1: terom@9: if (!readonly) { terom@11: assert(cache->mode & PT_OPEN_UPDATE); terom@9: terom@1: prot |= PROT_WRITE; terom@9: } terom@1: terom@53: // mmap() the full file including header terom@71: if ((addr = mmap(NULL, pt_cache_size(data_size), prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED) terom@17: RETURN_ERROR(PT_ERR_CACHE_MMAP); terom@1: terom@1: // ok terom@60: *file_ptr = addr; terom@1: terom@1: return 0; terom@1: } terom@1: terom@56: int pt_cache_open (struct pt_cache *cache) terom@9: { terom@56: struct pt_cache_header header; terom@56: int err; terom@9: terom@56: // ignore if already open terom@56: if (cache->file) terom@56: return 0; terom@9: terom@56: // open the .cache in readonly mode terom@56: if ((err = pt_cache_open_read_fd(cache, &cache->fd))) terom@56: return err; terom@56: terom@56: // read in header terom@56: if ((err = pt_cache_read_header(cache->fd, &header))) terom@56: JUMP_ERROR(err); terom@56: terom@56: // check version terom@56: if (header.version != PT_CACHE_VERSION) terom@56: JUMP_SET_ERROR(err, PT_ERR_CACHE_VERSION); terom@56: terom@56: // mmap the header + data terom@60: if ((err = pt_cache_open_mmap(cache, &cache->file, header.data_size, true))) terom@56: JUMP_ERROR(err); terom@9: terom@9: // done terom@9: return 0; terom@56: terom@56: error: terom@56: // cleanup terom@56: pt_cache_abort(cache); terom@56: terom@56: return err; terom@9: } terom@9: terom@9: /** terom@1: * Write out the cache header into the opened file terom@1: */ terom@1: static int pt_cache_write_header (struct pt_cache *cache, const struct pt_cache_header *header) terom@1: { terom@1: size_t len = sizeof(*header); terom@1: const char *buf = (const char *) header; terom@1: terom@1: // seek to start terom@1: if (lseek(cache->fd, 0, SEEK_SET) != 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_SEEK); terom@1: terom@1: // write out full header terom@1: while (len) { terom@1: ssize_t ret; terom@1: terom@1: // try and write out the header terom@17: if ((ret = write(cache->fd, buf, len)) <= 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_WRITE); terom@1: terom@1: // update offset terom@1: buf += ret; terom@1: len -= ret; terom@1: } terom@1: terom@1: // done terom@1: return 0; terom@1: } terom@1: terom@1: /** terom@4: * Create a new .tmp cache file, open it, and write out the header. terom@0: */ terom@11: static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header) terom@0: { terom@17: int err; terom@7: terom@66: assert(cache->mode & PT_OPEN_UPDATE); terom@2: terom@4: // open as .tmp terom@17: if ((err = pt_cache_open_tmp_fd(cache, &cache->fd))) terom@17: return err; terom@1: terom@56: // write header terom@56: if ((err = pt_cache_write_header(cache, header))) terom@56: JUMP_ERROR(err); terom@1: terom@1: // grow file terom@71: if (ftruncate(cache->fd, pt_cache_size(header->data_size)) < 0) terom@17: JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC); terom@1: terom@7: // mmap header and data terom@60: if ((err = pt_cache_open_mmap(cache, &cache->file, header->data_size, false))) terom@17: JUMP_ERROR(err); terom@1: terom@1: // done terom@1: return 0; terom@1: terom@1: error: terom@1: // cleanup terom@1: pt_cache_abort(cache); terom@1: terom@17: return err; terom@0: } terom@0: terom@4: /** terom@4: * Rename the opened .tmp to .cache terom@4: */ terom@4: static int pt_cache_create_done (struct pt_cache *cache) terom@4: { terom@4: char tmp_path[1024]; terom@66: int err; terom@4: terom@4: // get .tmp path terom@76: if ((err = pt_cache_tmp_name(cache, tmp_path, sizeof(tmp_path)))) terom@66: return err; terom@4: terom@4: // rename terom@4: if (rename(tmp_path, cache->path) < 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_RENAME_TMP); terom@4: terom@4: // ok terom@4: return 0; terom@4: } terom@4: terom@66: /** terom@66: * Abort a failed cache update after cache_create terom@66: */ terom@66: static void pt_cache_create_abort (struct pt_cache *cache) terom@66: { terom@66: char tmp_path[1024]; terom@66: int err; terom@66: terom@66: // close open stuff terom@66: pt_cache_abort(cache); terom@66: terom@66: // get .tmp path terom@76: if ((err = pt_cache_tmp_name(cache, tmp_path, sizeof(tmp_path)))) { terom@66: log_warn_errno("pt_cache_tmp_name: %s: %s", cache->path, pt_strerror(err)); terom@66: terom@66: return; terom@66: } terom@66: terom@66: // remove .tmp terom@66: if (unlink(tmp_path)) terom@66: log_warn_errno("unlink: %s", tmp_path); terom@66: } terom@66: terom@56: int pt_cache_update (struct pt_cache *cache, struct pt_png_img *img, const struct pt_image_params *params) terom@0: { terom@0: struct pt_cache_header header; terom@17: int err; terom@66: terom@66: // check mode terom@66: if (!(cache->mode & PT_OPEN_UPDATE)) terom@66: RETURN_ERROR(PT_ERR_OPEN_MODE); terom@66: terom@66: // close if open terom@66: if ((err = pt_cache_close(cache))) terom@66: return err; terom@56: terom@56: // prep header terom@56: header.version = PT_CACHE_VERSION; terom@56: header.format = PT_IMG_PNG; terom@1: terom@56: // read img header terom@56: if ((err = pt_png_read_header(img, &header.png, &header.data_size))) terom@56: return err; terom@6: terom@56: // save any params terom@52: if (params) terom@52: header.params = *params; terom@52: terom@56: // create/open .tmp and write out header terom@17: if ((err = pt_cache_create(cache, &header))) terom@17: return err; terom@56: terom@56: // decode to disk terom@56: if ((err = pt_png_decode(img, &cache->file->header.png, &cache->file->header.params, cache->file->data))) terom@66: goto error; terom@7: terom@56: // done, commit .tmp terom@17: if ((err = pt_cache_create_done(cache))) terom@66: goto error; terom@66: terom@66: return 0; terom@4: terom@66: error: terom@66: // cleanup .tmp terom@66: pt_cache_create_abort(cache); terom@66: terom@66: return err; terom@12: } terom@12: terom@56: int pt_cache_tile (struct pt_cache *cache, struct pt_tile *tile) terom@34: { terom@34: int err; terom@34: terom@34: // ensure open terom@34: if ((err = pt_cache_open(cache))) terom@34: return err; terom@34: terom@56: // render terom@56: if ((err = pt_png_tile(&cache->file->header.png, cache->file->data, tile))) terom@56: return err; terom@34: terom@9: return 0; terom@9: } terom@9: terom@59: int pt_cache_close (struct pt_cache *cache) terom@59: { terom@59: if (cache->file != NULL) { terom@59: if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size)) terom@59: RETURN_ERROR(PT_ERR_CACHE_MUNMAP); terom@59: terom@59: cache->file = NULL; terom@59: } terom@59: terom@59: if (cache->fd >= 0) { terom@59: if (close(cache->fd)) terom@59: RETURN_ERROR(PT_ERR_CACHE_CLOSE); terom@59: terom@59: cache->fd = -1; terom@59: } terom@59: terom@59: return 0; terom@59: } terom@59: terom@1: void pt_cache_destroy (struct pt_cache *cache) terom@1: { terom@59: // cleanup terom@1: pt_cache_abort(cache); terom@1: terom@59: free(cache->path); terom@1: free(cache); terom@1: } terom@11: