Dmitrijs Ledkovs has proposed merging lp:~xnox/upstart/user-log-dir into 
lp:upstart.

Requested reviews:
  Upstart Reviewers (upstart-reviewers)

For more details, see:
https://code.launchpad.net/~xnox/upstart/user-log-dir/+merge/143091

This branch implements writting job logs to $XDG_CACHE_HOME/upstart/ when 
running in --user-mode.
One can override the location with command-line arg.
Note the environment variable is not honoured.
-- 
https://code.launchpad.net/~xnox/upstart/user-log-dir/+merge/143091
Your team Upstart Reviewers is requested to review the proposed merge of 
lp:~xnox/upstart/user-log-dir into lp:upstart.
=== modified file 'ChangeLog'
--- ChangeLog	2013-01-08 10:27:17 +0000
+++ ChangeLog	2013-01-14 11:54:20 +0000
@@ -1,3 +1,15 @@
+2013-01-14  Dmitrijs Ledkovs  <[email protected]>
+
+	* init/xdg.[ch]: add xdg_get_cache_home and get_user_log_dir
+	  functions. These retrieve XDG_CACHE_HOME and a subdir inside it
+	  for upstart.
+	* init/tests/test_xdg.c: reuse test_get_config_home to test both
+	  xdg_get_cache_home and xdg_get_config_home. Add test for
+	  get_user_log_dir.
+	* init/main.c: use get_user_log_dir to setup logging
+	  directory in user_mode. For now, command line argument is
+	  honoured, while the environment override is not.
+
 2013-01-04  Dmitrijs Ledkovs  <[email protected]>
 
 	* init/conf.c: add ability to apply override files from higher

=== modified file 'init/main.c'
--- init/main.c	2012-12-18 14:09:55 +0000
+++ init/main.c	2013-01-14 11:54:20 +0000
@@ -957,6 +957,11 @@
 	if (log_dir)
 		goto out;
 
+	if (user_mode) {
+		log_dir = get_user_log_dir ();
+		return;
+	}
+
 	log_dir = JOB_LOGDIR;
 
 	dir = getenv (LOGDIR_ENV);

=== modified file 'init/tests/test_xdg.c'
--- init/tests/test_xdg.c	2012-12-18 14:16:10 +0000
+++ init/tests/test_xdg.c	2013-01-14 11:54:20 +0000
@@ -69,25 +69,23 @@
 }
 
 void
-test_get_config_home (void)
+_test_get_home (char * env_var_name, char * dir_name, char * (*function)(void))
 {
 	char   dirname[PATH_MAX];
 	char * outname;
 	char * expected;
 
-	TEST_FUNCTION ("xdg_get_config_home");
-
 	TEST_FEATURE ("with HOME set and without environment override");
 	TEST_FILENAME (dirname);
 	TEST_EQ (setenv ("HOME", dirname, 1), 0);
-	TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0);
+	TEST_EQ (unsetenv (env_var_name), 0);
 	TEST_ALLOC_FAIL {
 		TEST_ALLOC_SAFE {
-			expected = NIH_MUST (nih_sprintf (NULL, "%s/.config", dirname));
+			expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name));
 		}
 
 		outname = NULL;
-		outname = xdg_get_config_home ();
+		outname = function ();
 
 		if (! test_alloc_failed) {
 			TEST_EQ_STR (outname, expected);
@@ -102,15 +100,15 @@
 	}
 
 	TEST_FEATURE ("with HOME set and with empty environment override");
-	TEST_EQ (setenv ("XDG_CONFIG_HOME", "", 1), 0);
+	TEST_EQ (setenv (env_var_name, "", 1), 0);
 
 
 	TEST_ALLOC_FAIL {
 		TEST_ALLOC_SAFE {
-			expected = NIH_MUST (nih_sprintf (NULL, "%s/.config", dirname));
+			expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name));
 		}
 		outname = NULL;
