| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
#ifdef _WIN32 |
| 16 |
#include <direct.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
using namespace android; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class CacheUpdater { |
| 32 |
public: |
| 33 |
virtual ~CacheUpdater() {} |
| 34 |
|
| 35 |
|
| 36 |
virtual void ensureDirectoriesExist(String8 path) = 0; |
| 37 |
|
| 38 |
|
| 39 |
virtual void deleteFile(String8 path) = 0; |
| 40 |
|
| 41 |
|
| 42 |
virtual void processImage(String8 source, String8 dest) = 0; |
| 43 |
private: |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
class SystemCacheUpdater : public CacheUpdater { |
| 52 |
public: |
| 53 |
|
| 54 |
explicit SystemCacheUpdater (Bundle* b) |
| 55 |
: bundle(b) { }; |
| 56 |
|
| 57 |
|
| 58 |
virtual void ensureDirectoriesExist(String8 path) |
| 59 |
{ |
| 60 |
|
| 61 |
String8 existsPath; |
| 62 |
String8 toCreate; |
| 63 |
String8 remains; |
| 64 |
struct stat s; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| •69 |
if (stat(path.c_str(),&s) == -1) { |
| 70 |
|
| 71 |
existsPath = path; |
| 72 |
do { |
| 73 |
|
| 74 |
|
| 75 |
toCreate = existsPath.getPathLeaf().appendPath(toCreate); |
| 76 |
existsPath = existsPath.getPathDir(); |
| •77 |
} while (stat(existsPath.c_str(),&s) == -1); |
| 78 |
|
| 79 |
|
| 80 |
do { |
| 81 |
|
| 82 |
existsPath.appendPath(toCreate.walkPath(&remains)); |
| 83 |
toCreate = remains; |
| 84 |
#ifdef _WIN32 |
| •85 |
_mkdir(existsPath.c_str()); |
| 86 |
#else |
| •87 |
mkdir(existsPath.c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 88 |
#endif |
| 89 |
} while (remains.length() > 0); |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
virtual void deleteFile(String8 path) |
| 95 |
{ |
| •96 |
if (remove(path.c_str()) != 0) |
| 97 |
fprintf(stderr,"ERROR DELETING %s\n",path.c_str()); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
virtual void processImage(String8 source, String8 dest) |
| 102 |
{ |
| 103 |
|
| 104 |
ensureDirectoriesExist(dest.getPathDir()); |
| 105 |
|
| 106 |
preProcessImageToCache(bundle, source, dest); |
| 107 |
}; |
| 108 |
private: |
| 109 |
Bundle* bundle; |
| 110 |
}; |
| 111 |
|
| 112 |
#endif |