monitor/att: Detect cache changes
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Tue, 18 Oct 2022 21:25:10 +0000 (14:25 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:55 +0000 (14:55 +0530)
This attempts to detect if the were any changes on cache files since
they were last loaded and then attempt to reload them.

Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
monitor/att.c

index bf5089f..7cd9967 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdbool.h>
 #include <errno.h>
 #include <linux/limits.h>
+#include <sys/stat.h>
 
 #include <glib.h>
 
@@ -2425,7 +2426,9 @@ struct att_read {
 
 struct att_conn_data {
        struct gatt_db *ldb;
+       struct timespec ldb_mtim;
        struct gatt_db *rdb;
+       struct timespec rdb_mtim;
        struct queue *reads;
 };
 
@@ -2439,6 +2442,28 @@ static void att_conn_data_free(void *data)
        free(att_data);
 }
 
+static void gatt_load_db(struct gatt_db *db, const char *filename,
+                                               struct timespec *mtim)
+{
+       struct stat st;
+
+       if (lstat(filename, &st))
+               return;
+
+       if (!gatt_db_isempty(db)) {
+               /* Check if file has been modified since last time */
+               if (st.st_mtim.tv_sec == mtim->tv_sec &&
+                                   st.st_mtim.tv_nsec == mtim->tv_nsec)
+                       return;
+               /* Clear db before reloading */
+               gatt_db_clear(db);
+       }
+
+       *mtim = st.st_mtim;
+
+       btd_settings_gatt_db_load(db, filename);
+}
+
 static void load_gatt_db(struct packet_conn_data *conn)
 {
        struct att_conn_data *data = conn->data;
@@ -2454,22 +2479,14 @@ static void load_gatt_db(struct packet_conn_data *conn)
                conn->destroy = att_conn_data_free;
        }
 
-       if (!gatt_db_isempty(data->ldb) && !gatt_db_isempty(data->rdb))
-               return;
-
        ba2str((bdaddr_t *)conn->src, local);
        ba2str((bdaddr_t *)conn->dst, peer);
 
-       if (gatt_db_isempty(data->ldb)) {
-               create_filename(filename, PATH_MAX, "/%s/attributes", local);
-               btd_settings_gatt_db_load(data->ldb, filename);
-       }
+       create_filename(filename, PATH_MAX, "/%s/attributes", local);
+       gatt_load_db(data->ldb, filename, &data->ldb_mtim);
 
-       if (gatt_db_isempty(data->rdb)) {
-               create_filename(filename, PATH_MAX, "/%s/cache/%s", local,
-                                                               peer);
-               btd_settings_gatt_db_load(data->rdb, filename);
-       }
+       create_filename(filename, PATH_MAX, "/%s/cache/%s", local, peer);
+       gatt_load_db(data->rdb, filename, &data->rdb_mtim);
 }
 
 static struct gatt_db_attribute *get_attribute(const struct l2cap_frame *frame,