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

Commit 5a8db38b authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Move adb_dirname and adb_basename to libbase"

parents 1e48879b 58021d15
Loading
Loading
Loading
Loading
+2 −49
Original line number Diff line number Diff line
@@ -19,16 +19,15 @@
#include "adb_utils.h"
#include "adb_unique_fd.h"

#include <libgen.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <algorithm>
#include <mutex>
#include <vector>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/stringprintf.h>
@@ -100,52 +99,6 @@ std::string escape_arg(const std::string& s) {
  return result;
}

std::string adb_basename(const std::string& path) {
  static std::mutex& basename_lock = *new std::mutex();

  // Copy path because basename may modify the string passed in.
  std::string result(path);

  // Use lock because basename() may write to a process global and return a
  // pointer to that. Note that this locking strategy only works if all other
  // callers to basename in the process also grab this same lock.
  std::lock_guard<std::mutex> lock(basename_lock);

  // Note that if std::string uses copy-on-write strings, &str[0] will cause
  // the copy to be made, so there is no chance of us accidentally writing to
  // the storage for 'path'.
  char* name = basename(&result[0]);

  // In case dirname returned a pointer to a process global, copy that string
  // before leaving the lock.
  result.assign(name);

  return result;
}

std::string adb_dirname(const std::string& path) {
  static std::mutex& dirname_lock = *new std::mutex();

  // Copy path because dirname may modify the string passed in.
  std::string result(path);

  // Use lock because dirname() may write to a process global and return a
  // pointer to that. Note that this locking strategy only works if all other
  // callers to dirname in the process also grab this same lock.
  std::lock_guard<std::mutex> lock(dirname_lock);

  // Note that if std::string uses copy-on-write strings, &str[0] will cause
  // the copy to be made, so there is no chance of us accidentally writing to
  // the storage for 'path'.
  char* parent = dirname(&result[0]);

  // In case dirname returned a pointer to a process global, copy that string
  // before leaving the lock.
  result.assign(parent);

  return result;
}

// Given a relative or absolute filepath, create the directory hierarchy
// as needed. Returns true if the hierarchy is/was setup.
bool mkdirs(const std::string& path) {
@@ -171,7 +124,7 @@ bool mkdirs(const std::string& path) {
    return true;
  }

  const std::string parent(adb_dirname(path));
  const std::string parent(android::base::Dirname(path));

  // If dirname returned the same path as what we passed in, don't go recursive.
  // This can happen on Windows when walking up the directory hierarchy and not
+0 −5
Original line number Diff line number Diff line
@@ -28,11 +28,6 @@ void close_stdin();
bool getcwd(std::string* cwd);
bool directory_exists(const std::string& path);

// Like the regular basename and dirname, but thread-safe on all
// platforms and capable of correctly handling exotic Windows paths.
std::string adb_basename(const std::string& path);
std::string adb_dirname(const std::string& path);

// Return the user's home directory.
std::string adb_get_homedir_path();

+0 −12
Original line number Diff line number Diff line
@@ -109,18 +109,6 @@ TEST(adb_utils, escape_arg) {
  ASSERT_EQ(R"('abc)')", escape_arg("abc)"));
}

TEST(adb_utils, adb_basename) {
  EXPECT_EQ("sh", adb_basename("/system/bin/sh"));
  EXPECT_EQ("sh", adb_basename("sh"));
  EXPECT_EQ("sh", adb_basename("/system/bin/sh/"));
}

TEST(adb_utils, adb_dirname) {
  EXPECT_EQ("/system/bin", adb_dirname("/system/bin/sh"));
  EXPECT_EQ(".", adb_dirname("sh"));
  EXPECT_EQ("/system/bin", adb_dirname("/system/bin/sh/"));
}

void test_mkdirs(const std::string& basepath) {
  // Test creating a directory hierarchy.
  ASSERT_TRUE(mkdirs(basepath));
+3 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <string>
#include <vector>

#include <android-base/file.h>
#include <android-base/strings.h>

#include "sysdeps.h"
@@ -113,14 +114,14 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface

  private:
    void SetLineMessage(const std::string& action) {
        line_message_ = action + " " + adb_basename(dest_file_);
        line_message_ = action + " " + android::base::Basename(dest_file_);
    }

    void SetSrcFile(const std::string path) {
        src_file_ = path;
        if (!dest_dir_.empty()) {
            // Only uses device-provided name when user passed a directory.
            dest_file_ = adb_basename(path);
            dest_file_ = android::base::Basename(path);
            SetLineMessage("generating");
        }
    }
+3 −2
Original line number Diff line number Diff line
@@ -2115,7 +2115,8 @@ static int install_multiple_app(TransportType transport, const char* serial, int

        std::string cmd = android::base::StringPrintf(
                "%s install-write -S %" PRIu64 " %d %d_%s -",
                install_cmd.c_str(), static_cast<uint64_t>(sb.st_size), session_id, i, adb_basename(file).c_str());
                install_cmd.c_str(), static_cast<uint64_t>(sb.st_size), session_id, i,
                android::base::Basename(file).c_str());

        int localFd = adb_open(file, O_RDONLY);
        if (localFd < 0) {
@@ -2233,7 +2234,7 @@ static int install_app_legacy(TransportType transport, const char* serial, int a
    int result = -1;
    std::vector<const char*> apk_file = {argv[last_apk]};
    std::string apk_dest = android::base::StringPrintf(
        where, adb_basename(argv[last_apk]).c_str());
        where, android::base::Basename(argv[last_apk]).c_str());
    if (!do_sync_push(apk_file, apk_dest.c_str())) goto cleanup_apk;
    argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
    result = pm_command(transport, serial, argc, argv);
Loading