Преглед на файлове

only initialize light rc when needed

Christian Westrom преди 2 години
родител
ревизия
4f93f0d798
No account linked to committer's email
променени са 1 файла, в които са добавени 47 реда и са изтрити 43 реда
  1. 47
    43
      src/light.c

+ 47
- 43
src/light.c Целия файл

@@ -70,27 +70,59 @@ static void _light_add_device_target(light_device_t *device, light_device_target
70 70
     device->num_targets = new_num_targets;
71 71
 }
72 72
 
73
+static bool light_rc_initialize(light_context_t *new_ctx) {
74
+    // Setup the configuration folder
75
+    // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
76
+    uid_t euid = geteuid();
77
+
78
+    if(euid == 0)
79
+    {
80
+        snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
81
+    }
82
+    else
83
+    {
84
+        char *xdg_conf = getenv("XDG_CONFIG_HOME");
85
+
86
+        if(xdg_conf != NULL)
87
+        {
88
+            snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
89
+        }
90
+        else
91
+        {
92
+            snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
93
+        }
94
+    }
95
+
96
+    // Make sure the configuration folder exists, otherwise attempt to create it
97
+    int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
98
+    if(rc && errno != EEXIST)
99
+    {
100
+        LIGHT_WARN("couldn't create configuration directory");
101
+        return false;
102
+    }
103
+    return true;
104
+}
105
+
73 106
 static void _light_get_target_path(light_context_t* ctx, char* output_path, size_t output_size)
74 107
 {
75
-    snprintf(output_path, output_size,
76
-                "%s/targets/%s/%s/%s",
77
-                ctx->sys_params.conf_dir,
78
-                ctx->run_params.device_target->device->enumerator->name,
79
-                ctx->run_params.device_target->device->name,
80
-                ctx->run_params.device_target->name
81
-            );
108
+    if (light_rc_initialize(ctx)) {
109
+      snprintf(output_path, output_size, "%s/targets/%s/%s/%s",
110
+               ctx->sys_params.conf_dir,
111
+               ctx->run_params.device_target->device->enumerator->name,
112
+               ctx->run_params.device_target->device->name,
113
+               ctx->run_params.device_target->name);
114
+    }
82 115
 }
83 116
 
84 117
 static void _light_get_target_file(light_context_t* ctx, char* output_path, size_t output_size, char const * file)
85 118
 {
86
-    snprintf(output_path, output_size,
87
-                "%s/targets/%s/%s/%s/%s",
88
-                ctx->sys_params.conf_dir,
89
-                ctx->run_params.device_target->device->enumerator->name,
90
-                ctx->run_params.device_target->device->name,
91
-                ctx->run_params.device_target->name,
92
-                file
93
-            );
119
+    if (light_rc_initialize(ctx)) {
120
+      snprintf(output_path, output_size, "%s/targets/%s/%s/%s/%s",
121
+               ctx->sys_params.conf_dir,
122
+               ctx->run_params.device_target->device->enumerator->name,
123
+               ctx->run_params.device_target->device->name,
124
+               ctx->run_params.device_target->name, file);
125
+    }
94 126
 }
95 127
 
96 128
 static uint64_t _light_get_min_cap(light_context_t *ctx)
@@ -447,34 +479,6 @@ light_context_t* light_initialize(int argc, char **argv)
447 479
         }
448 480
     }
449 481
 
450
-    // Setup the configuration folder
451
-    // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
452
-    if(euid == 0)
453
-    {
454
-        snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
455
-    }
456
-    else
457
-    {
458
-        char *xdg_conf = getenv("XDG_CONFIG_HOME");
459
-        
460
-        if(xdg_conf != NULL)
461
-        {
462
-            snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
463
-        }
464
-        else
465
-        {
466
-            snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
467
-        }
468
-    }
469
-    
470
-    // Make sure the configuration folder exists, otherwise attempt to create it
471
-    int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
472
-    if(rc && errno != EEXIST)
473
-    {
474
-        LIGHT_WARN("couldn't create configuration directory");
475
-        return false;
476
-    }
477
-    
478 482
     // Create the built-in enumerators
479 483
     light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
480 484
     light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);