Move String8 path functions to androidfw and aapt
Test: m checkbuild
Bug: 295394788
Change-Id: I9488bc5632cbd47c83f6b5f2df4c87eb324a1e8e
diff --git a/tools/aapt/AaptAssets.cpp b/tools/aapt/AaptAssets.cpp
index 0fc2617..0aaf3e8 100644
--- a/tools/aapt/AaptAssets.cpp
+++ b/tools/aapt/AaptAssets.cpp
@@ -7,7 +7,9 @@
#include "AaptUtil.h"
#include "Main.h"
#include "ResourceFilter.h"
+#include "Utils.h"
+#include <androidfw/PathUtils.h>
#include <utils/misc.h>
#include <utils/SortedVector.h>
@@ -96,7 +98,7 @@
char *matchedPattern = NULL;
String8 fullPath(root);
- fullPath.appendPath(path);
+ appendPath(fullPath, String8(path));
FileType type = getFileType(fullPath);
int plen = strlen(path);
@@ -508,7 +510,7 @@
{
if (hasData()) {
String8 name(mGroupEntry.toDirName(String8()));
- name.appendPath(mPath);
+ appendPath(name, mPath);
name.append(" #generated");
return name;
}
@@ -615,7 +617,7 @@
String8 remain = path;
sp<AaptDir> subdir = this;
- while (name = remain.walkPath(&remain), remain != "") {
+ while (name = walkPath(remain, &remain), remain != "") {
subdir = subdir->makeDir(name);
}
@@ -623,7 +625,7 @@
if (i >= 0) {
return subdir->mDirs.valueAt(i);
}
- sp<AaptDir> dir = new AaptDir(name, subdir->mPath.appendPathCopy(name));
+ sp<AaptDir> dir = new AaptDir(name, appendPathCopy(subdir->mPath, name));
subdir->mDirs.add(name, dir);
return dir;
}
@@ -645,7 +647,7 @@
if (mFiles.indexOfKey(leafName) >= 0) {
group = mFiles.valueFor(leafName);
} else {
- group = new AaptGroup(leafName, mPath.appendPathCopy(leafName));
+ group = new AaptGroup(leafName, appendPathCopy(mPath, leafName));
mFiles.add(leafName, group);
}
@@ -684,7 +686,7 @@
// Add fully qualified path for dependency purposes
// if we're collecting them
if (fullResPaths != NULL) {
- fullResPaths->add(srcDir.appendPathCopy(name));
+ fullResPaths->add(appendPathCopy(srcDir, name));
}
}
closedir(dir);
@@ -701,7 +703,7 @@
String8 pathName(srcDir);
FileType type;
- pathName.appendPath(fileNames[i].c_str());
+ appendPath(pathName, fileNames[i]);
type = getFileType(pathName.c_str());
if (type == kFileTypeDirectory) {
sp<AaptDir> subdir;
@@ -709,7 +711,7 @@
if (mDirs.indexOfKey(fileNames[i]) >= 0) {
subdir = mDirs.valueFor(fileNames[i]);
} else {
- subdir = new AaptDir(fileNames[i], mPath.appendPathCopy(fileNames[i]));
+ subdir = new AaptDir(fileNames[i], appendPathCopy(mPath, fileNames[i]));
notAdded = true;
}
ssize_t res = subdir->slurpFullTree(bundle, pathName, kind,
@@ -821,11 +823,11 @@
{
if (mFiles.size() > 0) {
// Arbitrarily pull the first file out of the list as the source dir.
- return mFiles.valueAt(0)->getPrintableSource().getPathDir();
+ return getPathDir(mFiles.valueAt(0)->getPrintableSource());
}
if (mDirs.size() > 0) {
// Or arbitrarily pull the first dir out of the list as the source dir.
- return mDirs.valueAt(0)->getPrintableSource().getPathDir();
+ return getPathDir(mDirs.valueAt(0)->getPrintableSource());
}
// Should never hit this case, but to be safe...
@@ -908,8 +910,8 @@
sp<AaptFile> file;
String8 root, remain(filePath), partialPath;
while (remain.length() > 0) {
- root = remain.walkPath(&remain);
- partialPath.appendPath(root);
+ root = walkPath(remain, &remain);
+ appendPath(partialPath, root);
const String8 rootStr(root);
@@ -924,7 +926,7 @@
return NULL;
}
}
- file = new AaptFile(srcDir.appendPathCopy(filePath), entry, resType);
+ file = new AaptFile(appendPathCopy(srcDir, filePath), entry, resType);
status_t res = group->addFile(file);
if (res != NO_ERROR) {
return NULL;
@@ -981,7 +983,7 @@
if (bundle->getAndroidManifestFile() != NULL) {
// place at root of zip.
String8 srcFile(bundle->getAndroidManifestFile());
- addFile(srcFile.getPathLeaf(), AaptGroupEntry(), srcFile.getPathDir(),
+ addFile(getPathLeaf(srcFile), AaptGroupEntry(), getPathDir(srcFile),
NULL, String8());
totalCount++;
}
@@ -1154,7 +1156,7 @@
}
String8 subdirName(srcDir);
- subdirName.appendPath(entry->d_name);
+ appendPath(subdirName, entry->d_name);
AaptGroupEntry group;
String8 resType;
@@ -1239,16 +1241,16 @@
String8 entryName(entry->getFileName());
- String8 dirName = entryName.getPathDir();
+ String8 dirName = getPathDir(entryName);
sp<AaptDir> dir = dirName == "" ? this : makeDir(dirName);
String8 resType;
AaptGroupEntry kind;
String8 remain;
- if (entryName.walkPath(&remain) == kResourceDir) {
+ if (walkPath(entryName, &remain) == kResourceDir) {
// these are the resources, pull their type out of the directory name
- kind.initFromDirName(remain.walkPath().c_str(), &resType);
+ kind.initFromDirName(walkPath(remain).c_str(), &resType);
} else {
// these are untyped and don't have an AaptGroupEntry
}
@@ -1258,10 +1260,10 @@
}
// use the one from the zip file if they both exist.
- dir->removeFile(entryName.getPathLeaf());
+ dir->removeFile(getPathLeaf(entryName));
sp<AaptFile> file = new AaptFile(entryName, kind, resType);
- status_t err = dir->addLeafFile(entryName.getPathLeaf(), file);
+ status_t err = dir->addLeafFile(getPathLeaf(entryName), file);
if (err != NO_ERROR) {
fprintf(stderr, "err=%s entryName=%s\n", strerror(err), entryName.c_str());
count = err;
@@ -1374,7 +1376,7 @@
// containing no entries.
continue;
}
- if (file->getPath().getPathExtension() == ".xml") {
+ if (getPathExtension(file->getPath()) == ".xml") {
// We can't remove .xml files at this point, because when
// we parse them they may add identifier resources, so
// removing them can cause our resource identifiers to
@@ -1411,7 +1413,7 @@
// containing no entries.
continue;
}
- if (file->getPath().getPathExtension() == ".xml") {
+ if (getPathExtension(file->getPath()) == ".xml") {
// We can't remove .xml files at this point, because when
// we parse them they may add identifier resources, so
// removing them can cause our resource identifiers to
diff --git a/tools/aapt/Android.bp b/tools/aapt/Android.bp
index cecd95a..68db56d 100644
--- a/tools/aapt/Android.bp
+++ b/tools/aapt/Android.bp
@@ -51,6 +51,10 @@
"libz",
],
+ whole_static_libs: [
+ "libandroidfw_pathutils",
+ ],
+
cflags: [
"-Wall",
"-Werror",
diff --git a/tools/aapt/CacheUpdater.h b/tools/aapt/CacheUpdater.h
index 2dc143c..dc5493f 100644
--- a/tools/aapt/CacheUpdater.h
+++ b/tools/aapt/CacheUpdater.h
@@ -7,6 +7,7 @@
#ifndef CACHE_UPDATER_H
#define CACHE_UPDATER_H
+#include <androidfw/PathUtils.h>
#include <utils/String8.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -16,6 +17,8 @@
#include <direct.h>
#endif
+#include "Utils.h"
+
using namespace android;
/** CacheUpdater
@@ -72,14 +75,14 @@
do {
// As we remove the end of existsPath add it to
// the string of paths to create.
- toCreate = existsPath.getPathLeaf().appendPath(toCreate);
- existsPath = existsPath.getPathDir();
+ toCreate = appendPathCopy(getPathLeaf(existsPath), toCreate);
+ existsPath = getPathDir(existsPath);
} while (stat(existsPath.c_str(),&s) == -1);
// Walk forwards and build directories as we go
do {
// Advance to the next segment of the path
- existsPath.appendPath(toCreate.walkPath(&remains));
+ appendPath(existsPath, walkPath(toCreate, &remains));
toCreate = remains;
#ifdef _WIN32
_mkdir(existsPath.c_str());
@@ -101,7 +104,7 @@
virtual void processImage(String8 source, String8 dest)
{
// Make sure we're trying to write to a directory that is extant
- ensureDirectoriesExist(dest.getPathDir());
+ ensureDirectoriesExist(getPathDir(dest));
preProcessImageToCache(bundle, source, dest);
};
diff --git a/tools/aapt/Command.cpp b/tools/aapt/Command.cpp
index 60f3f27..800466a 100644
--- a/tools/aapt/Command.cpp
+++ b/tools/aapt/Command.cpp
@@ -12,6 +12,7 @@
#include "ResourceTable.h"
#include "XMLNode.h"
+#include <androidfw/PathUtils.h>
#include <utils/Errors.h>
#include <utils/KeyedVector.h>
#include <utils/List.h>
@@ -2486,12 +2487,12 @@
for (int i = 1; i < bundle->getFileSpecCount(); i++) {
const char* fileName = bundle->getFileSpecEntry(i);
- if (strcasecmp(String8(fileName).getPathExtension().c_str(), ".gz") == 0) {
+ if (strcasecmp(getPathExtension(String8(fileName)).c_str(), ".gz") == 0) {
printf(" '%s'... (from gzip)\n", fileName);
- result = zip->addGzip(fileName, String8(fileName).getBasePath().c_str(), NULL);
+ result = zip->addGzip(fileName, getBasePath(String8(fileName)).c_str(), NULL);
} else {
if (bundle->getJunkPath()) {
- String8 storageName = String8(fileName).getPathLeaf();
+ String8 storageName = getPathLeaf(String8(fileName));
printf(" '%s' as '%s'...\n", fileName,
ResTable::normalizeForOutput(storageName.c_str()).c_str());
result = zip->add(fileName, storageName.c_str(),
@@ -2617,10 +2618,10 @@
return original;
}
- String8 ext(original.getPathExtension());
+ String8 ext(getPathExtension(original));
if (ext == String8(".apk")) {
return String8::format("%s_%s%s",
- original.getBasePath().c_str(),
+ getBasePath(original).c_str(),
split->getDirectorySafeName().c_str(),
ext.c_str());
}
@@ -2756,7 +2757,7 @@
// generate the dependency file in the R.java package subdirectory
// e.g. gen/com/foo/app/R.java.d
dependencyFile = String8(bundle->getRClassDir());
- dependencyFile.appendPath("R.java.d");
+ appendPath(dependencyFile, "R.java.d");
}
// Make sure we have a clean dependency file to start with
fp = fopen(dependencyFile, "w");
diff --git a/tools/aapt/CrunchCache.cpp b/tools/aapt/CrunchCache.cpp
index 1f2febe..e731ce0 100644
--- a/tools/aapt/CrunchCache.cpp
+++ b/tools/aapt/CrunchCache.cpp
@@ -5,6 +5,7 @@
// This file defines functions laid out and documented in
// CrunchCache.h
+#include <androidfw/PathUtils.h>
#include <utils/Compat.h>
#include <utils/Vector.h>
#include <utils/String8.h>
@@ -52,15 +53,15 @@
relativePath = String8(rPathPtr + offset);
if (forceOverwrite || needsUpdating(relativePath)) {
- cu->processImage(mSourcePath.appendPathCopy(relativePath),
- mDestPath.appendPathCopy(relativePath));
+ cu->processImage(appendPathCopy(mSourcePath, relativePath),
+ appendPathCopy(mDestPath, relativePath));
numFilesUpdated++;
// crunchFile(relativePath);
}
// Delete this file from the source files and (if it exists) from the
// dest files.
mSourceFiles.removeItemsAt(0);
- mDestFiles.removeItem(mDestPath.appendPathCopy(relativePath));
+ mDestFiles.removeItem(appendPathCopy(mDestPath, relativePath));
}
// Iterate through what's left of destFiles and delete leftovers
@@ -99,7 +100,7 @@
// Retrieve modification dates for this file entry under the source and
// cache directory trees. The vectors will return a modification date of 0
// if the file doesn't exist.
- time_t sourceDate = mSourceFiles.valueFor(mSourcePath.appendPathCopy(relativePath));
- time_t destDate = mDestFiles.valueFor(mDestPath.appendPathCopy(relativePath));
+ time_t sourceDate = mSourceFiles.valueFor(appendPathCopy(mSourcePath, relativePath));
+ time_t destDate = mDestFiles.valueFor(appendPathCopy(mDestPath, relativePath));
return sourceDate > destDate;
}
diff --git a/tools/aapt/DirectoryWalker.h b/tools/aapt/DirectoryWalker.h
index cea3a6e..7f60d4d 100644
--- a/tools/aapt/DirectoryWalker.h
+++ b/tools/aapt/DirectoryWalker.h
@@ -7,6 +7,7 @@
#ifndef DIRECTORYWALKER_H
#define DIRECTORYWALKER_H
+#include <androidfw/PathUtils.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/param.h>
@@ -77,7 +78,7 @@
mEntry = *entryPtr;
// Get stats
- String8 fullPath = mBasePath.appendPathCopy(mEntry.d_name);
+ String8 fullPath = appendPathCopy(mBasePath, mEntry.d_name);
stat(fullPath.c_str(),&mStats);
return &mEntry;
};
diff --git a/tools/aapt/FileFinder.cpp b/tools/aapt/FileFinder.cpp
index a5c19806..69a8fa9 100644
--- a/tools/aapt/FileFinder.cpp
+++ b/tools/aapt/FileFinder.cpp
@@ -5,6 +5,7 @@
// File Finder implementation.
// Implementation for the functions declared and documented in FileFinder.h
+#include <androidfw/PathUtils.h>
#include <utils/Vector.h>
#include <utils/String8.h>
#include <utils/KeyedVector.h>
@@ -57,7 +58,7 @@
if (entry->d_name[0] == '.') // Skip hidden files and directories
continue;
- String8 fullPath = basePath.appendPathCopy(entryName);
+ String8 fullPath = appendPathCopy(basePath, entryName);
// If this entry is a directory we'll recurse into it
if (isDirectory(fullPath.c_str()) ) {
DirectoryWalker* copy = dw->clone();
@@ -83,10 +84,10 @@
{
// Loop over the extensions, checking for a match
bool done = false;
- String8 ext(path.getPathExtension());
+ String8 ext(getPathExtension(path));
ext.toLower();
for (size_t i = 0; i < extensions.size() && !done; ++i) {
- String8 ext2 = extensions[i].getPathExtension();
+ String8 ext2 = getPathExtension(extensions[i]);
ext2.toLower();
// Compare the extensions. If a match is found, add to storage.
if (ext == ext2) {
diff --git a/tools/aapt/Images.cpp b/tools/aapt/Images.cpp
index c6c7e96..cd4de90 100644
--- a/tools/aapt/Images.cpp
+++ b/tools/aapt/Images.cpp
@@ -8,6 +8,7 @@
#include "Images.h"
+#include <androidfw/PathUtils.h>
#include <androidfw/ResourceTypes.h>
#include <utils/ByteOrder.h>
@@ -1357,7 +1358,7 @@
status_t preProcessImage(const Bundle* bundle, const sp<AaptAssets>& /* assets */,
const sp<AaptFile>& file, String8* /* outNewLeafName */)
{
- String8 ext(file->getPath().getPathExtension());
+ String8 ext(getPathExtension(file->getPath()));
// We currently only process PNG images.
if (strcmp(ext.c_str(), ".png") != 0) {
@@ -1518,7 +1519,7 @@
// Check to see if we're dealing with a 9-patch
// If we are, process appropriately
- if (source.getBasePath().getPathExtension() == ".9") {
+ if (getPathExtension(getBasePath(source)) == ".9") {
if (do_9patch(source.c_str(), &imageInfo) != NO_ERROR) {
return error;
}
@@ -1584,12 +1585,12 @@
status_t postProcessImage(const Bundle* bundle, const sp<AaptAssets>& assets,
ResourceTable* table, const sp<AaptFile>& file)
{
- String8 ext(file->getPath().getPathExtension());
+ String8 ext(getPathExtension(file->getPath()));
// At this point, now that we have all the resource data, all we need to
// do is compile XML files.
if (strcmp(ext.c_str(), ".xml") == 0) {
- String16 resourceName(parseResourceName(file->getSourceFile().getPathLeaf()));
+ String16 resourceName(parseResourceName(getPathLeaf(file->getSourceFile())));
return compileXmlFile(bundle, assets, resourceName, file, table);
}
diff --git a/tools/aapt/Package.cpp b/tools/aapt/Package.cpp
index a7ff5fa..5e0f87f 100644
--- a/tools/aapt/Package.cpp
+++ b/tools/aapt/Package.cpp
@@ -10,6 +10,7 @@
#include "ResourceFilter.h"
#include "Utils.h"
+#include <androidfw/PathUtils.h>
#include <androidfw/misc.h>
#include <utils/Log.h>
@@ -170,7 +171,7 @@
/* anything here? */
if (zip->getNumEntries() == 0) {
if (bundle->getVerbose()) {
- printf("Archive is empty -- removing %s\n", outputFile.getPathLeaf().c_str());
+ printf("Archive is empty -- removing %s\n", getPathLeaf(outputFile).c_str());
}
delete zip; // close the file so we can remove it in Win32
zip = NULL;
@@ -274,9 +275,9 @@
return true;
}
- if (strcasecmp(storageName.getPathExtension().c_str(), ".gz") == 0) {
+ if (strcasecmp(getPathExtension(storageName).c_str(), ".gz") == 0) {
fromGzip = true;
- storageName = storageName.getBasePath();
+ storageName = getBasePath(storageName);
}
if (bundle->getUpdate()) {
@@ -366,7 +367,7 @@
*/
bool okayToCompress(Bundle* bundle, const String8& pathName)
{
- String8 ext = pathName.getPathExtension();
+ String8 ext = getPathExtension(pathName);
int i;
if (ext.length() == 0)
diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp
index 4a360ed..647790b 100644
--- a/tools/aapt/Resource.cpp
+++ b/tools/aapt/Resource.cpp
@@ -19,6 +19,8 @@
#include "WorkQueue.h"
#include "XMLNode.h"
+#include <androidfw/PathUtils.h>
+
#include <algorithm>
// STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary.
@@ -143,8 +145,8 @@
mParams.inputFlags, mParams.navigation);
}
mPath = "res";
- mPath.appendPath(file->getGroupEntry().toDirName(mResType));
- mPath.appendPath(leaf);
+ appendPath(mPath, file->getGroupEntry().toDirName(mResType));
+ appendPath(mPath, leaf);
mBaseName = parseResourceName(leaf);
if (mBaseName == "") {
fprintf(stderr, "Error: malformed resource filename %s\n",
@@ -1686,7 +1688,7 @@
ResourceDirIterator it(fonts, String8("font"));
while ((err=it.next()) == NO_ERROR) {
// fonts can be resources other than xml.
- if (it.getFile()->getPath().getPathExtension() == ".xml") {
+ if (getPathExtension(it.getFile()->getPath()) == ".xml") {
String8 src = it.getFile()->getPrintableSource();
err = compileXmlFile(bundle, assets, String16(it.getBaseName()),
it.getFile(), &table, xmlFlags);
@@ -1716,7 +1718,7 @@
workItem.file, &table, xmlCompilationFlags);
if (err == NO_ERROR && workItem.file->hasData()) {
- assets->addResource(workItem.resPath.getPathLeaf(),
+ assets->addResource(getPathLeaf(workItem.resPath),
workItem.resPath,
workItem.file,
workItem.file->getResourceType());
@@ -2851,7 +2853,7 @@
s++;
if (s > last && (*s == '.' || *s == 0)) {
String8 part(last, s-last);
- dest.appendPath(part);
+ appendPath(dest, part);
#ifdef _WIN32
_mkdir(dest.c_str());
#else
@@ -2861,7 +2863,7 @@
}
} while (*s);
}
- dest.appendPath(className);
+ appendPath(dest, className);
dest.append(".java");
FILE* fp = fopen(dest.c_str(), "w+");
if (fp == NULL) {
@@ -2892,7 +2894,7 @@
if (textSymbolsDest != NULL && R == className) {
String8 textDest(textSymbolsDest);
- textDest.appendPath(className);
+ appendPath(textDest, className);
textDest.append(".txt");
FILE* fp = fopen(textDest.c_str(), "w+");
@@ -2918,7 +2920,7 @@
if (bundle->getGenDependencies() && R == className) {
// Add this R.java to the dependency file
String8 dependencyFile(bundle->getRClassDir());
- dependencyFile.appendPath("R.java.d");
+ appendPath(dependencyFile, "R.java.d");
FILE *fp = fopen(dependencyFile.c_str(), "a");
fprintf(fp,"%s \\\n", dest.c_str());
diff --git a/tools/aapt/ResourceTable.cpp b/tools/aapt/ResourceTable.cpp
index bccf73e..421cae7 100644
--- a/tools/aapt/ResourceTable.cpp
+++ b/tools/aapt/ResourceTable.cpp
@@ -14,6 +14,7 @@
#include "Utils.h"
#include <algorithm>
+#include <androidfw/PathUtils.h>
#include <androidfw/ResourceTypes.h>
#include <utils/ByteOrder.h>
#include <utils/TypeHelpers.h>
@@ -83,7 +84,7 @@
sp<AaptDir> resDir = assets->getDirs().valueFor(String8("res"));
sp<AaptDir> dir = resDir->getDirs().valueFor(target->getGroupEntry().toDirName(
target->getResourceType()));
- dir->removeFile(target->getPath().getPathLeaf());
+ dir->removeFile(getPathLeaf(target->getPath()));
return NO_ERROR;
}
diff --git a/tools/aapt/Utils.cpp b/tools/aapt/Utils.cpp
index 36b018e..946916a 100644
--- a/tools/aapt/Utils.cpp
+++ b/tools/aapt/Utils.cpp
@@ -36,3 +36,26 @@
}
#endif
}
+
+String8 walkPath(const String8& path, String8* outRemains) {
+ const char* cp;
+ const char* const str = path.c_str();
+ const char* buf = str;
+
+ cp = strchr(buf, OS_PATH_SEPARATOR);
+ if (cp == buf) {
+ // don't include a leading '/'.
+ buf = buf + 1;
+ cp = strchr(buf, OS_PATH_SEPARATOR);
+ }
+
+ if (cp == nullptr) {
+ String8 res = buf != str ? String8(buf) : path;
+ if (outRemains) *outRemains = String8();
+ return res;
+ }
+
+ String8 res(buf, cp - buf);
+ if (outRemains) *outRemains = String8(cp + 1);
+ return res;
+}
diff --git a/tools/aapt/Utils.h b/tools/aapt/Utils.h
index 8eb5941..f0d6979 100644
--- a/tools/aapt/Utils.h
+++ b/tools/aapt/Utils.h
@@ -26,3 +26,13 @@
// If the default OS separator is backslash, this converts all
// backslashes to slashes, in-place. Otherwise it does nothing.
void convertToResPath(android::String8&);
+
+/**
+ * Retrieve the front (root dir) component. Optionally also return the
+ * remaining components.
+ *
+ * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
+ * "/tmp" --> "tmp" (remain = "")
+ * "bar.c" --> "bar.c" (remain = "")
+ */
+android::String8 walkPath(const android::String8& path, android::String8* outRemains = nullptr);