sp_dir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "precompile.h"
  2. #include "sp_dir.h"
  3. #include "sp_def.h"
  4. #include "shm_mem.h"
  5. #include "fileutil.h"
  6. int sp_dir_create(sp_dir_t **p_dir)
  7. {
  8. sp_dir_t *dir;
  9. int rc = Error_Unexpect;
  10. char tmp[MAX_PATH], drivePath[_MAX_DRIVE] = "";
  11. dir = shm_malloc(sizeof(sp_dir_t));
  12. memset(dir, 0, sizeof(sp_dir_t));
  13. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  14. *strrchr(tmp, SPLIT_SLASH) = 0;
  15. dir->bin_path = shm_strdup(tmp);
  16. *strrchr(tmp, SPLIT_SLASH) = 0;
  17. dir->base_path = shm_strdup(tmp);
  18. *strrchr(tmp, SPLIT_SLASH) = 0;
  19. dir->root_ver_path = shm_strdup(tmp);
  20. *strrchr(tmp, SPLIT_SLASH) = 0;
  21. dir->root_path = shm_strdup(tmp);
  22. *strrchr(tmp, SPLIT_SLASH) = 0;
  23. /*get current disk name.*/
  24. sp_dir_get_cur_drive(drivePath);
  25. dir->rvc_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "rvc", drivePath);
  26. #ifdef _WIN32
  27. dir->root_ver_path = shm_strdup_printf("%s\\version", dir->root_path);
  28. dir->root_hardwarecfg_path = shm_strdup_printf("%s\\hardwarecfg", dir->root_path);
  29. dir->root_runinfo_path = shm_strdup_printf("%s\\runinfo", dir->root_path);
  30. dir->dep_path = shm_strdup_printf("%s\\dep", dir->base_path);
  31. dir->cfg_path = shm_strdup_printf("%s\\cfg", dir->base_path);
  32. dir->mod_path = shm_strdup_printf("%s\\mod", dir->base_path);
  33. dir->obj_path = shm_strdup_printf("%s\\obj", dir->base_path);
  34. dir->dbg_path = shm_strdup_printf("%s\\dbg", dir->rvc_path);
  35. dir->slv_path = shm_strdup_printf("%s\\slv", dir->rvc_path);
  36. dir->dmp_path = shm_strdup_printf("%s\\dmp", dir->rvc_path);
  37. dir->ad0_path = shm_strdup_printf("%s\\ad0", dir->rvc_path);
  38. #else
  39. dir->root_ver_path = shm_strdup_printf("%s/version", dir->root_path);
  40. dir->root_hardwarecfg_path = shm_strdup_printf("%s/hardwarecfg", dir->root_path);
  41. dir->root_runinfo_path = shm_strdup_printf("%s/runinfo", dir->root_path);
  42. dir->dep_path = shm_strdup_printf("%s/dep", dir->base_path);
  43. dir->cfg_path = shm_strdup_printf("%s/cfg", dir->base_path);
  44. dir->mod_path = shm_strdup_printf("%s/mod", dir->base_path);
  45. dir->obj_path = shm_strdup_printf("%s/obj", dir->base_path);
  46. dir->dbg_path = shm_strdup_printf("%s/dbg", dir->rvc_path);
  47. dir->slv_path = shm_strdup_printf("%s/slv", dir->rvc_path);
  48. dir->dmp_path = shm_strdup_printf("%s/dmp", dir->rvc_path);
  49. dir->ad0_path = shm_strdup_printf("%s/ad0", dir->rvc_path);
  50. #endif //_WIN32
  51. /*if (!ExistsDirA(dir->obj_path)) {
  52. CreateDirRecursiveA(dir->obj_path);
  53. }*/
  54. if (!ExistsDirA(dir->rvc_path)) {
  55. CreateDirRecursiveA(dir->rvc_path);
  56. }
  57. if (!ExistsDirA(dir->dmp_path)) {
  58. CreateDirRecursiveA(dir->dmp_path);
  59. }
  60. if (!ExistsDirA(dir->dbg_path)) {
  61. CreateDirRecursiveA(dir->dbg_path);
  62. }
  63. if (!ExistsDirA(dir->slv_path)) {
  64. CreateDirRecursiveA(dir->slv_path);
  65. }
  66. memset(tmp, 0, sizeof(tmp));
  67. #ifdef _WIN32
  68. sprintf(tmp, "%s\\runcfg", dir->root_runinfo_path);
  69. #else
  70. sprintf(tmp, "%s/runcfg", dir->root_runinfo_path);
  71. #endif //_WIN32
  72. if (!ExistsDirA(tmp))
  73. CreateDirRecursiveA(tmp);
  74. *p_dir = dir;
  75. rc = 0;
  76. return rc;
  77. }
  78. void sp_dir_destroy(sp_dir_t* dir)
  79. {
  80. if (dir) {
  81. if (dir->base_path)
  82. shm_free(dir->base_path);
  83. if (dir->rvc_path)
  84. shm_free(dir->rvc_path);
  85. if (dir->bin_path)
  86. shm_free(dir->bin_path);
  87. if (dir->dep_path)
  88. shm_free(dir->dep_path);
  89. if (dir->cfg_path)
  90. shm_free(dir->cfg_path);
  91. if (dir->mod_path)
  92. shm_free(dir->mod_path);
  93. if (dir->obj_path)
  94. shm_free(dir->obj_path);
  95. if (dir->dbg_path)
  96. shm_free(dir->dbg_path);
  97. if (dir->slv_path)
  98. shm_free(dir->slv_path);
  99. if (dir->dmp_path)
  100. shm_free(dir->dmp_path);
  101. if (dir->root_path)
  102. shm_free(dir->root_path);
  103. if (dir->root_hardwarecfg_path)
  104. shm_free(dir->root_hardwarecfg_path);
  105. if (dir->root_runinfo_path)
  106. shm_free(dir->root_runinfo_path);
  107. if (dir->root_ver_path)
  108. shm_free(dir->root_ver_path);
  109. shm_free(dir);
  110. }
  111. }
  112. static int __dir_get_path(char *base_dir, int flag, const char *cat_name, char *buf, int len, const char *site)
  113. {
  114. int rc = 0;
  115. int needed = strlen(base_dir);
  116. if (flag == SP_DIR_ENTITY_INI) {
  117. needed += strlen(cat_name) + 6;
  118. } else if (flag == SP_DIR_SHELL_INI) {
  119. needed += 11;
  120. }else if (flag == SP_DIR_SHELLVAR_INI)
  121. {
  122. needed += 22;
  123. }else if (flag == SP_DIR_MODULE_BIN) {
  124. needed += strlen(cat_name) + 6;
  125. } else if (flag == SP_DIR_ROOT_INI) {
  126. needed += 10;
  127. } else if (flag == SP_DIR_DEVICE_ENTITY_INI) {
  128. needed += strlen(cat_name) + 6;
  129. } else if (flag == SP_DIR_RUNINFO_INI) {
  130. needed += strlen(cat_name) + 6 + 7;
  131. } else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  132. needed += strlen(cat_name) + 6 + 8;
  133. } else if (flag == SP_DIR_INSTALL_INI) {
  134. needed += 13;
  135. } else if (flag == SP_DIR_EXPLORER_INI) {
  136. needed += 16;
  137. } else if (flag == SP_DIR_CENTER_SETTING_INI){
  138. needed += 18;
  139. } else {
  140. rc = Error_Param;
  141. }
  142. if (buf) {
  143. if (len && len < needed)
  144. return Error_TooSmallBuffer;
  145. } else {
  146. return needed;
  147. }
  148. if (flag == SP_DIR_ENTITY_INI || flag == SP_DIR_DEVICE_ENTITY_INI)
  149. {
  150. strcpy(buf, base_dir);
  151. strcat(buf, SPLIT_SLASH_STR);
  152. strcat(buf, cat_name);
  153. strcat(buf, ".ini");
  154. }
  155. else if (flag == SP_DIR_RUNINFO_INI)
  156. {
  157. strcpy(buf, base_dir);
  158. strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR);
  159. strcat(buf, cat_name);
  160. strcat(buf, ".ini");
  161. }
  162. else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  163. strcpy(buf, base_dir);
  164. strcat(buf, SPLIT_SLASH_STR "bootrec" SPLIT_SLASH_STR);
  165. strcat(buf, cat_name);
  166. strcat(buf, ".log");
  167. }else if (flag == SP_DIR_SHELLVAR_INI){
  168. strcpy(buf, base_dir);
  169. strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "shellVar.ini");
  170. }else if (flag == SP_DIR_SHELL_INI) {
  171. strcpy(buf, base_dir);
  172. strcat(buf, SPLIT_SLASH_STR "shell.ini");
  173. } else if (flag == SP_DIR_MODULE_BIN) {
  174. strcpy(buf, base_dir);
  175. strcat(buf, SPLIT_SLASH_STR);
  176. #ifdef _WIN32
  177. strcat(buf, cat_name);
  178. strcat(buf, ".dll");
  179. #else
  180. strcat(buf, "lib");
  181. strcat(buf, cat_name);
  182. strcat(buf, ".so");
  183. #endif //_WIN32
  184. } else if (flag == SP_DIR_ROOT_INI) {
  185. strcpy(buf, base_dir);
  186. strcat(buf, SPLIT_SLASH_STR "root.ini");
  187. } else if (flag == SP_DIR_INSTALL_INI) {
  188. strcpy(buf, base_dir);
  189. strcat(buf, SPLIT_SLASH_STR "install.ini");
  190. }
  191. else if (flag == SP_DIR_CENTER_SETTING_INI)
  192. {
  193. // 为了兼容旧版本实体,此处仍返回原有配置名
  194. strcpy(buf, base_dir);
  195. strcat(buf, SPLIT_SLASH_STR "CenterSetting.ini");
  196. }
  197. return rc;
  198. }
  199. static int __dir_get_path_new(char *base_dir, int flag, const char *cat_name, char *buf, int len, const char *site)
  200. {
  201. int rc = 0;
  202. int needed = strlen(base_dir);
  203. if (flag == SP_DIR_ENTITY_INI) {
  204. needed += strlen(cat_name) + 6;
  205. } else if (flag == SP_DIR_SHELL_INI) {
  206. needed += 11;
  207. } else if (flag == SP_DIR_MODULE_BIN) {
  208. needed += strlen(cat_name) + 6;
  209. } else if (flag == SP_DIR_ROOT_INI) {
  210. needed += 10;
  211. } else if (flag == SP_DIR_DEVICE_ENTITY_INI) {
  212. needed += strlen(cat_name) + 6;
  213. } else if (flag == SP_DIR_RUNINFO_INI) {
  214. needed += strlen(cat_name) + 6 + 7;
  215. } else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  216. needed += strlen(cat_name) + 6 + 8;
  217. } else if (flag == SP_DIR_INSTALL_INI) {
  218. needed += 13;
  219. } else if (flag == SP_DIR_EXPLORER_INI) {
  220. needed += 16;
  221. } else if (flag == SP_DIR_CENTER_SETTING_INI){
  222. needed += 18;
  223. } else {
  224. rc = Error_Param;
  225. }
  226. if (buf) {
  227. if (len && len < needed)
  228. return Error_TooSmallBuffer;
  229. } else {
  230. return needed;
  231. }
  232. if (flag == SP_DIR_ENTITY_INI || flag == SP_DIR_DEVICE_ENTITY_INI)
  233. {
  234. strcpy(buf, base_dir);
  235. strcat(buf, SPLIT_SLASH_STR);
  236. strcat(buf, cat_name);
  237. strcat(buf, ".ini");
  238. }
  239. else if (flag == SP_DIR_RUNINFO_INI)
  240. {
  241. strcpy(buf, base_dir);
  242. strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR);
  243. strcat(buf, cat_name);
  244. strcat(buf, ".ini");
  245. }
  246. else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  247. strcpy(buf, base_dir);
  248. strcat(buf, SPLIT_SLASH_STR "bootrec" SPLIT_SLASH_STR);
  249. strcat(buf, cat_name);
  250. strcat(buf, ".log");
  251. } else if (flag == SP_DIR_SHELL_INI) {
  252. strcpy(buf, base_dir);
  253. strcat(buf, SPLIT_SLASH_STR "shell.ini");
  254. } else if (flag == SP_DIR_MODULE_BIN) {
  255. strcpy(buf, base_dir);
  256. strcat(buf, SPLIT_SLASH_STR);
  257. strcat(buf, cat_name);
  258. strcat(buf, ".dll");
  259. } else if (flag == SP_DIR_ROOT_INI) {
  260. strcpy(buf, base_dir);
  261. strcat(buf, SPLIT_SLASH_STR "root.ini");
  262. } else if (flag == SP_DIR_INSTALL_INI) {
  263. strcpy(buf, base_dir);
  264. strcat(buf, SPLIT_SLASH_STR "install.ini");
  265. }
  266. else if (flag == SP_DIR_CENTER_SETTING_INI)
  267. {
  268. // 为了兼容旧版本实体,此处仍返回原有配置名
  269. strcpy(buf, base_dir);
  270. if ('C' == base_dir[0])
  271. {
  272. strcat(buf, SPLIT_SLASH_STR "CenterSetting.DMZ.ini");
  273. }
  274. else
  275. {
  276. strcat(buf, SPLIT_SLASH_STR "CenterSetting.LAN.ini");
  277. }
  278. }
  279. return rc;
  280. }
  281. int sp_dir_get_path(sp_dir_t* dir, int flag, const char *cat_name, char *buf, int len)
  282. {
  283. char *base_path = NULL;
  284. if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
  285. base_path = dir->root_hardwarecfg_path;
  286. }
  287. else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
  288. base_path = dir->cfg_path;
  289. }
  290. else if (flag == SP_DIR_MODULE_BIN) {
  291. base_path = dir->mod_path;
  292. }
  293. else if (flag == SP_DIR_RUNINFO_INI || flag == SP_DIR_SHELLVAR_INI) {
  294. base_path = dir->root_runinfo_path;
  295. }
  296. else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  297. base_path = dir->root_runinfo_path;
  298. }
  299. else if (flag == SP_DIR_INSTALL_INI) {
  300. base_path = dir->base_path;
  301. }
  302. else if (flag == SP_DIR_EXPLORER_INI) {
  303. base_path = dir->root_path;
  304. }
  305. else if (flag == SP_DIR_CENTER_SETTING_INI) {
  306. base_path = dir->cfg_path;
  307. }
  308. else {
  309. return Error_Param;
  310. }
  311. return __dir_get_path(base_path, flag, cat_name, buf, len, NULL);
  312. }
  313. int sp_dir_get_path_new(sp_dir_t* dir, int flag, const char *cat_name, char *buf, int len)
  314. {
  315. char *base_path = NULL;
  316. if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
  317. base_path = dir->root_hardwarecfg_path;
  318. }
  319. else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
  320. base_path = dir->cfg_path;
  321. }
  322. else if (flag == SP_DIR_MODULE_BIN) {
  323. base_path = dir->mod_path;
  324. }
  325. else if (flag == SP_DIR_RUNINFO_INI) {
  326. base_path = dir->root_runinfo_path;
  327. }
  328. else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  329. base_path = dir->root_runinfo_path;
  330. }
  331. else if (flag == SP_DIR_INSTALL_INI) {
  332. base_path = dir->base_path;
  333. }
  334. else if (flag == SP_DIR_EXPLORER_INI) {
  335. base_path = dir->root_path;
  336. }
  337. else if (flag == SP_DIR_CENTER_SETTING_INI) {
  338. base_path = dir->cfg_path;
  339. }
  340. else {
  341. return Error_Param;
  342. }
  343. return __dir_get_path_new(base_path, flag, cat_name, buf, len, NULL);
  344. }
  345. 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)
  346. {
  347. char tmp[MAX_PATH];
  348. char *base_path = NULL;
  349. if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
  350. base_path = dir->root_hardwarecfg_path;
  351. } else if (flag == SP_DIR_RUNINFO_INI || flag == SP_DIR_RUNINFO_BOOT_LOG) {
  352. base_path = dir->root_runinfo_path;
  353. } else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
  354. sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d" SPLIT_SLASH_STR "cfg", dir->root_ver_path, major, minor, revision, build);
  355. base_path = tmp;
  356. } else if (flag == SP_DIR_MODULE_BIN) {
  357. sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d" SPLIT_SLASH_STR "mod", dir->root_ver_path, major, minor, revision, build);
  358. base_path = tmp;
  359. } else if (flag == SP_DIR_INSTALL_INI) {
  360. sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d", dir->root_ver_path, major, minor, revision, build);
  361. base_path = tmp;
  362. } else {
  363. return Error_Param;
  364. }
  365. return __dir_get_path(base_path, flag, cat_name, buf, len, NULL);
  366. }
  367. void sp_dir_get_cur_drive(char* path)
  368. {
  369. char drive[_MAX_DRIVE] = {'\0'};
  370. char dir[_MAX_DIR] = { '\0' };
  371. char fname[_MAX_FNAME] = { '\0' };
  372. char ext[_MAX_EXT] = { '\0' };
  373. char chpath[MAX_PATH] = { '\0' };
  374. GetModuleFileNameA(NULL, (LPSTR)chpath, sizeof(chpath));
  375. _splitpath(chpath, drive, dir, fname, ext);
  376. memcpy(path, drive, sizeof(drive));
  377. }