[sysprof] sysprof-cli: Don't overwrite capture file by default



commit 87289e04212b2cde72ca53f7ce29c6a3b43322d9
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Nov 26 10:03:21 2016 -0500

    sysprof-cli: Don't overwrite capture file by default
    
    Add -f / --force to allow overwrite
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775062

 tools/sysprof-cli.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/tools/sysprof-cli.c b/tools/sysprof-cli.c
index 9e24415..7ddcf13 100644
--- a/tools/sysprof-cli.c
+++ b/tools/sysprof-cli.c
@@ -89,11 +89,14 @@ main (gint   argc,
   GSource *gsource;
   gchar *command = NULL;
   gboolean version = FALSE;
+  gboolean force = FALSE;
   int pid = -1;
   int fd;
+  int flags;
   GOptionEntry entries[] = {
     { "pid", 'p', 0, G_OPTION_ARG_INT, &pid, N_("Make sysprof specific to a task"), N_("PID") },
     { "command", 'c', 0, G_OPTION_ARG_STRING, &command, N_("Run a command and profile the process"), 
N_("COMMAND") },
+    { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Force overwrite the capture file") },
     { "version", 0, 0, G_OPTION_ARG_NONE, &version, N_("Print the sysprof-cli version and exit") },
     { NULL }
   };
@@ -151,7 +154,19 @@ main (gint   argc,
   if (argc == 2)
     filename = argv[1];
 
-  if (-1 == (fd = g_open (filename, O_CREAT | O_RDWR | O_CLOEXEC, 0640)))
+  flags = O_CREAT | O_RDWR | O_CLOEXEC;
+  if (!force)
+    {
+      /* if we don't force overwrite we want to ensure the file doesn't exist
+       * and never overwrite it. O_EXCL will prevent opening in that case */
+      flags |= O_EXCL;
+      if (g_file_test (filename, G_FILE_TEST_EXISTS))
+        {
+          g_printerr (_("%s exists. Use --force to overwrite\n"), filename);
+          return EXIT_FAILURE;
+        }
+    }
+  if (-1 == (fd = g_open (filename, flags, 0640)))
     {
       g_printerr ("Failed to open %s\n", filename);
       return EXIT_FAILURE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]