123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- #include "precompile.h"
- #include "sp_dir.h"
- #include "sp_def.h"
- #include "shm_mem.h"
- #include "fileutil.h"
- int sp_dir_create(sp_dir_t **p_dir)
- {
- sp_dir_t *dir;
- int rc = Error_Unexpect;
- char tmp[MAX_PATH], drivePath[_MAX_DRIVE] = "";
- dir = shm_malloc(sizeof(sp_dir_t));
- memset(dir, 0, sizeof(sp_dir_t));
- GetModuleFileNameA(NULL, tmp, MAX_PATH);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- dir->bin_path = shm_strdup(tmp);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- dir->base_path = shm_strdup(tmp);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- dir->root_ver_path = shm_strdup(tmp);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- dir->root_path = shm_strdup(tmp);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- /*get current disk name.*/
- sp_dir_get_cur_drive(drivePath);
- dir->rvc_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "rvc", drivePath);
- #ifdef _WIN32
- dir->root_ver_path = shm_strdup_printf("%s\\version", dir->root_path);
- dir->root_hardwarecfg_path = shm_strdup_printf("%s\\hardwarecfg", dir->root_path);
- dir->root_runinfo_path = shm_strdup_printf("%s\\runinfo", dir->root_path);
- dir->dep_path = shm_strdup_printf("%s\\dep", dir->base_path);
- dir->cfg_path = shm_strdup_printf("%s\\cfg", dir->base_path);
- dir->mod_path = shm_strdup_printf("%s\\mod", dir->base_path);
- dir->obj_path = shm_strdup_printf("%s\\obj", dir->base_path);
- dir->dbg_path = shm_strdup_printf("%s\\dbg", dir->rvc_path);
- dir->slv_path = shm_strdup_printf("%s\\slv", dir->rvc_path);
- dir->dmp_path = shm_strdup_printf("%s\\dmp", dir->rvc_path);
- dir->ad0_path = shm_strdup_printf("%s\\ad0", dir->rvc_path);
- #else
- dir->root_ver_path = shm_strdup_printf("%s/version", dir->root_path);
- dir->root_hardwarecfg_path = shm_strdup_printf("%s/hardwarecfg", dir->root_path);
- dir->root_runinfo_path = shm_strdup_printf("%s/runinfo", dir->root_path);
- dir->dep_path = shm_strdup_printf("%s/dep", dir->base_path);
- dir->cfg_path = shm_strdup_printf("%s/cfg", dir->base_path);
- dir->mod_path = shm_strdup_printf("%s/mod", dir->base_path);
- dir->obj_path = shm_strdup_printf("%s/obj", dir->base_path);
- dir->dbg_path = shm_strdup_printf("%s/dbg", dir->rvc_path);
- dir->slv_path = shm_strdup_printf("%s/slv", dir->rvc_path);
- dir->dmp_path = shm_strdup_printf("%s/dmp", dir->rvc_path);
- dir->ad0_path = shm_strdup_printf("%s/ad0", dir->rvc_path);
- #endif //_WIN32
- /*if (!ExistsDirA(dir->obj_path)) {
- CreateDirRecursiveA(dir->obj_path);
- }*/
- if (!ExistsDirA(dir->rvc_path)) {
- CreateDirRecursiveA(dir->rvc_path);
- }
- if (!ExistsDirA(dir->dmp_path)) {
- CreateDirRecursiveA(dir->dmp_path);
- }
- if (!ExistsDirA(dir->dbg_path)) {
- CreateDirRecursiveA(dir->dbg_path);
- }
- if (!ExistsDirA(dir->slv_path)) {
- CreateDirRecursiveA(dir->slv_path);
- }
- memset(tmp, 0, sizeof(tmp));
- #ifdef _WIN32
- sprintf(tmp, "%s\\runcfg", dir->root_runinfo_path);
- #else
- sprintf(tmp, "%s/runcfg", dir->root_runinfo_path);
- #endif //_WIN32
- if (!ExistsDirA(tmp))
- CreateDirRecursiveA(tmp);
- *p_dir = dir;
- rc = 0;
- return rc;
- }
- void sp_dir_destroy(sp_dir_t* dir)
- {
- if (dir) {
- if (dir->base_path)
- shm_free(dir->base_path);
- if (dir->rvc_path)
- shm_free(dir->rvc_path);
- if (dir->bin_path)
- shm_free(dir->bin_path);
- if (dir->dep_path)
- shm_free(dir->dep_path);
- if (dir->cfg_path)
- shm_free(dir->cfg_path);
- if (dir->mod_path)
- shm_free(dir->mod_path);
- if (dir->obj_path)
- shm_free(dir->obj_path);
- if (dir->dbg_path)
- shm_free(dir->dbg_path);
- if (dir->slv_path)
- shm_free(dir->slv_path);
- if (dir->dmp_path)
- shm_free(dir->dmp_path);
- if (dir->root_path)
- shm_free(dir->root_path);
- if (dir->root_hardwarecfg_path)
- shm_free(dir->root_hardwarecfg_path);
- if (dir->root_runinfo_path)
- shm_free(dir->root_runinfo_path);
- if (dir->root_ver_path)
- shm_free(dir->root_ver_path);
- shm_free(dir);
- }
- }
- static int __dir_get_path(char *base_dir, int flag, const char *cat_name, char *buf, int len, const char *site)
- {
- int rc = 0;
- int needed = strlen(base_dir);
- if (flag == SP_DIR_ENTITY_INI) {
- needed += strlen(cat_name) + 6;
- } else if (flag == SP_DIR_SHELL_INI) {
- needed += 11;
- }else if (flag == SP_DIR_SHELLVAR_INI)
- {
- needed += 22;
- }else if (flag == SP_DIR_MODULE_BIN) {
- needed += strlen(cat_name) + 6;
- } else if (flag == SP_DIR_ROOT_INI) {
- needed += 10;
- } else if (flag == SP_DIR_DEVICE_ENTITY_INI) {
- needed += strlen(cat_name) + 6;
- } else if (flag == SP_DIR_RUNINFO_INI) {
- needed += strlen(cat_name) + 6 + 7;
- } else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
- needed += strlen(cat_name) + 6 + 8;
- } else if (flag == SP_DIR_INSTALL_INI) {
- needed += 13;
- } else if (flag == SP_DIR_EXPLORER_INI) {
- needed += 16;
- } else if (flag == SP_DIR_CENTER_SETTING_INI){
- needed += 18;
- } else {
- rc = Error_Param;
- }
- if (buf) {
- if (len && len < needed)
- return Error_TooSmallBuffer;
- } else {
- return needed;
- }
- if (flag == SP_DIR_ENTITY_INI || flag == SP_DIR_DEVICE_ENTITY_INI)
- {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".ini");
- }
- else if (flag == SP_DIR_RUNINFO_INI)
- {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".ini");
- }
- else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "bootrec" SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".log");
- }else if (flag == SP_DIR_SHELLVAR_INI){
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "shellVar.ini");
- }else if (flag == SP_DIR_SHELL_INI) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "shell.ini");
- } else if (flag == SP_DIR_MODULE_BIN) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR);
- #ifdef _WIN32
- strcat(buf, cat_name);
- strcat(buf, ".dll");
- #else
- strcat(buf, "lib");
- strcat(buf, cat_name);
- strcat(buf, ".so");
- #endif //_WIN32
- } else if (flag == SP_DIR_ROOT_INI) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "root.ini");
- } else if (flag == SP_DIR_INSTALL_INI) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "install.ini");
- }
- else if (flag == SP_DIR_CENTER_SETTING_INI)
- {
- // 为了兼容旧版本实体,此处仍返回原有配置名
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "CenterSetting.ini");
- }
- return rc;
- }
- static int __dir_get_path_new(char *base_dir, int flag, const char *cat_name, char *buf, int len, const char *site)
- {
- int rc = 0;
- int needed = strlen(base_dir);
- if (flag == SP_DIR_ENTITY_INI) {
- needed += strlen(cat_name) + 6;
- } else if (flag == SP_DIR_SHELL_INI) {
- needed += 11;
- } else if (flag == SP_DIR_MODULE_BIN) {
- needed += strlen(cat_name) + 6;
- } else if (flag == SP_DIR_ROOT_INI) {
- needed += 10;
- } else if (flag == SP_DIR_DEVICE_ENTITY_INI) {
- needed += strlen(cat_name) + 6;
- } else if (flag == SP_DIR_RUNINFO_INI) {
- needed += strlen(cat_name) + 6 + 7;
- } else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
- needed += strlen(cat_name) + 6 + 8;
- } else if (flag == SP_DIR_INSTALL_INI) {
- needed += 13;
- } else if (flag == SP_DIR_EXPLORER_INI) {
- needed += 16;
- } else if (flag == SP_DIR_CENTER_SETTING_INI){
- needed += 18;
- } else {
- rc = Error_Param;
- }
- if (buf) {
- if (len && len < needed)
- return Error_TooSmallBuffer;
- } else {
- return needed;
- }
- if (flag == SP_DIR_ENTITY_INI || flag == SP_DIR_DEVICE_ENTITY_INI)
- {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".ini");
- }
- else if (flag == SP_DIR_RUNINFO_INI)
- {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".ini");
- }
- else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "bootrec" SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".log");
- } else if (flag == SP_DIR_SHELL_INI) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "shell.ini");
- } else if (flag == SP_DIR_MODULE_BIN) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR);
- strcat(buf, cat_name);
- strcat(buf, ".dll");
- } else if (flag == SP_DIR_ROOT_INI) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "root.ini");
- } else if (flag == SP_DIR_INSTALL_INI) {
- strcpy(buf, base_dir);
- strcat(buf, SPLIT_SLASH_STR "install.ini");
- }
- else if (flag == SP_DIR_CENTER_SETTING_INI)
- {
- // 为了兼容旧版本实体,此处仍返回原有配置名
- strcpy(buf, base_dir);
- if ('C' == base_dir[0])
- {
- strcat(buf, SPLIT_SLASH_STR "CenterSetting.DMZ.ini");
- }
- else
- {
- strcat(buf, SPLIT_SLASH_STR "CenterSetting.LAN.ini");
- }
- }
- return rc;
- }
- int sp_dir_get_path(sp_dir_t* dir, int flag, const char *cat_name, char *buf, int len)
- {
- char *base_path = NULL;
- if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
- base_path = dir->root_hardwarecfg_path;
- }
- else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
- base_path = dir->cfg_path;
- }
- else if (flag == SP_DIR_MODULE_BIN) {
- base_path = dir->mod_path;
- }
- else if (flag == SP_DIR_RUNINFO_INI || flag == SP_DIR_SHELLVAR_INI) {
- base_path = dir->root_runinfo_path;
- }
- else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
- base_path = dir->root_runinfo_path;
- }
- else if (flag == SP_DIR_INSTALL_INI) {
- base_path = dir->base_path;
- }
- else if (flag == SP_DIR_EXPLORER_INI) {
- base_path = dir->root_path;
- }
- else if (flag == SP_DIR_CENTER_SETTING_INI) {
- base_path = dir->cfg_path;
- }
- else {
- return Error_Param;
- }
- return __dir_get_path(base_path, flag, cat_name, buf, len, NULL);
- }
- int sp_dir_get_path_new(sp_dir_t* dir, int flag, const char *cat_name, char *buf, int len)
- {
- char *base_path = NULL;
- if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
- base_path = dir->root_hardwarecfg_path;
- }
- else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
- base_path = dir->cfg_path;
- }
- else if (flag == SP_DIR_MODULE_BIN) {
- base_path = dir->mod_path;
- }
- else if (flag == SP_DIR_RUNINFO_INI) {
- base_path = dir->root_runinfo_path;
- }
- else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
- base_path = dir->root_runinfo_path;
- }
- else if (flag == SP_DIR_INSTALL_INI) {
- base_path = dir->base_path;
- }
- else if (flag == SP_DIR_EXPLORER_INI) {
- base_path = dir->root_path;
- }
- else if (flag == SP_DIR_CENTER_SETTING_INI) {
- base_path = dir->cfg_path;
- }
- else {
- return Error_Param;
- }
- return __dir_get_path_new(base_path, flag, cat_name, buf, len, NULL);
- }
- int sp_dir_get_path_version(sp_dir_t* dir, int major, int minor, int revision, int build, int flag, const char *cat_name, char *buf, int len)
- {
- char tmp[MAX_PATH];
- char *base_path = NULL;
-
- if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
- base_path = dir->root_hardwarecfg_path;
- } else if (flag == SP_DIR_RUNINFO_INI || flag == SP_DIR_RUNINFO_BOOT_LOG) {
- base_path = dir->root_runinfo_path;
- } else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
- sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d" SPLIT_SLASH_STR "cfg", dir->root_ver_path, major, minor, revision, build);
- base_path = tmp;
- } else if (flag == SP_DIR_MODULE_BIN) {
- sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d" SPLIT_SLASH_STR "mod", dir->root_ver_path, major, minor, revision, build);
- base_path = tmp;
- } else if (flag == SP_DIR_INSTALL_INI) {
- sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d", dir->root_ver_path, major, minor, revision, build);
- base_path = tmp;
- } else {
- return Error_Param;
- }
- return __dir_get_path(base_path, flag, cat_name, buf, len, NULL);
- }
- void sp_dir_get_cur_drive(char* path)
- {
- char drive[_MAX_DRIVE] = {'\0'};
- char dir[_MAX_DIR] = { '\0' };
- char fname[_MAX_FNAME] = { '\0' };
- char ext[_MAX_EXT] = { '\0' };
- char chpath[MAX_PATH] = { '\0' };
- GetModuleFileNameA(NULL, (LPSTR)chpath, sizeof(chpath));
- _splitpath(chpath, drive, dir, fname, ext);
- memcpy(path, drive, sizeof(drive));
- }
|