-		outname = xdg_get_config_home();
+		outname = function ();
 
 		if (test_alloc_failed) {
 			TEST_EQ_P (outname, NULL);
@@ -124,11 +122,11 @@
 
 	TEST_FEATURE ("with HOME set and with environment override");
 	expected = NIH_MUST (nih_strdup (NULL, "/home/me/.config-test"));
-	TEST_EQ (setenv ("XDG_CONFIG_HOME", expected, 1), 0);
+	TEST_EQ (setenv (env_var_name, expected, 1), 0);
 
 	TEST_ALLOC_FAIL {
 		outname = NULL;
-		outname = xdg_get_config_home();
+		outname = function ();
 
 		if (test_alloc_failed) {
 			TEST_EQ_P (outname, NULL);
@@ -144,7 +142,7 @@
 
 	TEST_ALLOC_FAIL {
 		outname = NULL;
-		outname = xdg_get_config_home();
+		outname = function ();
 
 		if (test_alloc_failed) {
 			TEST_EQ_P (outname, NULL);
@@ -157,24 +155,39 @@
 	nih_free(expected);
 
 	TEST_FEATURE ("without HOME set and with empty environment override");
-	TEST_EQ (setenv ("XDG_CONFIG_HOME", "", 1), 0);
+	TEST_EQ (setenv (env_var_name, "", 1), 0);
 
 	TEST_ALLOC_FAIL {
 		outname = NULL;
-		outname = xdg_get_config_home();
+		outname = function ();
 		TEST_EQ_P (outname, NULL);
 	}
 
 	TEST_FEATURE ("without HOME set and without environment override");
-	TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0);
+	TEST_EQ (unsetenv (env_var_name), 0);
 	TEST_ALLOC_FAIL {
 		outname = NULL;
-		outname = xdg_get_config_home();
+		outname = function ();
 		TEST_EQ_P (outname, NULL);
 	}
 }
 
 void
+test_get_config_home (void)
+{
+	TEST_FUNCTION ("xdg_get_config_home");
+	_test_get_home ("XDG_CONFIG_HOME", ".config", &xdg_get_config_home);
+
+}
+
+void
+test_get_cache_home (void)
+{
+	TEST_FUNCTION ("xdg_get_cache_home");
+	_test_get_home ("XDG_CACHE_HOME", ".cache", &xdg_get_cache_home);
+}
+
+void
 test_get_config_dirs (void)
 {
 	char   **dirs = NULL;
@@ -288,9 +301,36 @@
 			TEST_EQ (dirs[4], NULL);
 			nih_free (dirs);
 		}
-		nih_free(expected);
-	}
-
+		nih_free (expected);
+	}
+
+}
+
+void
+test_get_user_log_dir (void)
+{
+	char dirname[PATH_MAX];
+	char         *expected;
+	char             *path;
+
+	TEST_FUNCTION ("get_user_log_dir");
+	TEST_FEATURE ("with HOME set");
+	TEST_FILENAME (dirname);
+	TEST_EQ (setenv ("HOME", dirname, 1), 0);
+	TEST_EQ (unsetenv ("XDG_CACHE_HOME"), 0);
+
+	expected = nih_sprintf (NULL, "%s/.cache/upstart", dirname);
+
+	TEST_ALLOC_FAIL {
+		path = get_user_log_dir ();
+		if (test_alloc_failed) {
+			TEST_EQ_P (path, NULL);
+		} else {
+			TEST_EQ_STR (path, expected);
+			nih_free (path);
+		}
+	}
+	nih_free (expected);
 }
 
 int
@@ -301,6 +341,8 @@
 	test_get_config_home ();
 	test_get_config_dirs ();
 	test_get_user_upstart_dirs ();
+	test_get_cache_home ();
+	test_get_user_log_dir ();
 
 	return 0;
 }

=== modified file 'init/xdg.c'
--- init/xdg.c	2012-12-18 16:31:55 +0000
+++ init/xdg.c	2013-01-14 11:54:20 +0000
@@ -56,6 +56,31 @@
 }
 
 /**
+ * xdg_get_cache_home:
+ *
+ * Determine an XDG compliant XDG_CACHE_HOME
+ *
+ * Returns: newly-allocated path, or NULL on error.
+ **/
+char *
+xdg_get_cache_home (void)
+{
+	nih_local char  **env = NULL;
+	char             *dir;
+
+	dir = getenv ("XDG_CACHE_HOME");
+	
+	if (dir && dir[0]) {
+		dir = nih_strdup (NULL, dir);
+		return dir;
+	}
+
+	dir = get_home_subdir (".cache");
+
+	return dir;
+}
+
+/**
  * xdg_get_config_home:
  *
  * Determine an XDG compliant XDG_CONFIG_HOME
@@ -185,3 +210,20 @@
 	return NULL;
 }
 
+/**
+ * get_user_log_dir:
+ *
+ * Constructs an XDG compliant path to a cache directory in the user's
+ * home directory. It can be used to store logs.
+ *
+ * Returns: newly-allocated array of paths, or NULL or error.
+ **/
+char *
+get_user_log_dir (void)
+{
+	char *path=NULL;
+	path = xdg_get_cache_home ();
+	if (! path)
+		return NULL;
+	return nih_sprintf (NULL, "%s/%s", path, INIT_XDG_SUBDIR);
+}

=== modified file 'init/xdg.h'
--- init/xdg.h	2012-12-18 13:58:23 +0000
+++ init/xdg.h	2013-01-14 11:54:20 +0000
@@ -31,12 +31,18 @@
 char *    xdg_get_config_home    (void)
 	__attribute__ ((malloc, warn_unused_result));
 
+char *    xdg_get_cache_home     (void)
+	__attribute__ ((malloc, warn_unused_result));
+
 char **   xdg_get_config_dirs    (void)
 	__attribute__ ((malloc, warn_unused_result));
 
 char **   get_user_upstart_dirs  (void)
 	__attribute__ ((malloc, warn_unused_result));
 
+char *    get_user_log_dir       (void)
+	__attribute__ ((malloc, warn_unused_result));
+
 NIH_END_EXTERN
 
 #endif /* INIT_XDG_H */

-- 
upstart-devel mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/upstart-devel

Reply via email to