Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
sdcard: use libpackageparser
Browse files Browse the repository at this point in the history
Switch from the internal packages.list file parser
implementation to a common parser library.

See Change-Id: I87a406802f95d8e7bfd8ee85f723f80e9e6b6c0c
for all of the details.

Change-Id: I98924dce406b322e0d402bca7fdac51f6a1e6a4b
Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Oct 22, 2015
1 parent c52b3c0 commit e509980
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
3 changes: 1 addition & 2 deletions sdcard/Android.mk
Expand Up @@ -5,7 +5,6 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := sdcard.c
LOCAL_MODULE := sdcard
LOCAL_CFLAGS := -Wall -Wno-unused-parameter -Werror

LOCAL_SHARED_LIBRARIES := libcutils
LOCAL_SHARED_LIBRARIES := libcutils libpackagelistparser

include $(BUILD_EXECUTABLE)
50 changes: 21 additions & 29 deletions sdcard/sdcard.c
Expand Up @@ -24,6 +24,7 @@
#include <limits.h>
#include <linux/fuse.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -34,13 +35,15 @@
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

#include <cutils/fs.h>
#include <cutils/hashmap.h>
#include <cutils/log.h>
#include <cutils/multiuser.h>
#include <packagelistparser/packagelistparser.h>

#include <private/android_filesystem_config.h>

Expand Down Expand Up @@ -103,9 +106,6 @@
* or that a reply has already been written. */
#define NO_STATUS 1

/* Path to system-provided mapping of package name to appIds */
static const char* const kPackagesListFile = "/data/system/packages.list";

/* Supplementary groups to execute with */
static const gid_t kGroups[1] = { AID_PACKAGE_INFO };

Expand Down Expand Up @@ -1636,35 +1636,27 @@ static bool remove_str_to_int(void *key, void *value, void *context) {
return true;
}

static int 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);
static bool package_parse_callback(pkg_info *info, void *userdata) {
struct fuse_global *global = (struct fuse_global *)userdata;

FILE* file = fopen(kPackagesListFile, "r");
if (!file) {
ERROR("failed to open package list: %s\n", strerror(errno));
pthread_mutex_unlock(&global->lock);
return -1;
}
char* name = strdup(info->name);
hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid);
packagelist_free(info);
return true;
}

char buf[512];
while (fgets(buf, sizeof(buf), file) != NULL) {
char package_name[512];
int appid;
char gids[512];
static bool read_package_list(struct fuse_global* global) {
pthread_mutex_lock(&global->lock);

if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
char* package_name_dup = strdup(package_name);
hashmapPut(global->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
}
}
hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);

bool rc = packagelist_parse(package_parse_callback, global);
TRACE("read_package_list: found %zu packages\n",
hashmapSize(global->package_to_appid));
fclose(file);

pthread_mutex_unlock(&global->lock);
return 0;

return rc;
}

static void watch_package_list(struct fuse_global* global) {
Expand All @@ -1680,11 +1672,11 @@ static void watch_package_list(struct fuse_global* global) {
bool active = false;
while (1) {
if (!active) {
int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
if (res == -1) {
if (errno == ENOENT || errno == EACCES) {
/* Framework may not have created yet, sleep and retry */
ERROR("missing packages.list; retrying\n");
ERROR("missing \"%s\"; retrying\n", PACKAGES_LIST_FILE);
sleep(3);
continue;
} else {
Expand All @@ -1695,8 +1687,8 @@ static void watch_package_list(struct fuse_global* global) {

/* Watch above will tell us about any future changes, so
* read the current state. */
if (read_package_list(global) == -1) {
ERROR("read_package_list failed: %s\n", strerror(errno));
if (read_package_list(global) == false) {
ERROR("read_package_list failed\n");
return;
}
active = true;
Expand Down

0 comments on commit e509980

Please sign in to comment.