summaryrefslogtreecommitdiffstats
path: root/tools/atree/fs.cpp
diff options
context:
space:
mode:
authorRaphael <raphael@google.com>2011-09-14 15:07:05 -0700
committerRaphael <raphael@google.com>2011-09-15 11:22:16 -0700
commit0b3ec5d32f15bdea67d15af95cf68e455867c668 (patch)
tree424f7ad6c56edc405aadab264f55c7de3a43d417 /tools/atree/fs.cpp
parent4d7ddab160ba29247225e11c2092dcfd68bd6baf (diff)
downloadbuild-0b3ec5d32f15bdea67d15af95cf68e455867c668.zip
build-0b3ec5d32f15bdea67d15af95cf68e455867c668.tar.gz
build-0b3ec5d32f15bdea67d15af95cf68e455867c668.tar.bz2
Add rm and strip abilities to atree.
The new line syntax is: [SRC] [rm|strip] DEST This allows one to write things like this in atree: bin/src bin/src bin/dest bin/src "bin/another file name" rm dest/file rm dest/dir # recursive strip bin/src bin/src strip bin/dest Src and dest can contain spaces if full enclosed in double-quotes. The strip command can be overridden using the STRIP env var. Change-Id: I22aae7a87c36c082e1aab87132099a3c644914da
Diffstat (limited to 'tools/atree/fs.cpp')
-rw-r--r--tools/atree/fs.cpp34
1 files changed, 30 insertions, 4 deletions
diff --git a/tools/atree/fs.cpp b/tools/atree/fs.cpp
index 9971879..b648394 100644
--- a/tools/atree/fs.cpp
+++ b/tools/atree/fs.cpp
@@ -1,7 +1,9 @@
#include "fs.h"
#include "files.h"
#include <unistd.h>
+#include <stdlib.h>
#include <sys/types.h>
+#include <sys/wait.h>
#include <dirent.h>
#include <string>
#include <vector>
@@ -64,10 +66,10 @@ remove_recursively(const string& path)
#ifdef HAVE_DIRENT_D_TYPE
bool is_directory = (ent->d_type == DT_DIR);
#else
- // If dirent.d_type is missing, then use stat instead
- struct stat stat_buf;
- stat(full.c_str(), &stat_buf);
- bool is_directory = S_ISDIR(stat_buf.st_mode);
+ // If dirent.d_type is missing, then use stat instead
+ struct stat stat_buf;
+ stat(full.c_str(), &stat_buf);
+ bool is_directory = S_ISDIR(stat_buf.st_mode);
#endif
if (is_directory) {
dirs.push_back(full);
@@ -146,3 +148,27 @@ copy_file(const string& src, const string& dst)
COPY_NO_DEREFERENCE | COPY_FORCE | COPY_PERMISSIONS);
return err;
}
+
+int
+strip_file(const string& path)
+{
+ // Default strip command to run is "strip" unless overridden by the STRIP env var.
+ const char* strip_cmd = getenv("STRIP");
+ if (!strip_cmd || !strip_cmd[0]) {
+ strip_cmd = "strip";
+ }
+ pid_t pid = fork();
+ if (pid == -1) {
+ // Fork failed. errno should be set.
+ return -1;
+ } else if (pid == 0) {
+ // Exec in the child. Only returns if execve failed.
+ return execlp(strip_cmd, strip_cmd, path.c_str(), (char *)NULL);
+ } else {
+ // Wait for child pid and return its exit code.
+ int status;
+ waitpid(pid, &status, 0);
+ return status;
+ }
+}
+