blob: dc5493f9e5004d27601a54f1b9954fb678e99916 [file] [log] [blame]
Josiah Gaskin8a39da82011-06-06 17:00:35 -07001//
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 Wasilczyk804e8192023-08-23 02:22:53 +000010#include <androidfw/PathUtils.h>
Josiah Gaskin8a39da82011-06-06 17:00:35 -070011#include <utils/String8.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <stdio.h>
15#include "Images.h"
Elliott Hughese17788c2015-08-17 12:41:46 -070016#ifdef _WIN32
Andrew Hsiehc9d32392014-05-07 20:14:30 +080017#include <direct.h>
18#endif
Josiah Gaskin8a39da82011-06-06 17:00:35 -070019
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000020#include "Utils.h"
21
Josiah Gaskin8a39da82011-06-06 17:00:35 -070022using 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 */
34class CacheUpdater {
35public:
Adam Lesinski4bf58102014-11-03 11:21:19 -080036 virtual ~CacheUpdater() {}
37
Josiah Gaskin8a39da82011-06-06 17:00:35 -070038 // 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;
46private:
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 */
54class SystemCacheUpdater : public CacheUpdater {
55public:
56 // Constructor to set bundle to pass to preProcessImage
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -070057 explicit SystemCacheUpdater (Bundle* b)
Josiah Gaskin8a39da82011-06-06 17:00:35 -070058 : 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 Wasilczykd2a69832023-08-10 23:54:44 +000072 if (stat(path.c_str(),&s) == -1) {
Josiah Gaskin8a39da82011-06-06 17:00:35 -070073 // 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 Wasilczyk804e8192023-08-23 02:22:53 +000078 toCreate = appendPathCopy(getPathLeaf(existsPath), toCreate);
79 existsPath = getPathDir(existsPath);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000080 } while (stat(existsPath.c_str(),&s) == -1);
Josiah Gaskin8a39da82011-06-06 17:00:35 -070081
82 // Walk forwards and build directories as we go
83 do {
84 // Advance to the next segment of the path
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000085 appendPath(existsPath, walkPath(toCreate, &remains));
Josiah Gaskin8a39da82011-06-06 17:00:35 -070086 toCreate = remains;
Elliott Hughese17788c2015-08-17 12:41:46 -070087#ifdef _WIN32
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000088 _mkdir(existsPath.c_str());
Josiah Gaskin8a39da82011-06-06 17:00:35 -070089#else
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000090 mkdir(existsPath.c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
Josiah Gaskin8a39da82011-06-06 17:00:35 -070091#endif
92 } while (remains.length() > 0);
93 } //if
94 };
95
96 // Delete a file
97 virtual void deleteFile(String8 path)
98 {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000099 if (remove(path.c_str()) != 0)
100 fprintf(stderr,"ERROR DELETING %s\n",path.c_str());
Josiah Gaskin8a39da82011-06-06 17:00:35 -0700101 };
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 Wasilczyk804e8192023-08-23 02:22:53 +0000107 ensureDirectoriesExist(getPathDir(dest));
Josiah Gaskin8a39da82011-06-06 17:00:35 -0700108
109 preProcessImageToCache(bundle, source, dest);
110 };
111private:
112 Bundle* bundle;
113};
114
Andreas Gampe2412f842014-09-30 20:55:57 -0700115#endif // CACHE_UPDATER_H