From d6d8faa69068d92e0e4021fced181b448d4e8cbd Mon Sep 17 00:00:00 2001 From: Jorge Lucangeli Obes Date: Tue, 19 Jul 2016 12:10:26 -0400 Subject: [PATCH] sdcard: Use std::map. Having CaseInsensitiveCompare use strcasecomp is not ideal, but other solutions are not prettier. Also, add a TODO to fix FUSE_TRACE, broken by the switch to C++. Bug: 27147273 Change-Id: I0017c3a7d0254eb81abd128b97cd06c5ad0d1dff --- sdcard/fuse.cpp | 5 +++-- sdcard/fuse.h | 17 +++++++++++++++-- sdcard/sdcard.cpp | 32 ++++++-------------------------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/sdcard/fuse.cpp b/sdcard/fuse.cpp index 1b6a5f19b..9b1b19065 100644 --- a/sdcard/fuse.cpp +++ b/sdcard/fuse.cpp @@ -275,8 +275,9 @@ static void derive_permissions_locked(struct fuse* fuse, struct node *parent, case PERM_ANDROID_DATA: case PERM_ANDROID_OBB: case PERM_ANDROID_MEDIA: - appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name); - if (appid != 0) { + const auto& iter = fuse->global->package_to_appid->find(node->name); + if (iter != fuse->global->package_to_appid->end()) { + appid = iter->second; node->uid = multiuser_get_uid(parent->userid, appid); } break; diff --git a/sdcard/fuse.h b/sdcard/fuse.h index d76c66422..634fbf191 100644 --- a/sdcard/fuse.h +++ b/sdcard/fuse.h @@ -30,14 +30,17 @@ #include #include +#include +#include + #include -#include #include #include #include #include +// TODO(b/30222003): Fix compilation with FUSE_TRACE == 1. #define FUSE_TRACE 0 #if FUSE_TRACE @@ -59,6 +62,16 @@ * the largest possible data payload. */ #define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE) +namespace { +struct CaseInsensitiveCompare { + bool operator()(const std::string& lhs, const std::string& rhs) const { + return strcasecmp(lhs.c_str(), rhs.c_str()) < 0; + } +}; +} + +using AppIdMap = std::map; + /* Permission mode for a specific node. Controls how file permissions * are derived for children nodes. */ typedef enum { @@ -135,7 +148,7 @@ struct fuse_global { char source_path[PATH_MAX]; char obb_path[PATH_MAX]; - Hashmap* package_to_appid; + AppIdMap* package_to_appid; __u64 next_generation; struct node root; diff --git a/sdcard/sdcard.cpp b/sdcard/sdcard.cpp index ba636366f..3d7bdc902 100644 --- a/sdcard/sdcard.cpp +++ b/sdcard/sdcard.cpp @@ -32,7 +32,6 @@ #include #include -#include #include #include #include @@ -78,41 +77,22 @@ /* Supplementary groups to execute with. */ static const gid_t kGroups[1] = { AID_PACKAGE_INFO }; -static int str_hash(void *key) { - return hashmapHash(key, strlen(static_cast(key))); -} - -/* Tests if two string keys are equal ignoring case. */ -static bool str_icase_equals(void *keyA, void *keyB) { - return strcasecmp(static_cast(keyA), static_cast(keyB)) == 0; -} - -static bool remove_str_to_int(void *key, void *value, void *context) { - Hashmap* map = static_cast(context); - hashmapRemove(map, key); - free(key); - return true; -} - static bool package_parse_callback(pkg_info *info, void *userdata) { struct fuse_global *global = (struct fuse_global *)userdata; - - char* name = strdup(info->name); - hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid); + bool res = global->package_to_appid->emplace(info->name, info->uid).second; packagelist_free(info); - return true; + return res; } static bool read_package_list(struct fuse_global* global) { pthread_mutex_lock(&global->lock); - hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid); - + global->package_to_appid->clear(); bool rc = packagelist_parse(package_parse_callback, global); TRACE("read_package_list: found %zu packages\n", - hashmapSize(global->package_to_appid)); + global->package_to_appid->size()); - /* Regenerate ownership details using newly loaded mapping */ + // Regenerate ownership details using newly loaded mapping. derive_permissions_recursive_locked(global->fuse_default, &global->root); pthread_mutex_unlock(&global->lock); @@ -245,7 +225,7 @@ static void run(const char* source_path, const char* label, uid_t uid, memset(&handler_write, 0, sizeof(handler_write)); pthread_mutex_init(&global.lock, NULL); - global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals); + global.package_to_appid = new AppIdMap; global.uid = uid; global.gid = gid; global.multi_user = multi_user;