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
);
79 * Create the empty config file a the path.
81 static int create_config_file(char *path
)
86 fp
= open_config(path
, "w+");
88 ERR("Unable to create config file");
102 * Append data to the config file in file_path
104 static int write_config(char *file_path
, size_t size
, char *data
)
110 fp
= open_config(file_path
, "a");
116 /* Write session name into config file */
117 len
= fwrite(data
, size
, 1, fp
);
127 * config_get_default_path
129 * Return the HOME directory path. Caller MUST NOT free(3) the return pointer.
131 char *config_get_default_path(void)
133 return getenv("HOME");
139 * Destroy directory config and file config.
141 void config_destroy(char *path
)
146 config_path
= config_get_file_path(path
);
147 if (config_path
== NULL
) {
151 ret
= remove(config_path
);
153 perror("remove config file");
160 * config_read_session_name
162 * Return sesson name from the config file.
164 char *config_read_session_name(char *path
)
168 char var
[NAME_MAX
], *session_name
;
170 fp
= open_config(path
, "r");
172 ERR("Can't find valid lttng config %s/.lttngrc", path
);
173 MSG("Did you create a session? (lttng create <my_sesion>)");
177 session_name
= malloc(NAME_MAX
);
179 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
181 ERR("Missing session=NAME in config file.");
187 if (strcmp(var
, "session") == 0) {
204 * config_add_session_name
206 * Write session name option to the config file.
208 int config_add_session_name(char *path
, char *name
)
211 char session_name
[NAME_MAX
];
213 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
217 ret
= write_config(path
, ret
, session_name
);
225 * Init configuration directory and file.
227 int config_init(char *session_name
)
232 path
= config_get_default_path();
238 /* Create default config file */
239 ret
= create_config_file(path
);
244 ret
= config_add_session_name(path
, session_name
);
249 DBG("Init config session in %s", path
);
This page took 0.033757 seconds and 4 git commands to generate.