removeFiles.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. remove.c - Remove files safely on Windows
  3. Copyright (c) All Rights Reserved. See details at the end of the file.
  4. */
  5. /********************************* Includes ***********************************/
  6. /*
  7. Suppress MS VS warnings
  8. */
  9. #define _CRT_SECURE_NO_WARNINGS
  10. /* Work-around to allow the windows 7.* SDK to be used with VS 2012 */
  11. #if _MSC_VER >= 1700
  12. #define SAL_SUPP_H
  13. #define SPECSTRING_SUPP_H
  14. #endif
  15. #include "me.h"
  16. #include <stdio.h>
  17. #include <direct.h>
  18. #include <windows.h>
  19. /*********************************** Locals ***********************************/
  20. #define PROGRAM ME_NAME " Removal Program"
  21. #define MPR_MAX_FNAME 1024
  22. static char *fileList[] = {
  23. "appWeb.conf",
  24. "*.obj",
  25. "*.lib",
  26. "*.dll",
  27. "*.pdb",
  28. "*.exe",
  29. "*.def",
  30. "*.exp",
  31. "*.idb",
  32. "*.plg",
  33. "*.res",
  34. "*.ncb",
  35. "*.opt",
  36. "*.bak",
  37. "*.0",
  38. "*.1",
  39. "*.2",
  40. "*.3",
  41. "*.4",
  42. "*.5",
  43. "*.6",
  44. "*.7",
  45. "*.8",
  46. "*.9",
  47. "make.dep",
  48. "install.log",
  49. ".port.log",
  50. 0
  51. };
  52. /***************************** Forward Declarations ***************************/
  53. static int initWindow();
  54. static void cleanup();
  55. static void recursiveRemove(char *dir, char *pattern);
  56. static int match(char *file, char *pat);
  57. static char *mprGetDirName(char *buf, int bufsize, const char *path);
  58. static int mprStrcpy(char *dest, int destMax, const char *src);
  59. /*********************************** Code *************************************/
  60. int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2)
  61. {
  62. char dir[MPR_MAX_FNAME], moduleBuf[MPR_MAX_FNAME], tmp[MPR_MAX_FNAME];
  63. char *cp;
  64. int errflg, sleepMsecs, removeOk;
  65. errflg = 0;
  66. sleepMsecs = 0;
  67. removeOk = 0;
  68. /*
  69. Get the directory above bin
  70. */
  71. GetModuleFileName(0, moduleBuf, sizeof(moduleBuf) - 1);
  72. mprGetDirName(tmp, sizeof(tmp), moduleBuf);
  73. mprGetDirName(dir, sizeof(dir), tmp);
  74. _chdir(dir);
  75. if (args && *args) {
  76. if (strstr(args, "-r") != 0) {
  77. removeOk++;
  78. }
  79. if ((cp = strstr(args, "-s")) != 0) {
  80. do {
  81. cp++;
  82. } while (isspace(*cp));
  83. sleepMsecs = atoi(cp) * 1000;
  84. }
  85. }
  86. /*
  87. We use removeOk to ensure that someone just running the program won't do anything bad.
  88. */
  89. if (errflg || !removeOk) {
  90. fprintf(stderr, "Bad Usage");
  91. return FALSE;
  92. }
  93. cleanup();
  94. /*
  95. Some products (services) take a while to exit. This is a convenient way to pause before removing
  96. */
  97. if (sleepMsecs) {
  98. printf("sleeping for %d msec\n", sleepMsecs);
  99. Sleep(sleepMsecs);
  100. }
  101. return 0;
  102. }
  103. /*
  104. Cleanup temporary files
  105. */
  106. static void cleanup()
  107. {
  108. char *file;
  109. char home[MPR_MAX_FNAME];
  110. int i;
  111. _getcwd(home, sizeof(home) - 1);
  112. for (i = 0; fileList[i]; i++) {
  113. file = fileList[i];
  114. recursiveRemove(home, file);
  115. }
  116. }
  117. /*
  118. Remove a file
  119. */
  120. static void recursiveRemove(char *dir, char *pattern)
  121. {
  122. HANDLE handle;
  123. WIN32_FIND_DATA data;
  124. char saveDir[MPR_MAX_FNAME];
  125. saveDir[sizeof(saveDir) - 1] = '\0';
  126. _getcwd(saveDir, sizeof(saveDir) - 1);
  127. _chdir(dir);
  128. handle = FindFirstFile("*.*", &data);
  129. while (FindNextFile(handle, &data)) {
  130. if (strcmp(data.cFileName, "..") == 0 ||
  131. strcmp(data.cFileName, ".") == 0) {
  132. continue;
  133. }
  134. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  135. recursiveRemove(data.cFileName, pattern);
  136. /*
  137. This will fail if there are files remaining in the directory.
  138. */
  139. printf("Removing directory %s\n", data.cFileName);
  140. RemoveDirectory(data.cFileName);
  141. continue;
  142. }
  143. if (match(data.cFileName, pattern)) {
  144. printf("Delete: %s\n", data.cFileName);
  145. DeleteFile(data.cFileName);
  146. }
  147. }
  148. FindClose(handle);
  149. _chdir(saveDir);
  150. }
  151. /*
  152. Simple wild-card matching
  153. */
  154. static int match(char *file, char *pat)
  155. {
  156. char fileBuf[MPR_MAX_FNAME], patBuf[MPR_MAX_FNAME];
  157. char *patExt;
  158. char *fileExt;
  159. mprStrcpy(fileBuf, sizeof(fileBuf), file);
  160. file = fileBuf;
  161. mprStrcpy(patBuf, sizeof(patBuf), pat);
  162. pat = patBuf;
  163. if (strcmp(file, pat) == 0) {
  164. return 1;
  165. }
  166. if ((fileExt = strrchr(file, '.')) != 0) {
  167. *fileExt++ = '\0';
  168. }
  169. if ((patExt = strrchr(pat, '.')) != 0) {
  170. *patExt++ = '\0';
  171. }
  172. if (*pat == '*' || strcmp(pat, file) == 0) {
  173. if (patExt && *patExt == '*') {
  174. return 1;
  175. } else {
  176. if (fileExt && strcmp(fileExt, patExt) == 0) {
  177. return 1;
  178. }
  179. }
  180. }
  181. return 0;
  182. }
  183. /*
  184. Return the directory portion of a pathname into the users buffer.
  185. */
  186. static char *mprGetDirName(char *buf, int bufsize, const char *path)
  187. {
  188. char *cp;
  189. int dlen;
  190. cp = strrchr(path, '/');
  191. if (cp == 0) {
  192. #if ME_WIN_LIKE
  193. cp = strrchr(path, '\\');
  194. if (cp == 0)
  195. #endif
  196. {
  197. buf[0] = '\0';
  198. return buf;
  199. }
  200. }
  201. if (cp == path && cp[1] == '\0') {
  202. strcpy(buf, ".");
  203. return buf;
  204. }
  205. dlen = (int) (cp - path);
  206. if (dlen < bufsize) {
  207. if (dlen == 0) {
  208. dlen++;
  209. }
  210. memcpy(buf, path, dlen);
  211. buf[dlen] = '\0';
  212. return buf;
  213. }
  214. return 0;
  215. }
  216. static int mprStrcpy(char *dest, int destMax, const char *src)
  217. {
  218. int len;
  219. len = (int) strlen(src);
  220. if (destMax > 0 && len >= destMax && len > 0) {
  221. return -1;
  222. }
  223. if (len > 0) {
  224. memcpy(dest, src, len);
  225. dest[len] = '\0';
  226. } else {
  227. *dest = '\0';
  228. len = 0;
  229. }
  230. return len;
  231. }
  232. /*
  233. @copy default
  234. Copyright (c) Embedthis Software LLC, 2003-2014. All Rights Reserved.
  235. This software is distributed under commercial and open source licenses.
  236. You may use the Embedthis Open Source license or you may acquire a
  237. commercial license from Embedthis Software. You agree to be fully bound
  238. by the terms of either license. Consult the LICENSE.md distributed with
  239. this software for full details and other copyrights.
  240. Local variables:
  241. tab-width: 4
  242. c-basic-offset: 4
  243. End:
  244. vim: sw=4 ts=4 expandtab
  245. @end
  246. */