Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e0b7502c authored by Josh Gao's avatar Josh Gao
Browse files

adb: add helper to get the ~/.android directory.

Extract the logic for creating ~/.android out of get_user_key_path into
its own function. Also, fall back to getpwuid_r when $HOME isn't
defined.

Change-Id: I676a7c750cb364f89b544818ffda07903d14fb97
Test: ran adb with ~/.android missing
parent 43824e72
Loading
Loading
Loading
Loading
+1 −15
Original line number Diff line number Diff line
@@ -246,21 +246,7 @@ static bool read_keys(const std::string& path, bool allow_dir = true) {
}

static std::string get_user_key_path() {
    const std::string home = adb_get_homedir_path(true);
    LOG(DEBUG) << "adb_get_homedir_path returned '" << home << "'";

    const std::string android_dir = android::base::StringPrintf("%s%c.android", home.c_str(),
                                                                OS_PATH_SEPARATOR);

    struct stat buf;
    if (stat(android_dir.c_str(), &buf) == -1) {
        if (adb_mkdir(android_dir.c_str(), 0750) == -1) {
            PLOG(ERROR) << "Cannot mkdir '" << android_dir << "'";
            return "";
        }
    }

    return android_dir + OS_PATH_SEPARATOR + "adbkey";
    return adb_get_android_dir_path() + OS_PATH_SEPARATOR + "adbkey";
}

static bool get_user_key() {
+27 −7
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <unistd.h>

#include <algorithm>
#include <vector>

#include <android-base/logging.h>
#include <android-base/parseint.h>
@@ -41,6 +42,8 @@
#  endif
#  include "windows.h"
#  include "shlobj.h"
#else
#include <pwd.h>
#endif

ADB_MUTEX_DEFINE(basename_lock);
@@ -263,14 +266,8 @@ bool forward_targets_are_valid(const std::string& source, const std::string& des
    return true;
}

std::string adb_get_homedir_path(bool check_env_first) {
std::string adb_get_homedir_path() {
#ifdef _WIN32
    if (check_env_first) {
        if (const char* const home = getenv("ANDROID_SDK_HOME")) {
            return home;
        }
    }

    WCHAR path[MAX_PATH];
    const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
    if (FAILED(hr)) {
@@ -286,6 +283,29 @@ std::string adb_get_homedir_path(bool check_env_first) {
    if (const char* const home = getenv("HOME")) {
        return home;
    }

    struct passwd pwent;
    struct passwd* result;
    int pwent_max = sysconf(_SC_GETPW_R_SIZE_MAX);
    std::vector<char> buf(pwent_max);
    int rc = getpwuid_r(getuid(), &pwent, buf.data(), buf.size(), &result);
    if (rc == 0 && result) {
        return result->pw_dir;
    }

    LOG(FATAL) << "failed to get user home directory";
    return {};
#endif
}

std::string adb_get_android_dir_path() {
    std::string user_dir = adb_get_homedir_path();
    std::string android_dir = user_dir + OS_PATH_SEPARATOR + ".android";
    struct stat buf;
    if (stat(android_dir.c_str(), &buf) == -1) {
        if (adb_mkdir(android_dir.c_str(), 0750) == -1) {
            PLOG(FATAL) << "Cannot mkdir '" << android_dir << "'";
        }
    }
    return android_dir;
}
+4 −4
Original line number Diff line number Diff line
@@ -33,10 +33,10 @@ std::string adb_basename(const std::string& path);
std::string adb_dirname(const std::string& path);

// Return the user's home directory.
// |check_env_first| - if true, on Windows check the ANDROID_SDK_HOME
// environment variable before trying the WinAPI call (useful when looking for
// the .android directory)
std::string adb_get_homedir_path(bool check_env_first);
std::string adb_get_homedir_path();

// Return the adb user directory.
std::string adb_get_android_dir_path();

bool mkdirs(const std::string& path);

+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@
static std::string adb_construct_auth_command() {
    static const char auth_token_filename[] = ".emulator_console_auth_token";

    std::string auth_token_path = adb_get_homedir_path(false);
    std::string auth_token_path = adb_get_homedir_path();
    auth_token_path += OS_PATH_SEPARATOR;
    auth_token_path += auth_token_filename;