]>
git.saurik.com Git - android/aapt.git/blob - CacheUpdater.h
2 // Copyright 2011 The Android Open Source Project
4 // Abstraction of calls to system to make directories and delete files and
5 // wrapper to image processing.
7 #ifndef CACHE_UPDATER_H
8 #define CACHE_UPDATER_H
10 #include <utils/String8.h>
11 #include <sys/types.h>
16 using namespace android
;
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
25 * To update/add a file to the cache, call processImage
26 * To remove a file from the cache, call deleteFile
30 // Make sure all the directories along this path exist
31 virtual void ensureDirectoriesExist(String8 path
) = 0;
34 virtual void deleteFile(String8 path
) = 0;
36 // Process an image from source out to dest
37 virtual void processImage(String8 source
, String8 dest
) = 0;
41 /** SystemCacheUpdater
42 * This is an implementation of the above virtual cache updater specification.
43 * This implementations hits the filesystem to manage a cache and calls out to
44 * the PNG crunching in images.h to process images out to its cache components.
46 class SystemCacheUpdater
: public CacheUpdater
{
48 // Constructor to set bundle to pass to preProcessImage
49 SystemCacheUpdater (Bundle
* b
)
52 // Make sure all the directories along this path exist
53 virtual void ensureDirectoriesExist(String8 path
)
55 // Check to see if we're dealing with a fully qualified path
61 // Check optomistically to see if all directories exist.
62 // If something in the path doesn't exist, then walk the path backwards
63 // and find the place to start creating directories forward.
64 if (stat(path
.string(),&s
) == -1) {
65 // Walk backwards to find place to start creating directories
68 // As we remove the end of existsPath add it to
69 // the string of paths to create.
70 toCreate
= existsPath
.getPathLeaf().appendPath(toCreate
);
71 existsPath
= existsPath
.getPathDir();
72 } while (stat(existsPath
.string(),&s
) == -1);
74 // Walk forwards and build directories as we go
76 // Advance to the next segment of the path
77 existsPath
.appendPath(toCreate
.walkPath(&remains
));
79 #ifdef HAVE_MS_C_RUNTIME
80 _mkdir(existsPath
.string());
82 mkdir(existsPath
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
84 } while (remains
.length() > 0);
89 virtual void deleteFile(String8 path
)
91 if (remove(path
.string()) != 0)
92 fprintf(stderr
,"ERROR DELETING %s\n",path
.string());
95 // Process an image from source out to dest
96 virtual void processImage(String8 source
, String8 dest
)
98 // Make sure we're trying to write to a directory that is extant
99 ensureDirectoriesExist(dest
.getPathDir());
101 preProcessImageToCache(bundle
, source
, dest
);
107 #endif // CACHE_UPDATER_H