@@ -33,6 +33,7 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
char *tracefs_instance_file_read(struct tracefs_instance *instance,
char *file, int *psize);
+bool tracefs_instance_exists(const char *name);
bool tracefs_file_exists(struct tracefs_instance *instance, char *name);
bool tracefs_dir_exists(struct tracefs_instance *instance, char *name);
@@ -257,7 +257,10 @@ static bool check_file_exists(struct tracefs_instance *instance,
int ret;
path = tracefs_instance_get_dir(instance);
- snprintf(file, PATH_MAX, "%s/%s", path, name);
+ if (name)
+ snprintf(file, PATH_MAX, "%s/%s", path, name);
+ else
+ snprintf(file, PATH_MAX, "%s", path);
tracefs_put_tracing_file(path);
ret = stat(file, &st);
if (ret < 0)
@@ -266,6 +269,23 @@ static bool check_file_exists(struct tracefs_instance *instance,
return !dir == !S_ISDIR(st.st_mode);
}
+/**
+ * tracefs_instance_exists - Check an instance with given name exists
+ * @name: name of the instance
+ *
+ * Returns true if the instance exists, false otherwise
+ *
+ */
+bool tracefs_instance_exists(const char *name)
+{
+ char file[PATH_MAX];
+
+ if (!name)
+ return false;
+ snprintf(file, PATH_MAX, "instances/%s", name);
+ return check_file_exists(NULL, file, true);
+}
+
/**
* tracefs_file_exists - Check if a file with given name exists in given instance
* @instance: ftrace instance, can be NULL for the top instance
@@ -197,6 +197,7 @@ static void test_instance_file(void)
CU_TEST(asprintf(&inst_dir, "%s/instances/%s", tdir, name) > 0);
CU_TEST(stat(inst_dir, &st) != 0);
+ CU_TEST(tracefs_instance_exists(name) == false);
instance = tracefs_instance_alloc(name);
CU_TEST(instance != NULL);
CU_TEST(stat(inst_dir, &st) != 0);
@@ -205,6 +206,7 @@ static void test_instance_file(void)
CU_TEST(strcmp(inst_name, name) == 0);
CU_TEST(tracefs_instance_create(instance) == 0);
+ CU_TEST(tracefs_instance_exists(name) == true);
CU_TEST(stat(inst_dir, &st) == 0);
CU_TEST(S_ISDIR(st.st_mode));
A new API is implemented: tracefs_instance_exists() which can be used to check if ftrace instance with given name exists in the system. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> --- include/tracefs/tracefs.h | 1 + lib/tracefs/tracefs-instance.c | 22 +++++++++++++++++++++- utest/tracefs-utest.c | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-)