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

Commit 0af7ee4a authored by Wei Wang's avatar Wei Wang Committed by Android (Google) Code Review
Browse files

Merge changes from topic 'cpuset_setall' into oc-dev

* changes:
  init: use read_file and write_file to implement do_copy builtin
  init: Use std::string for write_file()
parents ac0aa5f3 67f6a530
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -293,6 +293,11 @@ Commands
`copy <src> <dst>`
> Copies a file. Similar to write, but useful for binary/large
  amounts of data.
  Regarding to the src file, copying from symbolic link file and world-writable
  or group-writable files are not allowed.
  Regarding to the dst file, the default mode created is 0600 if it does not
  exist. And it will be truncated if dst file is a normal regular file and
  already exists.

`domainname <name>`
> Set the domain name.
+8 −50
Original line number Diff line number Diff line
@@ -155,7 +155,7 @@ static int do_class_restart(const std::vector<std::string>& args) {
}

static int do_domainname(const std::vector<std::string>& args) {
    return write_file("/proc/sys/kernel/domainname", args[1].c_str()) ? 0 : 1;
    return write_file("/proc/sys/kernel/domainname", args[1]) ? 0 : 1;
}

static int do_enable(const std::vector<std::string>& args) {
@@ -179,7 +179,7 @@ static int do_export(const std::vector<std::string>& args) {
}

static int do_hostname(const std::vector<std::string>& args) {
    return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1;
    return write_file("/proc/sys/kernel/hostname", args[1]) ? 0 : 1;
}

static int do_ifup(const std::vector<std::string>& args) {
@@ -648,57 +648,15 @@ static int do_verity_update_state(const std::vector<std::string>& args) {
}

static int do_write(const std::vector<std::string>& args) {
    const char* path = args[1].c_str();
    const char* value = args[2].c_str();
    return write_file(path, value) ? 0 : 1;
    return write_file(args[1], args[2]) ? 0 : 1;
}

static int do_copy(const std::vector<std::string>& args) {
    char* buffer = NULL;
    int rc = 0;
    int fd1 = -1, fd2 = -1;
    struct stat info;
    int brtw, brtr;
    char* p;

    if (stat(args[1].c_str(), &info) < 0) return -1;

    if ((fd1 = open(args[1].c_str(), O_RDONLY | O_CLOEXEC)) < 0) goto out_err;

    if ((fd2 = open(args[2].c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0660)) < 0)
        goto out_err;

    if (!(buffer = (char*)malloc(info.st_size))) goto out_err;

    p = buffer;
    brtr = info.st_size;
    while (brtr) {
        rc = read(fd1, p, brtr);
        if (rc < 0) goto out_err;
        if (rc == 0) break;
        p += rc;
        brtr -= rc;
    }

    p = buffer;
    brtw = info.st_size;
    while (brtw) {
        rc = write(fd2, p, brtw);
        if (rc < 0) goto out_err;
        if (rc == 0) break;
        p += rc;
        brtw -= rc;
    }

    rc = 0;
    goto out;
out_err:
    rc = -1;
out:
    if (buffer) free(buffer);
    if (fd1 >= 0) close(fd1);
    if (fd2 >= 0) close(fd2);
    return rc;
    std::string data;
    if (read_file(args[1], &data)) {
        return write_file(args[2], data) ? 0 : 1;
    }
    return 1;
}

static int do_chown(const std::vector<std::string>& args) {
+1 −1
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ bool Parser::ParseConfigFile(const std::string& path) {
    LOG(INFO) << "Parsing file " << path << "...";
    Timer t;
    std::string data;
    if (!read_file(path.c_str(), &data)) {
    if (!read_file(path, &data)) {
        return false;
    }

+6 −5
Original line number Diff line number Diff line
@@ -161,10 +161,11 @@ out_unlink:
    return -1;
}

bool read_file(const char* path, std::string* content) {
bool read_file(const std::string& path, std::string* content) {
    content->clear();

    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
    android::base::unique_fd fd(
        TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
    if (fd == -1) {
        return false;
    }
@@ -184,9 +185,9 @@ bool read_file(const char* path, std::string* content) {
    return android::base::ReadFdToString(fd, content);
}

bool write_file(const char* path, const char* content) {
    android::base::unique_fd fd(
        TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600)));
bool write_file(const std::string& path, const std::string& content) {
    android::base::unique_fd fd(TEMP_FAILURE_RETRY(
        open(path.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
    if (fd == -1) {
        PLOG(ERROR) << "write_file: Unable to open '" << path << "'";
        return false;
+2 −2
Original line number Diff line number Diff line
@@ -32,8 +32,8 @@ using namespace std::chrono_literals;
int create_socket(const char *name, int type, mode_t perm,
                  uid_t uid, gid_t gid, const char *socketcon);

bool read_file(const char* path, std::string* content);
bool write_file(const char* path, const char* content);
bool read_file(const std::string& path, std::string* content);
bool write_file(const std::string& path, const std::string& content);

// A std::chrono clock based on CLOCK_BOOTTIME.
class boot_clock {
Loading