[net-config] Manage the MAX saved wireless profiles. 74/215774/2
authorprashanth kumar <prasadam.p@samsung.com>
Tue, 15 Oct 2019 09:12:18 +0000 (14:42 +0530)
committerNiraj Kumar Goit <niraj.g@samsung.com>
Tue, 15 Oct 2019 14:08:34 +0000 (19:38 +0530)
If the number of saved profiles exceed 200, remove the
oldest profile in the list.

Change-Id: Ic0a2fc850f13f44f3191efe9bb8e2aa7f13cd911
Signed-off-by: prashanth kumar <prasadam.p@samsung.com>
include/util.h
include/wifi-config.h
src/utils/util.c
src/wifi-config.c

index 631b3b7..9a0442b 100755 (executable)
@@ -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
index b0a8f30..cbe1abe 100755 (executable)
@@ -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"
index 8d41a76..f113032 100755 (executable)
 #include <vconf-keys.h>
 #include <tzplatform_config.h>
 #include <system_info.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <time.h>
 
 #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;
+}
index 48b898d..e81bc43 100755 (executable)
@@ -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");