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