swdir.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: swdir.c
  4. * CONTENT: 常用的目录操作接口函数
  5. * NOTE:
  6. * HISTORY:
  7. * 1, [2010-12-24] created by NiuJiuRu
  8. ************************************************************************/
  9. #include "swapi.h"
  10. #include "swstring.h"
  11. #include "swfile.h"
  12. #include "swdir.h"
  13. /* 创建目录以及该目录前的所有父目录 */
  14. int sw_dir_create(const char *dir)
  15. {
  16. char path1[MAX_PATH_CHARS] = { 0 }, path2[MAX_PATH_CHARS] = { 0 };
  17. char *ptr1 = NULL, *ptr2 = NULL;
  18. int cnt = 0;
  19. if(!dir) return -1;
  20. strncpy(path1, dir, sizeof(path1)-1);
  21. path1[sizeof(path1) - 1] = '\0';
  22. xstrcharreplace(path1, '\\', '/');
  23. ptr1 = path1;
  24. while(ptr1 < path1+strlen(path1))
  25. {
  26. memset(path2, 0, sizeof(path2));
  27. ptr2 = strchr(ptr1, '/');
  28. if(ptr2) { memcpy(path2, path1, ptr2-path1 > 0 ? ptr2-path1 : 1); ptr1 = ptr2 + 1; }
  29. else { strcpy(path2, path1); ptr1 = path1+strlen(path1); }
  30. if(sw_dir_exists(path2)) continue;
  31. if(mkdir(path2, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) cnt++; // create a new directory success
  32. else break;
  33. }
  34. return cnt;
  35. }
  36. /* 删除目录以及该目录下的所有子目录和文件 */
  37. bool sw_dir_delete(const char *dir)
  38. {
  39. DIR *hDir = NULL; struct dirent *pData = NULL; struct stat statBuf = { 0 };
  40. char path[MAX_PATH_CHARS] = { 0 }; // full path(dir + entry name)
  41. bool success = true;
  42. hDir = (DIR *)sw_dir_open(dir);
  43. if(!hDir) return false;
  44. while(1)
  45. {
  46. pData = (struct dirent *)sw_dir_read(hDir);
  47. if(!pData) break;
  48. if(xstrcasecmp(pData->d_name, ".") == 0 || xstrcasecmp(pData->d_name, "..") == 0) continue;
  49. memset(path, 0, sizeof(path));
  50. strncpy(path, dir, sizeof(path)-1);
  51. xstrcharreplace(path, '\\', '/');
  52. if(strlen(path) > 0 && path[strlen(path)-1] != '/') strcat(path, "/");
  53. strcat(path, pData->d_name);
  54. memset(&statBuf, 0, sizeof(statBuf));
  55. if(lstat(path, &statBuf) == 0) // 用"lstat()"获取文件信息, 避免软链接等特殊文件导致的错误删除
  56. {
  57. if(S_ISDIR(statBuf.st_mode)) { if(!sw_dir_delete(path)) { success = false; break; } } // 目录
  58. else if(sw_file_delete(path) != 0) { success = false; break; } // 文件
  59. }
  60. }
  61. sw_dir_close(hDir);
  62. if(success && rmdir(dir) != 0) return false;
  63. return success;
  64. }
  65. /* 判断目录是否存在 */
  66. bool sw_dir_exists(const char *dir)
  67. {
  68. if(access(dir, 0) == 0) return true;
  69. else return false;
  70. }
  71. /* 打开目录 */
  72. void *sw_dir_open(const char *dir)
  73. {
  74. return opendir(dir);
  75. }
  76. /* 读目录内容, 返回NULL时表示结束 */
  77. void *sw_dir_read(void *hDir)
  78. {
  79. return readdir(hDir);
  80. }
  81. /* 关闭目录 */
  82. void sw_dir_close(void *hDir)
  83. {
  84. closedir(hDir);
  85. }
  86. /* 得到文件名 */
  87. char *xGetPathFileName(const char *path)
  88. {
  89. char *filename1 = NULL, *filename2 = NULL;
  90. filename1 = xstrcasechr(path, "right", '\\');
  91. filename2 = xstrcasechr(path, "right", '/');
  92. if(filename1 && filename2) return filename1 > filename2 ? filename1+1 : filename2+1;
  93. else if(filename1 && !filename2) return filename1+1;
  94. else if(!filename1 && filename2) return filename2+1;
  95. else return (char *)path;
  96. }
  97. /* 程序运行后, 得到其可执行文件所在的目录及对应文件名 */
  98. int xGetSelfRunningInfo(char *dir, char *bin)
  99. {
  100. char appModuleFile[MAX_PATH_CHARS] = { 0 };
  101. char *ptr = NULL;
  102. if(readlink("/proc/self/exe", appModuleFile, sizeof(appModuleFile)) < 0) return -1;
  103. ptr = xGetPathFileName(appModuleFile);
  104. if(dir) {
  105. memcpy(dir, appModuleFile, ptr-appModuleFile);
  106. dir[ptr-appModuleFile] = '\0';
  107. }
  108. if(bin) strcpy(bin, ptr);
  109. return 0;
  110. }