2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
32 * config_get_file_path
34 * Return the path with '/CONFIG_FILENAME' added to it.
36 char *config_get_file_path(char *path
)
41 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
43 ERR("Fail allocating config file path");
52 * Return an open FILE pointer to the config file.
54 static FILE *open_config(char *path
, const char *mode
)
59 file_path
= config_get_file_path(path
);
60 if (file_path
== NULL
) {
64 fp
= fopen(file_path
, mode
);
66 perror("config file");
80 * Create the empty config file a the path.
82 static int create_config_file(char *path
)
87 fp
= open_config(path
, "w+");
89 ERR("Unable to create config file");
103 * Append data to the config file in file_path
105 static void write_config(char *file_path
, size_t size
, char *data
)
109 fp
= open_config(file_path
, "a");
114 /* Write session name into config file */
115 fwrite(data
, size
, 1, fp
);
123 * config_get_default_path
125 * Return the HOME directory path. Caller MUST NOT free(3) the return pointer.
127 char *config_get_default_path(void)
129 return getenv("HOME");
135 * Destroy directory config and file config.
137 void config_destroy(char *path
)
142 config_path
= config_get_file_path(path
);
143 if (config_path
== NULL
) {
147 ret
= remove(config_path
);
149 perror("remove config file");
156 * config_read_session_name
158 * Return sesson name from the config file.
160 char *config_read_session_name(char *path
)
164 char var
[NAME_MAX
], *session_name
;
166 fp
= open_config(path
, "r");
168 ERR("Can't find valid lttng config %s/.lttngrc", path
);
172 session_name
= malloc(NAME_MAX
);
174 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
176 ERR("Missing session=NAME in config file.");
182 if (strcmp(var
, "session") == 0) {
199 * config_add_session_name
201 * Write session name option to the config file.
203 int config_add_session_name(char *path
, char *name
)
206 char session_name
[NAME_MAX
];
208 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
213 write_config(path
, ret
, session_name
);
223 * Init configuration directory and file.
225 int config_init(char *session_name
)
230 path
= config_get_default_path();
236 /* Create default config file */
237 ret
= create_config_file(path
);
242 ret
= config_add_session_name(path
, session_name
);
247 DBG("Init config session in %s", path
);
This page took 0.036092 seconds and 5 git commands to generate.