blob: e731ce0d5ccfc9176bc6e1d31ea5754935f8a7aa [file] [log] [blame]
Josiah Gaskin8a39da82011-06-06 17:00:35 -07001//
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
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00008#include <androidfw/PathUtils.h>
Elliott Hughes9ec96f92015-07-29 14:35:18 -07009#include <utils/Compat.h>
Josiah Gaskin8a39da82011-06-06 17:00:35 -070010#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
18using namespace android;
19
20CrunchCache::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
33size_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.
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000048 const char* rPathPtr = mSourceFiles.keyAt(0).c_str()+mSourcePath.length();
Josiah Gaskin8a39da82011-06-06 17:00:35 -070049 // 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)) {
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000056 cu->processImage(appendPathCopy(mSourcePath, relativePath),
57 appendPathCopy(mDestPath, relativePath));
Josiah Gaskin8a39da82011-06-06 17:00:35 -070058 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);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000064 mDestFiles.removeItem(appendPathCopy(mDestPath, relativePath));
Josiah Gaskin8a39da82011-06-06 17:00:35 -070065 }
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
80void 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
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -070098bool CrunchCache::needsUpdating(const String8& relativePath) const
Josiah Gaskin8a39da82011-06-06 17:00:35 -070099{
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.
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000103 time_t sourceDate = mSourceFiles.valueFor(appendPathCopy(mSourcePath, relativePath));
104 time_t destDate = mDestFiles.valueFor(appendPathCopy(mDestPath, relativePath));
Josiah Gaskin8a39da82011-06-06 17:00:35 -0700105 return sourceDate > destDate;
Andreas Gampe2412f842014-09-30 20:55:57 -0700106}