/platform/unix/scaffold/directory.d

http://github.com/wilkie/djehuty · D · 231 lines · 158 code · 57 blank · 16 comment · 32 complexity · 69e086a9bc4b5eece184f889677b4d25 MD5 · raw file

  1. /*
  2. * directory.d
  3. *
  4. * This Scaffold holds the Directory implementations for the Linux platform
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module scaffold.directory;
  10. import platform.unix.common;
  11. import binding.c;
  12. import platform.vars.directory;
  13. import platform.vars.file;
  14. import io.file;
  15. import io.console;
  16. import core.definitions;
  17. import core.string;
  18. import core.main;
  19. import data.list;
  20. bool DirectoryOpen(ref DirectoryPlatformVars dirVars, ref string path) {
  21. string pn;
  22. if (path == "") {
  23. pn = "/";
  24. }
  25. else {
  26. pn = path.dup;
  27. }
  28. pn ~= '\0';
  29. dirVars.dir = opendir(pn.ptr);
  30. return (dirVars.dir !is null);
  31. }
  32. bool DirectoryCreate(ref DirectoryPlatformVars dirVars, ref string path) {
  33. if (DirectoryFileIsDir(path)) {
  34. return false;
  35. }
  36. string makedir = "mkdir " ~ path ~ "\0";
  37. system(makedir.ptr);
  38. return DirectoryOpen(dirVars,path);
  39. }
  40. bool DirectoryClose(ref DirectoryPlatformVars dirVars) {
  41. closedir(dirVars.dir);
  42. return true;
  43. }
  44. string DirectoryGetBinary() {
  45. return "/usr/bin";
  46. }
  47. string DirectoryGetAppData() {
  48. return "/usr/share/" ~ Djehuty.app.name;
  49. }
  50. string DirectoryGetTempData() {
  51. return "/tmp/djp" ~ toStr(getpid());
  52. }
  53. string DirectoryGetUserData() {
  54. static string cached;
  55. // user data: $HOME/.{appname}
  56. if (cached is null) {
  57. char* result;
  58. result = getenv("HOME\0"c.ptr);
  59. char[] homePath;
  60. char* cur = result;
  61. int i;
  62. for(i = 0; *cur != '\0'; cur++, i++) {}
  63. if (i != 0) {
  64. homePath = result[0..i];
  65. cached = homePath ~ "/." ~ Djehuty.app.name;
  66. }
  67. else {
  68. cached = "";
  69. }
  70. }
  71. return cached;
  72. }
  73. string DirectoryGetApp() {
  74. // Store result
  75. static string cached = null;
  76. if (cached is null) {
  77. string procPath = "/proc/" ~ toStr(getpid()) ~ "/exe\0";
  78. size_t ret = -1;
  79. int len = 256;
  80. char[] path;
  81. while (ret == -1) {
  82. path = new char[len];
  83. ret = readlink(procPath.ptr, path.ptr, len-1);
  84. len <<= 1;
  85. if (ret == -1 && len > 32000) {
  86. // Error, path is too long
  87. cached = "";
  88. return cached.dup;
  89. }
  90. }
  91. cached = path[0..ret].dup;
  92. }
  93. return cached.dup;
  94. }
  95. string DirectoryGetCWD() {
  96. uint len = 512;
  97. char[] chrs;
  98. char* ptr;
  99. do {
  100. chrs = new char[len+1];
  101. ptr = getcwd(chrs.ptr, len);
  102. len <<= 1;
  103. } while (ptr is null);
  104. foreach (int i, chr; chrs) {
  105. if (chr == '\0') {
  106. chrs = chrs[0..i];
  107. break;
  108. }
  109. }
  110. return chrs.dup;
  111. }
  112. bool DirectoryFileIsDir(string path) {
  113. string newPath = path.dup;
  114. newPath ~= '\0';
  115. struct_stat inode;
  116. if (stat(newPath.ptr, &inode) != -1) {
  117. if (S_ISDIR(inode.st_mode)) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. bool DirectoryMove(ref string path, string newPath) {
  124. string exec = "mv " ~ path ~ " " ~ newPath ~ "\0";
  125. system(exec.ptr);
  126. return true;
  127. }
  128. bool DirectoryCopy(ref string path, string newPath) {
  129. string exec = "cp -r " ~ path ~ " " ~ newPath ~ "\0";
  130. system(exec.ptr);
  131. return true;
  132. }
  133. bool DirectoryRename(ref string path, ref string newName) {
  134. string npath = path.dup;
  135. npath ~= '\0';
  136. string str;
  137. foreach_reverse(int i, chr; path) {
  138. if (chr == '/') {
  139. // truncate
  140. str = path[0..(i+1)].dup;
  141. break;
  142. }
  143. }
  144. if (str is null) { return false; }
  145. str ~= newName;
  146. str ~= '\0';
  147. rename(npath.ptr, str.ptr);
  148. return true;
  149. }
  150. string[] DirectoryList(ref DirectoryPlatformVars dirVars, ref string path) {
  151. if (!DirectoryOpen(dirVars, path)) { return null; }
  152. dirent* dir;
  153. string[] list;
  154. // Retrieve first directory
  155. dir = readdir(dirVars.dir);
  156. while(dir !is null) {
  157. // Caculate Length of d_name
  158. int len;
  159. foreach(chr; dir.d_name) {
  160. if (chr == '\0') {
  161. break;
  162. }
  163. len++;
  164. }
  165. // Add to list
  166. if (dir.d_name[0..len] != "." && dir.d_name[0..len] != "..") {
  167. list ~= dir.d_name[0..len].dup;
  168. }
  169. // Retrieve next item in the directory
  170. dir = readdir(dirVars.dir);
  171. }
  172. DirectoryClose(dirVars);
  173. return list;
  174. }