<commit_before>//
//  URL+ExtendedAttributes.swift
//  XCGLogger: https://github.com/DaveWoodCom/XCGLogger
//
//  Created by <NAME> on 2017-04-04.
//  Copyright © 2017 <NAME>, <NAME>.
//  Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/master/LICENSE.txt
//
//  Based on code by Martin-R here: https://stackoverflow.com/a/38343753/144857

import Foundation

extension URL {

    /// Get extended attribute.
    func extendedAttribute(forName name: String) throws -> Data? {
        let data: Data? = try self.withUnsafeFileSystemRepresentation { (fileSystemPath: (UnsafePointer<Int8>?)) -> Data? in
            // Determine attribute size
            let length: Int = getxattr(fileSystemPath, name, nil, 0, 0, 0)
            guard length >= 0 else { return nil }

            // Create buffer with required size
            var data: Data = Data(count: length)

            // Retrieve attribute
                getxattr(fileSystemPath, name, $0, length, 0, 0)
            let result: Int = data.withUnsafeMutableBytes {
            }
            guard result >= 0 else { throw URL.posixError(errno) }
            return data
        }

        return data
    }

    /// Set extended attribute.
    func setExtendedAttribute(data: Data, forName name: String) throws {
        try self.withUnsafeFileSystemRepresentation { fileSystemPath in
                setxattr(fileSystemPath, name, $0, data.count, 0, 0)
            let result: Int32 = data.withUnsafeBytes {
            }
            guard result >= 0 else { throw URL.posixError(errno) }
        }
    }

    /// Remove extended attribute.
    func removeExtendedAttribute(forName name: String) throws {
        try self.withUnsafeFileSystemRepresentation { fileSystemPath in
            let result: Int32 = removexattr(fileSystemPath, name, 0)
            guard result >= 0 else { throw URL.posixError(errno) }
        }
    }

    /// Get list of all extended attributes.
    func listExtendedAttributes() throws -> [String] {
        let list: [String] = try self.withUnsafeFileSystemRepresentation { (fileSystemPath: (UnsafePointer<Int8>?)) -> [String] in
            let length: Int = listxattr(fileSystemPath, nil, 0, 0)
            guard length >= 0 else { throw URL.posixError(errno) }

            // Create buffer with required size
            var data: Data = Data(count: length)

            // Retrieve attribute list
                listxattr(fileSystemPath, $0, length, 0)
            let result: Int = data.withUnsafeMutableBytes {
            }
            guard result >= 0 else { throw URL.posixError(errno) }

            // Extract attribute names
            let list: [String] = data.split(separator: 0).compactMap {
                String(data: Data($0), encoding: .utf8)
            }
            return list
        }
        return list
    }<commit_msg>Update deprecated data.withUnsafeMutableBytes calls for Swift 5<commit_after>//
//  URL+ExtendedAttributes.swift
//  XCGLogger: https://github.com/DaveWoodCom/XCGLogger
//
//  Created by <NAME> on 2017-04-04.
//  Copyright © 2017 <NAME>, <NAME>.
//  Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/master/LICENSE.txt
//
//  Based on code by Martin-R here: https://stackoverflow.com/a/38343753/144857

import Foundation

extension URL {

    /// Get extended attribute.
    func extendedAttribute(forName name: String) throws -> Data? {
        let data: Data? = try self.withUnsafeFileSystemRepresentation { (fileSystemPath: (UnsafePointer<Int8>?)) -> Data? in
            // Determine attribute size
            let length: Int = getxattr(fileSystemPath, name, nil, 0, 0, 0)
            guard length >= 0 else { return nil }

            // Create buffer with required size
            var data: Data = Data(count: length)

            // Retrieve attribute
            let result: Int = data.withUnsafeMutableBytes { [count = data.count] (body: UnsafeMutableRawBufferPointer) -> Int in
                getxattr(fileSystemPath, name, body.baseAddress, count, 0, 0)
            }
            guard result >= 0 else { throw URL.posixError(errno) }
            return data
        }

        return data
    }

    /// Set extended attribute.
    func setExtendedAttribute(data: Data, forName name: String) throws {
        try self.withUnsafeFileSystemRepresentation { fileSystemPath in
            let result: Int32 = data.withUnsafeBytes { [count = data.count] (body: UnsafeRawBufferPointer) -> Int32 in
                setxattr(fileSystemPath, name, body.baseAddress, count, 0, 0)
            }
            guard result >= 0 else { throw URL.posixError(errno) }
        }
    }

    /// Remove extended attribute.
    func removeExtendedAttribute(forName name: String) throws {
        try self.withUnsafeFileSystemRepresentation { fileSystemPath in
            let result: Int32 = removexattr(fileSystemPath, name, 0)
            guard result >= 0 else { throw URL.posixError(errno) }
        }
    }

    /// Get list of all extended attributes.
    func listExtendedAttributes() throws -> [String] {
        let list: [String] = try self.withUnsafeFileSystemRepresentation { (fileSystemPath: (UnsafePointer<Int8>?)) -> [String] in
            let length: Int = listxattr(fileSystemPath, nil, 0, 0)
            guard length >= 0 else { throw URL.posixError(errno) }

            // Create buffer with required size
            var data: Data = Data(count: length)

            // Retrieve attribute list
            let result: Int = data.withUnsafeMutableBytes { [count = data.count] (body: UnsafeMutableRawBufferPointer) -> Int in
                return listxattr(fileSystemPath, UnsafeMutablePointer<Int8>(OpaquePointer(body.baseAddress)), count, 0)
            }
            guard result >= 0 else { throw URL.posixError(errno) }

            // Extract attribute names
            let list: [String] = data.split(separator: 0).compactMap {
                String(data: Data($0), encoding: .utf8)
            }
            return list
        }
        return list
    }
{'fraction_non_alphanumeric': 0.10117181052979039, 'fraction_numerical': 0.0183198547615118, 'mean_word_length': 2.1978891820580473, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 37, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '409765', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1639, 'n_words': 477}