diff --git a/Claunia.IO/ChangeLog b/Claunia.IO/ChangeLog index 2dfb890..5799cea 100644 --- a/Claunia.IO/ChangeLog +++ b/Claunia.IO/ChangeLog @@ -1,3 +1,10 @@ +2015-04-25 Natalia Portillo + + * Claunia.IO.csproj: + * Interop/Apple/Interop.Apple.xattr.cs: + Implemented Mac OS X getxattr(2), setxattr(2), + removexattr(2) and listxattr(2). + 2015-04-25 Natalia Portillo * Interop/Apple/Interop.Apple.getattrlist.cs: diff --git a/Claunia.IO/Claunia.IO.csproj b/Claunia.IO/Claunia.IO.csproj index 150dbe4..5812f9e 100644 --- a/Claunia.IO/Claunia.IO.csproj +++ b/Claunia.IO/Claunia.IO.csproj @@ -54,6 +54,7 @@ + diff --git a/Claunia.IO/Interop/Apple/Interop.Apple.xattr.cs b/Claunia.IO/Interop/Apple/Interop.Apple.xattr.cs new file mode 100644 index 0000000..d11c4ab --- /dev/null +++ b/Claunia.IO/Interop/Apple/Interop.Apple.xattr.cs @@ -0,0 +1,126 @@ +// +// Interop.Apple.xattr.cs +// +// Author: +// Natalia Portillo +// +// Copyright (c) 2015 © Claunia.com +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Apple + { + enum xattrOptions : int + { + /// + /// Don't follow symbolic links + /// + XATTR_NOFOLLOW = 0x0001, + /// + /// Set the value and fail if the xattr already exists + /// + XATTR_CREATE = 0x0002, + /// + /// Set the value and fail if the xattr does not exist + /// + XATTR_REPLACE = 0x0004, + /// + /// Bypass authorization checking + /// + XATTR_NOSECURITY = 0x0008, + /// + /// Bypass default extended attribute file (._file) + /// + XATTR_NODEFAULT = 0x0010, + /// + /// Expose the HFS+ compression extended attributes + /// + XATTR_SHOWCOMPRESSION = 0x0020 + } + + /// + /// Maximum length of xattr name + /// + const int XATTR_MAXNAMELEN = 127; + + /// + /// Name for as a xattr + /// + const int XATTR_FINDERINFO_NAME = "com.apple.FinderInfo"; + /// + /// Name for resource fork as a xattr + /// + const int XATTR_RESOURCEFORK_NAME = "com.apple.ResourceFork"; + + /// + /// Gets an extended attribute value + /// + /// Path to the file. + /// Name of the extended attribute. + /// Pointer to a buffer where to store the extended attribute. + /// Size of the allocated buffer. + /// Offset of the extended attribute where to start reading, valid only for resource forks, 0 for rest. + /// . + /// Size of the extended attribute. On failure, -1, and errno is set + [DllImport(Libraries.Libc, SetLastError = true)] + public static extern int getxattr(string path, string name, IntPtr value, UInt32 size, UInt32 position, xattrOptions options); + + /// + /// Sets an extended attribute value + /// + /// Path to the file. + /// Name of the extended attribute. + /// Pointer to a buffer where the extended attribute is stored. + /// Size of the allocated buffer. + /// Offset of the extended attribute where to start writing, valid only for resource forks, 0 for rest. + /// . + /// On success, 0. On failure, -1, and errno is set + [DllImport(Libraries.Libc, SetLastError = true)] + public static extern int getxattr(string path, string name, IntPtr value, UInt32 size, UInt32 position, xattrOptions options); + + /// + /// Removes an extended attribute + /// + /// Path to the file. + /// Name of the extended attribute. + /// . + /// On success, 0. On failure, -1, and errno is set + [DllImport(Libraries.Libc, SetLastError = true)] + public static extern int removexattr(string path, string name, xattrOptions options); + + /// + /// Lists the extended attributes from a file + /// + /// Path to the file. + /// Pointer to a buffer where an unordered list of null terminated UTF-8 strings wth the extended attributes names is to be stored. + /// Size of the allocated buffer. + /// . + /// If is set to null, the needed size to store the whole list. + /// On success, the size of the list. + /// If the file has no extended attributes, 0. + /// On failure, -1, and errno is set + [DllImport(Libraries.Libc, SetLastError = true)] + public static extern int listxattr(string path, IntPtr namebuf, UInt32 size, xattrOptions options); + } +} +