swdir.c 3.5 KB

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