Skip to content

Commit

Permalink
Refactor interop methods into separate partial class files
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed May 15, 2024
1 parent 21c7196 commit 2623a01
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 62 deletions.
28 changes: 2 additions & 26 deletions src/Lucene.Net/Support/IO/PosixFsyncSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using static Lucene.Net.Support.Native.Interop.Posix;
using static Lucene.Net.Support.Native.Interop.MacOS;

namespace Lucene.Net.Support.IO
{
Expand All @@ -24,32 +26,6 @@ namespace Lucene.Net.Support.IO

internal static class PosixFsyncSupport
{
// https://pubs.opengroup.org/onlinepubs/009695399/functions/fsync.html
[DllImport("libc", SetLastError = true)]
private static extern int fsync(int fd);

// https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html
[DllImport("libc", SetLastError = true)]
private static extern int open([MarshalAs(UnmanagedType.LPStr)] string pathname, int flags);

// https://pubs.opengroup.org/onlinepubs/009604499/functions/close.html
[DllImport("libc", SetLastError = true)]
private static extern int close(int fd);

// https://pubs.opengroup.org/onlinepubs/007904975/functions/fcntl.html
// and https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html
[DllImport("libc", SetLastError = true)]
private static extern int fcntl(int fd, int cmd, int arg);

private const int O_RDONLY = 0;
private const int O_WRONLY = 1;

// https://opensource.apple.com/source/xnu/xnu-6153.81.5/bsd/sys/fcntl.h.auto.html
private const int F_FULLFSYNC = 51;

private const int EACCES = 13;
private const int ENOENT = 2;

public static void Fsync(string path, bool isDir)
{
using DescriptorWrapper handle = new DescriptorWrapper(path, isDir);
Expand Down
37 changes: 1 addition & 36 deletions src/Lucene.Net/Support/IO/WindowsFsyncSupport.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using static Lucene.Net.Support.Native.Interop.Win32;

namespace Lucene.Net.Support.IO
{
Expand All @@ -23,42 +24,6 @@ namespace Lucene.Net.Support.IO

public static class WindowsFsyncSupport
{
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr CreateFileW(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile
);

// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FlushFileBuffers(IntPtr hFile);

// https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);

private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

// https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
private const int ERROR_FILE_NOT_FOUND = 2;
private const int ERROR_PATH_NOT_FOUND = 3;
private const int ERROR_ACCESS_DENIED = 5;

// https://learn.microsoft.com/en-us/windows/win32/secauthz/generic-access-rights
private const int GENERIC_WRITE = 0x40000000;

private const int FILE_SHARE_READ = 0x00000001;
private const int FILE_SHARE_WRITE = 0x00000002;
private const int FILE_SHARE_DELETE = 0x00000004;
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
private const int OPEN_EXISTING = 3;

public static void Fsync(string path, bool isDir)
{
using HandleWrapper handle = new HandleWrapper(path, isDir);
Expand Down
28 changes: 28 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.MacOS.Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class MacOS
{
// https://opensource.apple.com/source/xnu/xnu-6153.81.5/bsd/sys/fcntl.h.auto.html
internal const int F_FULLFSYNC = 51;
}
}
}
31 changes: 31 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Posix.Close.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Runtime.InteropServices;

namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Posix
{
// https://pubs.opengroup.org/onlinepubs/009604499/functions/close.html
[DllImport("libc", SetLastError = true)]
internal static extern int close(int fd);
}
}
}
31 changes: 31 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Posix.Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Posix
{
internal const int O_RDONLY = 0;
internal const int O_WRONLY = 1;

internal const int EACCES = 13;
internal const int ENOENT = 2;
}
}
}
32 changes: 32 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Posix.Fcntl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Runtime.InteropServices;

namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Posix
{
// https://pubs.opengroup.org/onlinepubs/007904975/functions/fcntl.html
// and https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html
[DllImport("libc", SetLastError = true)]
internal static extern int fcntl(int fd, int cmd, int arg);
}
}
}
31 changes: 31 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Posix.Fsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Runtime.InteropServices;

namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Posix
{
// https://pubs.opengroup.org/onlinepubs/009695399/functions/fsync.html
[DllImport("libc", SetLastError = true)]
internal static extern int fsync(int fd);
}
}
}
31 changes: 31 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Posix.Open.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Runtime.InteropServices;

namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Posix
{
// https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html
[DllImport("libc", SetLastError = true)]
internal static extern int open([MarshalAs(UnmanagedType.LPStr)] string pathname, int flags);
}
}
}
32 changes: 32 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Win32.CloseHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Runtime.InteropServices;

namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Win32
{
// https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool CloseHandle(IntPtr hObject);
}
}
}
43 changes: 43 additions & 0 deletions src/Lucene.Net/Support/Native/Interop.Win32.Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace Lucene.Net.Support.Native
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

internal static partial class Interop
{
internal static partial class Win32
{
internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

// https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
internal const int ERROR_FILE_NOT_FOUND = 2;
internal const int ERROR_PATH_NOT_FOUND = 3;
internal const int ERROR_ACCESS_DENIED = 5;

// https://learn.microsoft.com/en-us/windows/win32/secauthz/generic-access-rights
internal const int GENERIC_WRITE = 0x40000000;

internal const int FILE_SHARE_READ = 0x00000001;
internal const int FILE_SHARE_WRITE = 0x00000002;
internal const int FILE_SHARE_DELETE = 0x00000004;
internal const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
internal const int OPEN_EXISTING = 3;
}
}
}
Loading

0 comments on commit 2623a01

Please sign in to comment.