// Recursively create a path if it does not already exist static int esif_ccb_makepath (char *path) { int rc = -1; struct stat st = {0}; // create path only if it does not already exist if ((rc = esif_ccb_stat(path, &st)) != 0) { size_t len = esif_ccb_strlen(path, MAX_PATH); char dir[MAX_PATH]; char *slash; // trim any trailing slash esif_ccb_strcpy(dir, path, MAX_PATH); if (len > 0 && dir[len - 1] == *ESIF_PATH_SEP) { dir[len - 1] = 0; } // if path doesn't exist and can't be created, recursively create parent folder(s) if ((rc = esif_ccb_stat(dir, &st)) != 0 && (rc = esif_ccb_mkdir(dir)) != 0) { if ((slash = esif_ccb_strrchr(dir, *ESIF_PATH_SEP)) != 0) { *slash = 0; if ((rc = esif_ccb_makepath(dir)) == 0) { *slash = *ESIF_PATH_SEP; rc = esif_ccb_mkdir(dir); } } } } return rc; }
// Automatically Load all Static DataVaults and *.dv files in the current folder into the DataBank eEsifError DataBank_LoadDataVaults (DataBankPtr self) { eEsifError rc = ESIF_OK; esif_ccb_file_find_handle find_handle = INVALID_HANDLE_VALUE; char file_path[MAX_PATH] = {0}; char file_pattern[MAX_PATH] = {0}; struct esif_ccb_file *ffd_ptr; UInt32 idx; ASSERT(self); // Import all Static DataVaults into ReadOnly DataVaults for (idx = 0; g_StaticDataVaults[idx].name; idx++) { DataVaultPtr DB = DataBank_OpenNameSpace(self, g_StaticDataVaults[idx].name); if (DB) { IOStream_SetMemory(DB->stream, g_StaticDataVaults[idx].buffer, g_StaticDataVaults[idx].buf_len); DB->flags |= (ESIF_SERVICE_CONFIG_READONLY | ESIF_SERVICE_CONFIG_NOCACHE); DataVault_ReadVault(DB); } } // Create DataVault Directory if it doesn't exit esif_build_path(file_path, sizeof(file_path), ESIF_PATHTYPE_DV, NULL, NULL); esif_ccb_makepath(file_path); // Import all matching *.dv files into ReadWrite DataVaults esif_ccb_sprintf(MAX_PATH, file_pattern, "*%s", ESIFDV_FILEEXT); ffd_ptr = (struct esif_ccb_file*)esif_ccb_malloc(sizeof(*ffd_ptr)); if (NULL == ffd_ptr) { rc = ESIF_E_NO_MEMORY; } if (rc == ESIF_OK) { find_handle = esif_ccb_file_enum_first(file_path, file_pattern, ffd_ptr); } if (INVALID_HANDLE_VALUE != find_handle) { do { struct esif_ccb_file dv_file = {0}; DataVaultPtr DB = 0; // Read DataVault File, unless it's already been loaded as a Static DataVault if (esif_ccb_strlen(ffd_ptr->filename, MAX_PATH) > sizeof(ESIFDV_FILEEXT)) { ffd_ptr->filename[esif_ccb_strlen(ffd_ptr->filename, MAX_PATH) - (sizeof(ESIFDV_FILEEXT) - 1)] = 0; // Truncate ".dv" extension if (DataBank_GetNameSpace(self, ffd_ptr->filename) == NULL) { DB = DataBank_OpenNameSpace(self, ffd_ptr->filename); if (DB) { esif_build_path(dv_file.filename, sizeof(dv_file.filename), ESIF_PATHTYPE_DV, DB->name, ESIFDV_FILEEXT); IOStream_SetFile(DB->stream, dv_file.filename, "rb"); DataVault_ReadVault(DB); } } } } while (esif_ccb_file_enum_next(find_handle, file_pattern, ffd_ptr)); esif_ccb_file_enum_close(find_handle); } esif_ccb_free(ffd_ptr); return rc; }
// Build Full Pathname for a given path type including an optional filename and extention. esif_string esif_build_path( esif_string buffer, size_t buf_len, esif_pathtype type, esif_string filename, esif_string ext) { char *pathname = NULL; if (NULL == buffer) { return NULL; } // Get Pathname. Do not return full path in result string if it starts with a "#" unless no filename specified if ((pathname = esif_pathlist_get(type)) != NULL) { if (*pathname == '#') { if (filename == NULL && ext == NULL) pathname++; else pathname = ""; } esif_ccb_strcpy(buffer, pathname, buf_len); } // Create folder if necessary if (buffer[0] != '\0') { esif_ccb_makepath(buffer); } // Append Optional filename.ext if (filename != NULL || ext != NULL) { if (buffer[0] != '\0') esif_ccb_strcat(buffer, ESIF_PATH_SEP, buf_len); if (filename != NULL) esif_ccb_strcat(buffer, filename, buf_len); if (ext != NULL) esif_ccb_strcat(buffer, ext, buf_len); } if (buffer[0] == '\0') { buffer = NULL; } return buffer; }
// Automatically Load all Static DataVaults and *.dv files in the current folder into the DataBank eEsifError DataBank_LoadDataVaults (DataBankPtr self) { eEsifError rc = ESIF_OK; esif_ccb_file_find_handle find_handle; esif_string file_path = 0; char file_pattern[MAX_PATH]; char file_path_buf[MAX_PATH] = {0}; struct esif_ccb_file *ffd_ptr; UInt32 idx; ASSERT(self); esif_ccb_lock_init(&self->lock); // TODO: Locking // Import all Static DataVaults into ReadOnly DataVaults for (idx = 0; g_StaticDataVaults[idx].name; idx++) { DataVaultPtr DB = DataBank_OpenNameSpace(self, g_StaticDataVaults[idx].name); if (DB) { IOStream_SetMemory(DB->stream, g_StaticDataVaults[idx].buffer, g_StaticDataVaults[idx].buf_len); DB->flags |= (ESIF_SERVICE_CONFIG_READONLY | ESIF_SERVICE_CONFIG_NOCACHE); DataVault_ReadVault(DB); } } // Create ESIFDV_DIR if it doesn't exit (only 1 level deep for now) esif_ccb_makepath(ESIFDV_DIR); // Import all matching *.dv files into ReadWrite DataVaults esif_ccb_strcpy(file_path_buf, ESIFDV_DIR, sizeof(file_path_buf)); file_path = file_path_buf; esif_ccb_sprintf(MAX_PATH, file_pattern, "*%s", ESIFDV_FILEEXT); ffd_ptr = (struct esif_ccb_file*)esif_ccb_malloc(sizeof(*ffd_ptr)); if (NULL == ffd_ptr) { return ESIF_E_NO_MEMORY; } find_handle = esif_ccb_file_enum_first(file_path, file_pattern, ffd_ptr); if (INVALID_HANDLE_VALUE == find_handle) { rc = ESIF_OK; // No Persisted DataVaults found } else { do { struct esif_ccb_file dv_file = {0}; DataVaultPtr DB = 0; // Read DataVault File, unless it's already been loaded as a Static DataVault if (esif_ccb_strlen(ffd_ptr->filename, MAX_PATH) > sizeof(ESIFDV_FILEEXT)) { ffd_ptr->filename[esif_ccb_strlen(ffd_ptr->filename, MAX_PATH) - (sizeof(ESIFDV_FILEEXT) - 1)] = 0; // Truncate ".dv" extension if (DataBank_GetNameSpace(self, ffd_ptr->filename) == NULL) { DB = DataBank_OpenNameSpace(self, ffd_ptr->filename); if (DB) { esif_ccb_sprintf(MAX_PATH, dv_file.filename, "%s%s%s", file_path, DB->name, ESIFDV_FILEEXT); IOStream_SetFile(DB->stream, dv_file.filename, "rb"); DataVault_ReadVault(DB); } } } } while (esif_ccb_file_enum_next(find_handle, file_pattern, ffd_ptr)); esif_ccb_file_enum_close(find_handle); } esif_ccb_free(ffd_ptr); return rc; }