1 //
2 // Copyright 2011 The Android Open Source Project
3 //
4 // Implementation file for CrunchCache
5 // This file defines functions laid out and documented in
6 // CrunchCache.h
7
8 #include <androidfw/PathUtils.h>
9 #include <utils/Compat.h>
10 #include <utils/Vector.h>
11 #include <utils/String8.h>
12
13 #include "DirectoryWalker.h"
14 #include "FileFinder.h"
15 #include "CacheUpdater.h"
16 #include "CrunchCache.h"
17
18 using namespace android;
19
CrunchCache(String8 sourcePath,String8 destPath,FileFinder * ff)20 CrunchCache::CrunchCache(String8 sourcePath, String8 destPath, FileFinder* ff)
21 : mSourcePath(sourcePath), mDestPath(destPath), mSourceFiles(0), mDestFiles(0), mFileFinder(ff)
22 {
23 // We initialize the default value to return to 0 so if a file doesn't exist
24 // then all files are automatically "newer" than it.
25
26 // Set file extensions to look for. Right now just pngs.
27 mExtensions.push(String8(".png"));
28
29 // Load files into our data members
30 loadFiles();
31 }
32
crunch(CacheUpdater * cu,bool forceOverwrite)33 size_t CrunchCache::crunch(CacheUpdater* cu, bool forceOverwrite)
34 {
35 size_t numFilesUpdated = 0;
36
37 // Iterate through the source files and compare to cache.
38 // After processing a file, remove it from the source files and
39 // from the dest files.
40 // We're done when we're out of files in source.
41 String8 relativePath;
42 while (mSourceFiles.size() > 0) {
43 // Get the full path to the source file, then convert to a c-string
44 // and offset our beginning pointer to the length of the sourcePath
45 // This efficiently strips the source directory prefix from our path.
46 // Also, String8 doesn't have a substring method so this is what we've
47 // got to work with.
48 const char* rPathPtr = mSourceFiles.keyAt(0).c_str()+mSourcePath.length();
49 // Strip leading slash if present
50 int offset = 0;
51 if (rPathPtr[0] == OS_PATH_SEPARATOR)
52 offset = 1;
53 relativePath = String8(rPathPtr + offset);
54
55 if (forceOverwrite || needsUpdating(relativePath)) {
56 cu->processImage(appendPathCopy(mSourcePath, relativePath),
57 appendPathCopy(mDestPath, relativePath));
58 numFilesUpdated++;
59 // crunchFile(relativePath);
60 }
61 // Delete this file from the source files and (if it exists) from the
62 // dest files.
63 mSourceFiles.removeItemsAt(0);
64 mDestFiles.removeItem(appendPathCopy(mDestPath, relativePath));
65 }
66
67 // Iterate through what's left of destFiles and delete leftovers
68 while (mDestFiles.size() > 0) {
69 cu->deleteFile(mDestFiles.keyAt(0));
70 mDestFiles.removeItemsAt(0);
71 }
72
73 // Update our knowledge of the files cache
74 // both source and dest should be empty by now.
75 loadFiles();
76
77 return numFilesUpdated;
78 }
79
loadFiles()80 void CrunchCache::loadFiles()
81 {
82 // Clear out our data structures to avoid putting in duplicates
83 mSourceFiles.clear();
84 mDestFiles.clear();
85
86 // Make a directory walker that points to the system.
87 DirectoryWalker* dw = new SystemDirectoryWalker();
88
89 // Load files in the source directory
90 mFileFinder->findFiles(mSourcePath, mExtensions, mSourceFiles,dw);
91
92 // Load files in the destination directory
93 mFileFinder->findFiles(mDestPath,mExtensions,mDestFiles,dw);
94
95 delete dw;
96 }
97
needsUpdating(const String8 & relativePath) const98 bool CrunchCache::needsUpdating(const String8& relativePath) const
99 {
100 // Retrieve modification dates for this file entry under the source and
101 // cache directory trees. The vectors will return a modification date of 0
102 // if the file doesn't exist.
103 time_t sourceDate = mSourceFiles.valueFor(appendPathCopy(mSourcePath, relativePath));
104 time_t destDate = mDestFiles.valueFor(appendPathCopy(mDestPath, relativePath));
105 return sourceDate > destDate;
106 }
107