From fe69e01708c086e07034e6ecbd34b5bc2eb5f3fc Mon Sep 17 00:00:00 2001 From: prashanth kumar Date: Tue, 15 Oct 2019 14:42:18 +0530 Subject: [PATCH] [net-config] Manage the MAX saved wireless profiles. If the number of saved profiles exceed 200, remove the oldest profile in the list. Change-Id: Ic0a2fc850f13f44f3191efe9bb8e2aa7f13cd911 Signed-off-by: prashanth kumar --- include/util.h | 3 ++ include/wifi-config.h | 1 + src/utils/util.c | 74 +++++++++++++++++++++++++++++++++++++++++++ src/wifi-config.c | 26 ++++++++++++++- 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/include/util.h b/include/util.h index 631b3b7..9a0442b 100755 --- a/include/util.h +++ b/include/util.h @@ -130,6 +130,9 @@ gboolean netconfig_get_telephony_plugin_flag(); void netconfig_convert_bytes_to_hexstr(const char* bin, int blen, gchar* hexstr); +int get_files_count(const char* path); +char *get_least_recently_profile(const char* path); + #ifdef __cplusplus } #endif diff --git a/include/wifi-config.h b/include/wifi-config.h index b0a8f30..cbe1abe 100755 --- a/include/wifi-config.h +++ b/include/wifi-config.h @@ -33,6 +33,7 @@ extern "C" { #define WIFI_CONFIG_SECURITY_TYPE "Security" #define WIFI_CONFIG_FAVORITE "Favorite" #define WIFI_CONFIG_AUTOCONNECT "AutoConnect" +#define WIFI_CONFIG_MODIFIED "Modified" #define WIFI_CONFIG_HIDDEN "Hidden" #define WIFI_CONFIG_CREATED "Created" #define WIFI_CONFIG_IPV4_METHOD "IPv4.method" diff --git a/src/utils/util.c b/src/utils/util.c index 8d41a76..f113032 100755 --- a/src/utils/util.c +++ b/src/utils/util.c @@ -35,6 +35,10 @@ #include #include #include +#include +#include +#include +#include #include "log.h" #include "util.h" @@ -1641,3 +1645,73 @@ void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr) *hexstr = '\0'; } + +bool __is_hidden_file(const char *file) +{ + /* exclude "." , "..", "settings" and hidden files */ + if( g_strcmp0( file, "." ) == 0 || + g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 ) + { + return true; + } + + return false; +} + +int get_files_count(const char *path) +{ + DIR *dfd = NULL; + struct dirent *dir = NULL; + int file_count = 0; + dfd = opendir(path); + if (dfd) { + while ((dir = readdir(dfd)) != NULL) { + if (__is_hidden_file(dir->d_name)) + continue; + + if (strncmp(dir->d_name, "wifi_", 5) != 0) + continue; + + ++file_count; + } + closedir(dfd); + } + + return file_count; +} + +char * get_least_recently_profile(const char *path) +{ + DIR *dfd = NULL; + struct dirent *dir = NULL; + unsigned long lastModified = (unsigned long)~0; + char *file = NULL; + dfd = opendir(path); + if (dfd) { + while ((dir = readdir(dfd)) != NULL) { + if (__is_hidden_file(dir->d_name)) + continue; + + if (strncmp(dir->d_name, "wifi_", 5) != 0) + continue; + + struct stat attr; + gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name); + if (stat(full_path, &attr)== 0) { + if(lastModified > attr.st_mtime) + { + lastModified = attr.st_mtime; + file = dir->d_name; + } + } else { + ERR("stat failed"); + } + g_free(full_path); + } + closedir(dfd); + } + + DBG("least recently path: [%s]", file); + + return file; +} diff --git a/src/wifi-config.c b/src/wifi-config.c index 48b898d..e81bc43 100755 --- a/src/wifi-config.c +++ b/src/wifi-config.c @@ -55,6 +55,8 @@ #define NET_DNS_ADDR_MAX 2 +#define MAX_WIFI_PROFILES 200 + struct wifi_eap_config { gchar *anonymous_identity; gchar *ca_cert; @@ -85,6 +87,7 @@ struct wifi_config { gchar *security_type; gboolean favorite; gboolean autoconnect; + GTimeVal modified; gchar *is_hidden; gboolean is_created; gchar *proxy_address; @@ -301,7 +304,6 @@ static gboolean __remove_configuration(const gchar *pathname) ERR("Cannot remove [%s]", pathname); return FALSE; } - return TRUE; } @@ -1299,6 +1301,13 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context, g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite); g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect); + g_get_current_time(&conf->modified); + gchar *str = g_time_val_to_iso8601(&conf->modified); + if (str) { + g_key_file_set_string(keyfile, group_name, + WIFI_CONFIG_MODIFIED, str); + g_free(str); + } /* Optional field */ if (conf->proxy_address != NULL) { @@ -1355,6 +1364,21 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context, if (ret == TRUE) { INFO("Success to save configuration [%s]", config_id); wifi_complete_save_configuration(wifi, context); + char *file; + if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) { + file = get_least_recently_profile(CONNMAN_STORAGE); + if (file) { + gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file); + INFO("least modified file: %s", profileName); + if (profileName) { + if (__remove_configuration(profileName) != TRUE) + DBG("Failed to remove profile: [%s]", profileName); + } else + ERR("Profile: [%s] does not exist", profileName); + + g_free(profileName); + } + } } else { INFO("Fail to save configuration [%s]", config_id); netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration"); -- 2.34.1