/// <summary> /// Sets an attribute for a file. If it already exists, it will be replaced. /// </summary> /// <param name="this">The <see cref="FileInfo"/> referring to the file.</param> /// <param name="name">The name of the attribute to set.</param> /// <param name="data">The attribute data to write.</param> /// <exception cref="FileNotFoundException">If the file <paramref name="this"/> does not exist.</exception> /// <exception cref="ArgumentException"> /// If <paramref name="data"/> is larger than <see cref="Tsuku.MAX_ATTR_SIZE"/> bytes, or if /// <paramref name="name"/> is longer than <see cref="Tsuku.MAX_NAME_LEN"/>. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// If the filesystem of the file <paramref name="this"/> does not support extended attributes on the /// current operating system. /// </exception> public static void SetAttribute(this FileInfo @this, string name, ReadOnlySpan <byte> data) { DataAssertions.CheckValidity(name, data); if ([email protected]) { throw new FileNotFoundException("The requested file does not exist."); } Tsuku.GetImplementation(@this).Write(@this, name, data); }
/// <summary> /// Deletes an attribute for a file. /// </summary> /// <param name="this">The <see cref="FileInfo"/> referring to the file.</param> /// <param name="name">The name of the attribute to set.</param> /// <param name="data">The attribute data to write.</param> /// <exception cref="FileNotFoundException">If the file <paramref name="this"/> or argument does not exist.</exception> /// <exception cref="ArgumentException"> /// If <paramref name="name"/> is longer than <see cref="Tsuku.MAX_NAME_LEN"/>. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// If the filesystem of the file <paramref name="this"/> does not support extended attributes on the /// current operating system. /// </exception> public static void DeleteAttribute(this FileInfo @this, string name) { DataAssertions.CheckReadValidity(name); if ([email protected]) { throw new FileNotFoundException("The requested file does not exist."); } Tsuku.GetImplementation(@this).Delete(@this, name); }
/// <summary> /// Check that the outputs are valid for reading and writing. /// /// Ensures that <paramref name="name"/> is less than or equal to <see cref="Tsuku.MAX_NAME_LEN"/> characters, and the size of the /// <paramref name="data"/> buffer is less than <see cref="Tsuku.MAX_ATTR_SIZE"/>. /// </summary> /// <param name="name">The name of the attribute.</param> public static void CheckReadValidity(string name) { if (!DataAssertions.CheckNameLength(name)) { throw new ArgumentException($"Attribute name is longer than {Tsuku.MAX_NAME_LEN} characters."); } if (!DataAssertions.CheckNameValid(name)) { throw new ArgumentException("Attribute name contains invalid characters."); } }
/// <summary> /// Gets an attribute from a file. /// </summary> /// <param name="this">The <see cref="FileInfo"/> referring to the file.</param> /// <param name="name">The name of the attribute to get.</param> /// <exception cref="FileNotFoundException">If the file <paramref name="this"/>, or attribute does not exist.</exception> /// <exception cref="ArgumentException"> /// If <paramref name="name"/> is longer than <see cref="Tsuku.MAX_NAME_LEN"/>. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// If the filesystem of the file <paramref name="this"/> does not support extended attributes on the /// current operating system. /// </exception> public static byte[] GetAttribute(this FileInfo @this, string name) { Span <byte> data = stackalloc byte[Tsuku.MAX_ATTR_SIZE]; data.Clear(); DataAssertions.CheckValidity(name, data); int readBytes = Tsuku.GetImplementation(@this) .Read(@this, name, ref data); byte[] buf = new byte[readBytes]; data[..readBytes].CopyTo(buf);
/// <summary> /// Check that the inputs are valid for reading and writing. /// /// Ensures that <paramref name="name"/> is less than or equal to <see cref="Tsuku.MAX_NAME_LEN"/> characters, and the size of the /// <paramref name="data"/> buffer is less than <see cref="Tsuku.MAX_ATTR_SIZE"/>. /// </summary> /// <param name="name">The name of the attribute.</param> /// <param name="data">The buffer to read or write to.</param> public static void CheckValidity(string name, ReadOnlySpan <byte> data) { if (!DataAssertions.CheckNameLength(name)) { throw new ArgumentException($"Attribute name is longer than {Tsuku.MAX_NAME_LEN} characters."); } if (!DataAssertions.CheckNameValid(name)) { throw new ArgumentException("Attribute name contains invalid characters."); } if (!DataAssertions.CheckDataLength(data)) { throw new ArgumentException($"Buffer is longer than {Tsuku.MAX_ATTR_SIZE} bytes."); } }