path.exists
This commit is contained in:
parent
0055b8f6cb
commit
350cd98661
3 changed files with 32 additions and 18 deletions
|
@ -2,6 +2,7 @@ module internal Pentole.Native
|
||||||
|
|
||||||
open System
|
open System
|
||||||
open System.Runtime.InteropServices
|
open System.Runtime.InteropServices
|
||||||
|
open System.Runtime.CompilerServices
|
||||||
|
|
||||||
|
|
||||||
[<DllImport("libc", EntryPoint = "realpath", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)>]
|
[<DllImport("libc", EntryPoint = "realpath", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)>]
|
||||||
|
@ -10,7 +11,6 @@ extern nativeint _realpath (string _path, nativeint _resolved_path)
|
||||||
[<DllImport("libc", EntryPoint = "strerror", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)>]
|
[<DllImport("libc", EntryPoint = "strerror", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)>]
|
||||||
extern nativeint _strerror (int _errnum)
|
extern nativeint _strerror (int _errnum)
|
||||||
|
|
||||||
|
|
||||||
let realpath (path: string) =
|
let realpath (path: string) =
|
||||||
let ptr = _realpath(path, IntPtr.Zero)
|
let ptr = _realpath(path, IntPtr.Zero)
|
||||||
if ptr = IntPtr.Zero then
|
if ptr = IntPtr.Zero then
|
||||||
|
|
|
@ -2,8 +2,8 @@ namespace Pentole
|
||||||
|
|
||||||
open System
|
open System
|
||||||
|
|
||||||
|
/// don't use
|
||||||
module Internal =
|
module PathInternal =
|
||||||
let private invalid_chars = System.IO.Path.GetInvalidPathChars ()
|
let private invalid_chars = System.IO.Path.GetInvalidPathChars ()
|
||||||
|
|
||||||
let trim (a: string) =
|
let trim (a: string) =
|
||||||
|
@ -28,10 +28,17 @@ module Internal =
|
||||||
|
|
||||||
type IPath =
|
type IPath =
|
||||||
abstract member ToString: unit -> string
|
abstract member ToString: unit -> string
|
||||||
|
/// The string value of the path. Mainly useful for C# interop.
|
||||||
abstract member string_value: string
|
abstract member string_value: string
|
||||||
inherit IEquatable<IPath>
|
inherit IEquatable<IPath>
|
||||||
inherit IComparable
|
inherit IComparable
|
||||||
|
|
||||||
|
/// Specifies a location in the file system from the root
|
||||||
|
/// directory. It is not dependent on the current working directory
|
||||||
|
/// and provides a complete path to a file or directory. Note that an
|
||||||
|
/// `AbsolutePath` does not have to correspond to an actual file or
|
||||||
|
/// directory on the file system. To verify its existence or resolve
|
||||||
|
/// it to a real file, additional methods in the FileSystem module are provided
|
||||||
[<Struct>]
|
[<Struct>]
|
||||||
[<CustomEquality>]
|
[<CustomEquality>]
|
||||||
[<CustomComparison>]
|
[<CustomComparison>]
|
||||||
|
@ -50,8 +57,15 @@ type AbsolutePath internal (path: string) =
|
||||||
|
|
||||||
interface IEquatable<IPath> with
|
interface IEquatable<IPath> with
|
||||||
member _.Equals (o: IPath) =
|
member _.Equals (o: IPath) =
|
||||||
Internal.trim o.string_value = path
|
PathInternal.trim o.string_value = path
|
||||||
|
|
||||||
|
/// Specifies a location in the file system relative to the current
|
||||||
|
/// working directory. It does not provide a complete path and is
|
||||||
|
/// dependent on the context in which it is used. Similar to
|
||||||
|
/// `AbsolutePath`, a `RelativePath` does not have to correspond to an
|
||||||
|
/// actual file or directory. To verify its existence or resolve
|
||||||
|
/// it to a real file, additional methods in the FileSystem module are provided
|
||||||
|
/// should be used.
|
||||||
[<Struct>]
|
[<Struct>]
|
||||||
[<CustomEquality>]
|
[<CustomEquality>]
|
||||||
[<CustomComparison>]
|
[<CustomComparison>]
|
||||||
|
@ -69,11 +83,12 @@ type RelativePath internal (path: string) =
|
||||||
|
|
||||||
interface IEquatable<IPath> with
|
interface IEquatable<IPath> with
|
||||||
member _.Equals (o: IPath) =
|
member _.Equals (o: IPath) =
|
||||||
Internal.trim o.string_value = path
|
PathInternal.trim o.string_value = path
|
||||||
|
|
||||||
module Path =
|
module Path =
|
||||||
|
/// Construct a IPath object from the given path.
|
||||||
let of_string (path: string): Result<IPath, string> =
|
let of_string (path: string): Result<IPath, string> =
|
||||||
match Internal.construct path with
|
match PathInternal.construct path with
|
||||||
| Ok (path, true) -> AbsolutePath path :> IPath |> Ok
|
| Ok (path, true) -> AbsolutePath path :> IPath |> Ok
|
||||||
| Ok (path, false) -> RelativePath path :> IPath |> Ok
|
| Ok (path, false) -> RelativePath path :> IPath |> Ok
|
||||||
| Error e -> Error e
|
| Error e -> Error e
|
||||||
|
@ -121,13 +136,6 @@ module Path =
|
||||||
/// Returns the parent directory of the given path.
|
/// Returns the parent directory of the given path.
|
||||||
///
|
///
|
||||||
/// If the path is the root directory or does not have a parent, the original path is returned.
|
/// If the path is the root directory or does not have a parent, the original path is returned.
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `path`: The path to get the parent directory of.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// The parent directory of the given path, or the original path if it has no parent
|
|
||||||
let parent (path: IPath) =
|
let parent (path: IPath) =
|
||||||
let parent = IO.Path.GetDirectoryName path.string_value
|
let parent = IO.Path.GetDirectoryName path.string_value
|
||||||
if parent = null then
|
if parent = null then
|
||||||
|
@ -135,12 +143,12 @@ module Path =
|
||||||
else
|
else
|
||||||
parent |> should_not_fail
|
parent |> should_not_fail
|
||||||
|
|
||||||
|
|
||||||
// impure functions
|
|
||||||
// https://docs.python.org/3.8/library/pathlib.html
|
|
||||||
module FileSystem =
|
module FileSystem =
|
||||||
let resolve (path: IPath) =
|
let resolve (path: IPath) =
|
||||||
Native.realpath path.string_value |> Result.bind Path.of_string
|
Native.realpath path.string_value |> Result.bind Path.of_string
|
||||||
|
|
||||||
|
let exists (path: IPath) =
|
||||||
|
IO.Path.Exists path.string_value
|
||||||
(*
|
(*
|
||||||
|
|
||||||
let relative_to (parent: Path) (child: Path) =
|
let relative_to (parent: Path) (child: Path) =
|
||||||
|
|
|
@ -75,3 +75,9 @@ let parent_test () =
|
||||||
"/etc/conf" |> test |> Assert.are_equal (p "/etc")
|
"/etc/conf" |> test |> Assert.are_equal (p "/etc")
|
||||||
"/etc/../etc" |> test |> Assert.are_equal (p "/etc/../")
|
"/etc/../etc" |> test |> Assert.are_equal (p "/etc/../")
|
||||||
"etc/../etc" |> test |> Assert.are_equal (p "etc/../")
|
"etc/../etc" |> test |> Assert.are_equal (p "etc/../")
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let exitsts_test () =
|
||||||
|
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
|
||||||
|
FileSystem.exists (p "/tmp/") |> Assert.is_true
|
||||||
|
FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> Assert.is_false
|
||||||
|
|
Loading…
Reference in a new issue