]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph: clean up command output
authorSage Weil <sage.weil@dreamhost.com>
Thu, 8 Sep 2011 04:07:54 +0000 (21:07 -0700)
committerSage Weil <sage.weil@dreamhost.com>
Thu, 8 Sep 2011 04:07:54 +0000 (21:07 -0700)
Open + truncate output file _once_, and concatenate output of each command
to that fd.

Signed-off-by: Sage Weil <sage.weil@dreamhost.com>
src/tools/ceph.cc
src/tools/common.cc
src/tools/common.h

index 9ee7d9cf131b7e208be6e3ea4f7d8d7fff9b94bf..5e6037b1570b2b9242d37d110c1615fb028a0d34 100644 (file)
@@ -125,8 +125,8 @@ int main(int argc, const char **argv)
   global_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0);
   common_init_finish(g_ceph_context);
 
+  // input
   bufferlist indata;
-
   if (!in_file.empty()) {
     if (get_indata(in_file.c_str(), indata)) {
       derr << "failed to get data from '" << in_file << "'" << dendl;
@@ -134,6 +134,23 @@ int main(int argc, const char **argv)
     }
   }
 
+  // output
+  int out_fd;
+  if (out_file.empty()) {
+    out_fd = ::open("/dev/null", O_WRONLY);
+    out_file = "/dev/null";
+  } else if (out_file == "-") {
+    out_fd = dup(0);
+  } else {
+    out_fd = TEMP_FAILURE_RETRY(::open(out_file.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644));
+    if (out_fd < 0) {
+      int ret = errno;
+      derr << " failed to create file '" << out_file << "': "
+          << cpp_strerror(ret) << dendl;
+      return 1;
+    }
+  }
+
   CephToolCtx *ctx = ceph_tool_common_init(mode, concise);
   if (!ctx) {
     derr << "ceph_tool_common_init failed." << dendl;
@@ -156,8 +173,7 @@ int main(int argc, const char **argv)
       if (args.empty()) {
        if (ceph_tool_do_cli(ctx))
          ret = 1;
-      }
-      else {
+      } else {
        while (!args.empty()) {
          vector<string> cmd;
          for (vector<const char*>::iterator n = args.begin();
@@ -169,9 +185,16 @@ int main(int argc, const char **argv)
            cmd.push_back(np);
          }
 
-         if (ceph_tool_cli_input(ctx, cmd,
-               out_file.empty() ? NULL : out_file.c_str(), indata))
+         bufferlist obl;
+         if (ceph_tool_do_command(ctx, cmd, indata, obl))
            ret = 1;
+         int err = obl.write_fd(out_fd);
+         if (err) {
+           derr << " failed to write " << obl.length() << " bytes to " << out_file << ": "
+                << cpp_strerror(err) << dendl;
+           goto out;
+         }
+         derr << " wrote " << obl.length() << " byte payload to " << out_file << dendl;
        }
       }
       if (ceph_tool_messenger_shutdown())
@@ -186,6 +209,8 @@ int main(int argc, const char **argv)
     }
   }
 
+ out:
+  ::close(out_fd);
   if (ceph_tool_common_shutdown(ctx))
     ret = 1;
   return ret;
index 32aede39059101ac599eac2fa36ad9a8e7f7d51a..6b94eece3b864104e3bb6882ad219aecaaaf8178 100644 (file)
@@ -490,55 +490,11 @@ int run_command(CephToolCtx *ctx, const char *line)
   return 0;
 }
 
-int ceph_tool_cli_input(CephToolCtx *ctx, std::vector<std::string> &cmd, 
-                       const char *outfile, bufferlist &indata)
+int ceph_tool_do_command(CephToolCtx *ctx, std::vector<std::string>& cmd, 
+                        bufferlist& indata, bufferlist& outdata)
 {
   string rs;
-  bufferlist odata;
-  int ret = do_command(ctx, cmd, indata, rs, odata);
-  if (ret)
-    return ret;
-
-  int len = odata.length();
-  if (!len) {
-    // no output
-    return 0;
-  }
-
-  if (!outfile) {
-    // error: no output specified
-    derr << " got " << len << " byte payload, discarding "
-         << "(specify -o <outfile)" << dendl;
-    return 1;
-  }
-  if (strcmp(outfile, "-") == 0) {
-    // write to stdout
-    fwrite(odata.c_str(), len, 1, stdout);
-    return 0;
-  }
-
-  // Write to a file. Don't truncate the file.
-  int fd = TEMP_FAILURE_RETRY(::open(outfile, O_WRONLY|O_CREAT, 0644));
-  if (fd < 0) {
-    int err = errno;
-    derr << " failed to create file '" << outfile << "': "
-        << cpp_strerror(err) << dendl;
-    return 1;
-  }
-  ret = odata.write_fd(fd);
-  if (ret) {
-    derr << " error writing file: " << cpp_strerror(ret) << dendl;
-    close(fd);
-    return 1;
-  }
-  derr << " wrote " << len << " byte payload to " << outfile << dendl;
-  if (close(fd)) {
-    int err = errno;
-    derr << " error while closing file '" << outfile << "': "
-        << cpp_strerror(err) << dendl;
-    return 1;
-  }
-  return 0;
+  return do_command(ctx, cmd, indata, rs, outdata);
 }
 
 CephToolCtx* ceph_tool_common_init(ceph_tool_mode_t mode, bool concise)
index e91d1ca6879f1c35c526ade16a48fc22446c91df..6513036f7d2fde2fdf252416cab8384132e8f9ac 100644 (file)
@@ -81,8 +81,8 @@ int ceph_tool_do_cli(CephToolCtx *data);
 int run_command(CephToolCtx *data, const char *line);
 void send_observe_requests(CephToolCtx *ctx);
 CephToolCtx* ceph_tool_common_init(ceph_tool_mode_t mode, bool concise);
-int ceph_tool_cli_input(CephToolCtx *ctx, std::vector<std::string> &cmd, 
-                       const char *outfile, bufferlist &indata);
+int ceph_tool_do_command(CephToolCtx *ctx, std::vector<std::string>& cmd, 
+                        bufferlist& indata, bufferlist& outdata);
 int ceph_tool_messenger_shutdown();
 int ceph_tool_common_shutdown(CephToolCtx *ctx);