/src/lib/io/tools/file_tools.e

http://github.com/tybor/Liberty · Specman e · 303 lines · 227 code · 29 blank · 47 comment · 7 complexity · b29252a76d79d4f5cab22a2ec3332528 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class FILE_TOOLS
  5. -- This expanded class gather tools relatives to files such as
  6. -- file comparison, file renaming, file deletion, file size, file
  7. -- permissions...
  8. --
  9. -- Note this is a facilities class. Files are referenced with
  10. -- their names (as STRINGs). Consider using functions available in
  11. -- TEXT_FILE_READ if you are already connected to the file.
  12. insert
  13. ANY
  14. feature {ANY}
  15. same_files (path1, path2: ABSTRACT_STRING): BOOLEAN
  16. -- True if the `path1' file exists and has the very same content as file `path2'.
  17. require
  18. path1 /= Void
  19. path2 /= Void
  20. do
  21. tfr1.connect_to(path1)
  22. if tfr1.is_connected then
  23. tfr2.connect_to(path2)
  24. if tfr2.is_connected then
  25. Result := tfr1.same_as(tfr2)
  26. check
  27. tfr1_closed_by_same_as: not tfr1.is_connected
  28. tfr2_closed_by_same_as: not tfr2.is_connected
  29. end
  30. else
  31. tfr1.disconnect
  32. end
  33. end
  34. end
  35. same_physical_file (path1, path2: ABSTRACT_STRING): BOOLEAN
  36. -- True if `path1' and `path2' physically refer to the same file (e.g. symlinks to a same file will
  37. -- return True here)
  38. require
  39. path1 /= Void
  40. path2 /= Void
  41. do
  42. Result := io_same_physical_file(path1.to_external, path2.to_external)
  43. end
  44. file_exists (path: ABSTRACT_STRING): BOOLEAN
  45. require
  46. path /= Void
  47. path.count > 0
  48. do
  49. Result := io_file_exists(path.to_external)
  50. end
  51. is_readable (path: ABSTRACT_STRING): BOOLEAN
  52. -- True if `path' file exists and is either a readable file or an accessible directory.
  53. require
  54. path /= Void
  55. local
  56. bd: BASIC_DIRECTORY
  57. do
  58. if is_directory(path) then
  59. bd.connect_to(path)
  60. if bd.is_connected then
  61. Result := True
  62. bd.disconnect
  63. end
  64. else
  65. tfr1.connect_to(path)
  66. if tfr1.is_connected then
  67. Result := True
  68. tfr1.disconnect
  69. end
  70. end
  71. end
  72. is_empty (path: ABSTRACT_STRING): BOOLEAN
  73. -- True if `path' file exists, is readable and is an empty file.
  74. require
  75. path /= Void
  76. not path.is_empty
  77. do
  78. tfr1.connect_to(path)
  79. if tfr1.is_connected then
  80. tfr1.read_character
  81. Result := tfr1.end_of_input
  82. tfr1.disconnect
  83. end
  84. end
  85. rename_to (old_path, new_path: ABSTRACT_STRING)
  86. -- Try to change the name or the location of a file.
  87. require
  88. old_path /= Void
  89. new_path /= Void
  90. local
  91. p1, p2: POINTER
  92. do
  93. if file_exists(new_path) then
  94. delete(new_path)
  95. end
  96. p1 := old_path.to_external
  97. p2 := new_path.to_external
  98. last_rename_succeeded := io_rename(p1, p2)
  99. end
  100. last_rename_succeeded: BOOLEAN
  101. -- True if the last call to `rename_to` was successful.
  102. copy_to (source_path, target_path: ABSTRACT_STRING)
  103. -- Try to copy the source into the target.
  104. require
  105. source_path /= Void
  106. target_path /= Void
  107. local
  108. src, tgt: POINTER
  109. do
  110. src := source_path.to_external
  111. tgt := target_path.to_external
  112. last_copy_succeeded := io_copy(src, tgt)
  113. end
  114. last_copy_succeeded: BOOLEAN
  115. -- True if the last call to `copy_to` was successful.
  116. delete (path: ABSTRACT_STRING)
  117. -- Try to delete the given `path' file.
  118. require
  119. path /= Void
  120. local
  121. p: POINTER
  122. do
  123. p := path.to_external
  124. last_delete_succeeded := io_remove(p)
  125. end
  126. last_delete_succeeded: BOOLEAN
  127. -- True if the last call to `delete` was successful.
  128. size_of (path: ABSTRACT_STRING): INTEGER
  129. -- Total size of file `path' in number of bytes.
  130. -- When the corresponding file does not exists, the Result is negative.
  131. require
  132. path /= Void
  133. path.count > 0
  134. local
  135. p: POINTER
  136. do
  137. p := path.to_external
  138. Result := fstat_st_size(p)
  139. end
  140. last_change_of (path: ABSTRACT_STRING): TIME
  141. -- Of the last modification of `path'.
  142. require
  143. path /= Void
  144. path.count > 0
  145. local
  146. p: POINTER; time_memory: INTEGER_64
  147. do
  148. p := path.to_external
  149. time_memory := fstat_st_mtime(p)
  150. Result.set_time_memory(time_memory)
  151. end
  152. is_file (path: ABSTRACT_STRING): BOOLEAN
  153. -- Is `path' a regular file?
  154. require
  155. path /= Void
  156. path.count > 0
  157. do
  158. Result := fstat_st_is_file(path.to_external)
  159. end
  160. is_directory (path: ABSTRACT_STRING): BOOLEAN
  161. -- Is `path' a directory?
  162. require
  163. path /= Void
  164. path.count > 0
  165. do
  166. Result := fstat_st_is_dir(path.to_external)
  167. end
  168. feature {}
  169. io_remove (path: POINTER): BOOLEAN
  170. -- To implement `delete'.
  171. external "plug_in"
  172. alias "{
  173. location: "${sys}/plugins"
  174. module_name: "io"
  175. feature_name: "io_remove"
  176. }"
  177. end
  178. io_rename (old_path, new_path: POINTER): BOOLEAN
  179. external "plug_in"
  180. alias "{
  181. location: "${sys}/plugins"
  182. module_name: "io"
  183. feature_name: "io_rename"
  184. }"
  185. end
  186. io_copy (source, target: POINTER): BOOLEAN
  187. external "plug_in"
  188. alias "{
  189. location: "${sys}/plugins"
  190. module_name: "io"
  191. feature_name: "io_copy"
  192. }"
  193. end
  194. io_file_exists (path: POINTER): BOOLEAN
  195. external "plug_in"
  196. alias "{
  197. location: "${sys}/plugins"
  198. module_name: "io"
  199. feature_name: "io_file_exists"
  200. }"
  201. end
  202. io_same_physical_file (path1, path2: POINTER): BOOLEAN
  203. external "plug_in"
  204. alias "{
  205. location: "${sys}/plugins"
  206. module_name: "io"
  207. feature_name: "io_same_physical_file"
  208. }"
  209. end
  210. fstat_st_size (path: POINTER): INTEGER
  211. external "plug_in"
  212. alias "{
  213. location: "${sys}/plugins/io"
  214. module_name: "fstat"
  215. feature_name: "fstat_st_size"
  216. }"
  217. end
  218. fstat_st_mtime (path: POINTER): INTEGER_64
  219. external "plug_in"
  220. alias "{
  221. location: "${sys}/plugins/io"
  222. module_name: "fstat"
  223. feature_name: "fstat_st_mtime"
  224. }"
  225. end
  226. fstat_st_is_file (path: POINTER): BOOLEAN
  227. external "plug_in"
  228. alias "{
  229. location: "${sys}/plugins/io"
  230. module_name: "fstat"
  231. feature_name: "fstat_st_is_file"
  232. }"
  233. end
  234. fstat_st_is_dir (path: POINTER): BOOLEAN
  235. external "plug_in"
  236. alias "{
  237. location: "${sys}/plugins/io"
  238. module_name: "fstat"
  239. feature_name: "fstat_st_is_dir"
  240. }"
  241. end
  242. tfr1: TEXT_FILE_READ
  243. once
  244. create Result.make
  245. end
  246. tfr2: TEXT_FILE_READ
  247. once
  248. create Result.make
  249. end
  250. tmp_string: STRING
  251. once
  252. create Result.make(256)
  253. end
  254. end -- class FILE_TOOLS
  255. --
  256. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  257. --
  258. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  259. -- of this software and associated documentation files (the "Software"), to deal
  260. -- in the Software without restriction, including without limitation the rights
  261. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  262. -- copies of the Software, and to permit persons to whom the Software is
  263. -- furnished to do so, subject to the following conditions:
  264. --
  265. -- The above copyright notice and this permission notice shall be included in
  266. -- all copies or substantial portions of the Software.
  267. --
  268. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  269. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  270. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  271. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  272. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  273. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  274. -- THE SOFTWARE.