blob: 0af58723c0ea9e9caf7792cf7d9bfee9efc82519 [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
10#include <utils/String8.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <stdio.h>
14#include "Images.h"
15
16using namespace android;
17
18/** CacheUpdater
19 * This is a pure virtual class that declares abstractions of functions useful
20 * for managing a cache files. This manager is set up to be used in a
21 * mirror cache where the source tree is duplicated and filled with processed
22 * images. This class is abstracted to allow for dependency injection during
23 * unit testing.
24 * Usage:
25 * To update/add a file to the cache, call processImage
26 * To remove a file from the cache, call deleteFile
27 */
28class CacheUpdater {
29public:
30 // Make sure all the directories along this path exist
31 virtual void ensureDirectoriesExist(String8 path) = 0;
32
33 // Delete a file
34 virtual void deleteFile(String8 path) = 0;
35
36 // Process an image from source out to dest
37 virtual void processImage(String8 source, String8 dest) = 0;
Andreas Gampe2412f842014-09-30 20:55:57 -070038
39 virtual ~CacheUpdater() {}
Josiah Gaskin8a39da82011-06-06 17:00:35 -070040private:
41};
42
43/** SystemCacheUpdater
44 * This is an implementation of the above virtual cache updater specification.
45 * This implementations hits the filesystem to manage a cache and calls out to
46 * the PNG crunching in images.h to process images out to its cache components.
47 */
48class SystemCacheUpdater : public CacheUpdater {
49public:
50 // Constructor to set bundle to pass to preProcessImage
51 SystemCacheUpdater (Bundle* b)
52 : bundle(b) { };
53
54 // Make sure all the directories along this path exist
55 virtual void ensureDirectoriesExist(String8 path)
56 {
57 // Check to see if we're dealing with a fully qualified path
58 String8 existsPath;
59 String8 toCreate;
60 String8 remains;
61 struct stat s;
62
63 // Check optomistically to see if all directories exist.
64 // If something in the path doesn't exist, then walk the path backwards
65 // and find the place to start creating directories forward.
66 if (stat(path.string(),&s) == -1) {
67 // Walk backwards to find place to start creating directories
68 existsPath = path;
69 do {
70 // As we remove the end of existsPath add it to
71 // the string of paths to create.
72 toCreate = existsPath.getPathLeaf().appendPath(toCreate);
73 existsPath = existsPath.getPathDir();
74 } while (stat(existsPath.string(),&s) == -1);
75
76 // Walk forwards and build directories as we go
77 do {
78 // Advance to the next segment of the path
79 existsPath.appendPath(toCreate.walkPath(&remains));
80 toCreate = remains;
81#ifdef HAVE_MS_C_RUNTIME
82 _mkdir(existsPath.string());
83#else
84 mkdir(existsPath.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
85#endif
86 } while (remains.length() > 0);
87 } //if
88 };
89
90 // Delete a file
91 virtual void deleteFile(String8 path)
92 {
93 if (remove(path.string()) != 0)
94 fprintf(stderr,"ERROR DELETING %s\n",path.string());
95 };
96
97 // Process an image from source out to dest
98 virtual void processImage(String8 source, String8 dest)
99 {
100 // Make sure we're trying to write to a directory that is extant
101 ensureDirectoriesExist(dest.getPathDir());
102
103 preProcessImageToCache(bundle, source, dest);
104 };
105private:
106 Bundle* bundle;
107};
108
Andreas Gampe2412f842014-09-30 20:55:57 -0700109#endif // CACHE_UPDATER_H