Skip to content

Commit

Permalink
handle target root in system-agent (.target)
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Oct 23, 2013
1 parent d567951 commit d7f8de6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
51 changes: 41 additions & 10 deletions agent-system/src/SystemAgent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ SystemAgent::Read (const YCPPath& path, const YCPValue& arg, const YCPValue&)
return YCPNull ();
}

// prepend the current root
set_absolute_filename(filename);

if (cmd == "string")
{
/**
Expand Down Expand Up @@ -648,6 +651,18 @@ SystemAgent::Read (const YCPPath& path, const YCPValue& arg, const YCPValue&)
return YCPError (string("Undefined subpath for Read (") + path->toString() + ")");
}

/**
* prepend the current root to the file path
*/
void SystemAgent::set_absolute_filename(string &file)
{
if (root() != NULL && strcmp(root(), "/") != 0)
{
y2milestone("Prepending chroot '%s' to '%s' path", root(), file.c_str());
// prepend the current root + "/" separator
file.insert(0, string(root()).append("/"));
}
}

/**
* Write function
Expand Down Expand Up @@ -697,12 +712,9 @@ SystemAgent::Write (const YCPPath& path, const YCPValue& value,
path->component_str (1).c_str () + ":" + passwd +
"' |/usr/sbin/chpasswd -e >/dev/null 2>&1";

// Don't write the password into the log - even though it's crypted
// y2debug("Executing: '%s'", bashcommand.c_str());
int exitcode = shellcommand(root(), bashcommand);

int exitcode = system(bashcommand.c_str());

return YCPBoolean (WIFEXITED (exitcode) && WEXITSTATUS (exitcode) == 0);
return YCPBoolean(exitcode == 0);
}

else if (cmd == "string")
Expand Down Expand Up @@ -752,6 +764,9 @@ SystemAgent::Write (const YCPPath& path, const YCPValue& value,
return YCPBoolean (false);
}

// prepend the current root
set_absolute_filename(filename);

int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, filemode);
if (fd >= 0)
{
Expand Down Expand Up @@ -786,6 +801,9 @@ SystemAgent::Write (const YCPPath& path, const YCPValue& value,
}

string filename = value->asString ()->value ();
// prepend the current root
set_absolute_filename(filename);

YCPByteblock byteblock = arg->asByteblock ();

int fd = open (filename.c_str (), O_WRONLY | O_CREAT | O_TRUNC, 0644);
Expand Down Expand Up @@ -857,6 +875,9 @@ SystemAgent::Write (const YCPPath& path, const YCPValue& value,
return YCPBoolean (false);
}

// prepend the current root
set_absolute_filename(filename);

// Create directory, if missing
size_t pos = 0;
while (pos = filename.find('/', pos + 1), pos != string::npos)
Expand Down Expand Up @@ -1082,13 +1103,17 @@ SystemAgent::Execute (const YCPPath& path, const YCPValue& value,
{
return YCPError ("Bad arguments to Execute (.symlink, string old, string new)");
}
const char *oldpath = value->asString()->value_cstr();
const string newpath = targetPath(arg->asString()->value());
string oldpath = value->asString()->value();
string newpath = targetPath(arg->asString()->value());

// prepend the current root
set_absolute_filename(oldpath);
set_absolute_filename(newpath);

y2milestone ("symlink %s -> %s", oldpath, newpath.c_str());
y2milestone ("symlink %s -> %s", oldpath.c_str(), newpath.c_str());

remove (newpath.c_str());
return YCPBoolean (symlink (oldpath, newpath.c_str()) == 0);
return YCPBoolean (symlink (oldpath.c_str(), newpath.c_str()) == 0);

} // .symlink

Expand Down Expand Up @@ -1132,6 +1157,8 @@ SystemAgent::Execute (const YCPPath& path, const YCPValue& value,
size_t pos = 0;

y2milestone ("mkdir %s", path.c_str());
// prepend the current root
set_absolute_filename(path);

// Create leading components
while (pos = path.find('/', pos + 1), pos != string::npos)
Expand Down Expand Up @@ -1313,7 +1340,11 @@ SystemAgent::Execute (const YCPPath& path, const YCPValue& value,
return YCPError ("Bad file in Execute (.remove, string file)");
}

int ret = unlink (targetPath(value->asString ()->value()).c_str());
string file = targetPath(value->asString ()->value());
// prepend the current root
set_absolute_filename(file);

int ret = unlink(file.c_str());

return YCPBoolean (ret == 0);
}
Expand Down
3 changes: 3 additions & 0 deletions agent-system/src/SystemAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class SystemAgent : public SCRAgent

string tempdir;

// helper method for prepending the current root
void set_absolute_filename(string &file);

};


Expand Down

0 comments on commit d7f8de6

Please sign in to comment.