Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2011 The Android Open Source Project |
| 3 | // |
| 4 | // Abstraction of calls to system to make directories and delete files and |
| 5 | // wrapper to image processing. |
| 6 | |
| 7 | #ifndef CACHE_UPDATER_H |
| 8 | #define CACHE_UPDATER_H |
| 9 | |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 10 | #include <androidfw/PathUtils.h> |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 11 | #include <utils/String8.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <stdio.h> |
| 15 | #include "Images.h" |
Elliott Hughes | e17788c | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 16 | #ifdef _WIN32 |
Andrew Hsieh | c9d3239 | 2014-05-07 20:14:30 +0800 | [diff] [blame] | 17 | #include <direct.h> |
| 18 | #endif |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 19 | |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 20 | #include "Utils.h" |
| 21 | |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 22 | using namespace android; |
| 23 | |
| 24 | /** CacheUpdater |
| 25 | * This is a pure virtual class that declares abstractions of functions useful |
| 26 | * for managing a cache files. This manager is set up to be used in a |
| 27 | * mirror cache where the source tree is duplicated and filled with processed |
| 28 | * images. This class is abstracted to allow for dependency injection during |
| 29 | * unit testing. |
| 30 | * Usage: |
| 31 | * To update/add a file to the cache, call processImage |
| 32 | * To remove a file from the cache, call deleteFile |
| 33 | */ |
| 34 | class CacheUpdater { |
| 35 | public: |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 36 | virtual ~CacheUpdater() {} |
| 37 | |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 38 | // Make sure all the directories along this path exist |
| 39 | virtual void ensureDirectoriesExist(String8 path) = 0; |
| 40 | |
| 41 | // Delete a file |
| 42 | virtual void deleteFile(String8 path) = 0; |
| 43 | |
| 44 | // Process an image from source out to dest |
| 45 | virtual void processImage(String8 source, String8 dest) = 0; |
| 46 | private: |
| 47 | }; |
| 48 | |
| 49 | /** SystemCacheUpdater |
| 50 | * This is an implementation of the above virtual cache updater specification. |
| 51 | * This implementations hits the filesystem to manage a cache and calls out to |
| 52 | * the PNG crunching in images.h to process images out to its cache components. |
| 53 | */ |
| 54 | class SystemCacheUpdater : public CacheUpdater { |
| 55 | public: |
| 56 | // Constructor to set bundle to pass to preProcessImage |
Chih-Hung Hsieh | 9b8528f | 2016-08-10 14:15:30 -0700 | [diff] [blame] | 57 | explicit SystemCacheUpdater (Bundle* b) |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 58 | : bundle(b) { }; |
| 59 | |
| 60 | // Make sure all the directories along this path exist |
| 61 | virtual void ensureDirectoriesExist(String8 path) |
| 62 | { |
| 63 | // Check to see if we're dealing with a fully qualified path |
| 64 | String8 existsPath; |
| 65 | String8 toCreate; |
| 66 | String8 remains; |
| 67 | struct stat s; |
| 68 | |
| 69 | // Check optomistically to see if all directories exist. |
| 70 | // If something in the path doesn't exist, then walk the path backwards |
| 71 | // and find the place to start creating directories forward. |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 72 | if (stat(path.c_str(),&s) == -1) { |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 73 | // Walk backwards to find place to start creating directories |
| 74 | existsPath = path; |
| 75 | do { |
| 76 | // As we remove the end of existsPath add it to |
| 77 | // the string of paths to create. |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 78 | toCreate = appendPathCopy(getPathLeaf(existsPath), toCreate); |
| 79 | existsPath = getPathDir(existsPath); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 80 | } while (stat(existsPath.c_str(),&s) == -1); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 81 | |
| 82 | // Walk forwards and build directories as we go |
| 83 | do { |
| 84 | // Advance to the next segment of the path |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 85 | appendPath(existsPath, walkPath(toCreate, &remains)); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 86 | toCreate = remains; |
Elliott Hughes | e17788c | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 87 | #ifdef _WIN32 |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 88 | _mkdir(existsPath.c_str()); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 89 | #else |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 90 | mkdir(existsPath.c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 91 | #endif |
| 92 | } while (remains.length() > 0); |
| 93 | } //if |
| 94 | }; |
| 95 | |
| 96 | // Delete a file |
| 97 | virtual void deleteFile(String8 path) |
| 98 | { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 99 | if (remove(path.c_str()) != 0) |
| 100 | fprintf(stderr,"ERROR DELETING %s\n",path.c_str()); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | // Process an image from source out to dest |
| 104 | virtual void processImage(String8 source, String8 dest) |
| 105 | { |
| 106 | // Make sure we're trying to write to a directory that is extant |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 107 | ensureDirectoriesExist(getPathDir(dest)); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 108 | |
| 109 | preProcessImageToCache(bundle, source, dest); |
| 110 | }; |
| 111 | private: |
| 112 | Bundle* bundle; |
| 113 | }; |
| 114 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 115 | #endif // CACHE_UPDATER_H |