Newer
Older
dub_jkp / source / dub / internal / io / filesystem.d
  1. /**
  2. * An abstract filesystem representation
  3. *
  4. * This interface allows to represent the file system to various part of Dub.
  5. * Instead of direct use of `std.file`, an implementation of this interface can
  6. * be used, allowing to mock all I/O in unittest on a thread-local basis.
  7. */
  8. module dub.internal.io.filesystem;
  9.  
  10. public import std.datetime.systime;
  11.  
  12. public import dub.internal.vibecompat.inet.path;
  13.  
  14. /// Ditto
  15. public interface Filesystem
  16. {
  17. static import dub.internal.vibecompat.core.file;
  18.  
  19. /// TODO: Remove, the API should be improved
  20. public alias IterateDirDg = int delegate(
  21. scope int delegate(ref dub.internal.vibecompat.core.file.FileInfo));
  22.  
  23. /// Ditto
  24. public IterateDirDg iterateDirectory (in NativePath path) scope;
  25.  
  26. /// Returns: The `path` of this FSEntry
  27. public abstract NativePath getcwd () const scope;
  28.  
  29. /**
  30. * Implements `mkdir -p`: Create a directory and every intermediary
  31. *
  32. * There is no way to error out on intermediate directory,
  33. * like standard mkdir does. If you want this behavior,
  34. * simply check (`existsDirectory`) if the parent directory exists.
  35. *
  36. * Params:
  37. * path = The path of the directory to be created.
  38. */
  39. public abstract void mkdir (in NativePath path) scope;
  40.  
  41. /// Checks the existence of a file
  42. public abstract bool existsFile (in NativePath path) const scope;
  43.  
  44. /// Checks the existence of a directory
  45. public abstract bool existsDirectory (in NativePath path) const scope;
  46.  
  47. /// Reads a file, returns the content as `ubyte[]`
  48. public abstract ubyte[] readFile (in NativePath path) const scope;
  49.  
  50. /// Reads a file, returns the content as text
  51. public abstract string readText (in NativePath path) const scope;
  52.  
  53. /// Write to this file
  54. public final void writeFile (in NativePath path, const(char)[] data) scope
  55. {
  56. import std.string : representation;
  57.  
  58. this.writeFile(path, data.representation);
  59. }
  60.  
  61. /// Ditto
  62. public abstract void writeFile (in NativePath path, const(ubyte)[] data) scope;
  63.  
  64. /** Remove a file
  65. *
  66. * Always error if the target is a directory.
  67. * Does not error if the target does not exists
  68. * and `force` is set to `true`.
  69. *
  70. * Params:
  71. * path = Path to the file to remove
  72. * force = Whether to ignore non-existing file,
  73. * default to `false`.
  74. */
  75. public void removeFile (in NativePath path, bool force = false);
  76.  
  77. /** Remove a directory
  78. *
  79. * Remove an existing empty directory.
  80. * If `force` is set to `true`, no error will be thrown
  81. * if the directory is empty or non-existing.
  82. *
  83. * Params:
  84. * path = Path to the directory to remove
  85. * force = Whether to ignore non-existing / non-empty directories,
  86. * default to `false`.
  87. */
  88. public void removeDir (in NativePath path, bool force = false);
  89.  
  90. /// Implement `std.file.setTimes`
  91. public void setTimes (in NativePath path, in SysTime accessTime,
  92. in SysTime modificationTime);
  93.  
  94. /// Implement `std.file.setAttributes`
  95. public void setAttributes (in NativePath path, uint attributes);
  96. }