AMSMB2

3.2.0

Swift framework to connect SMB2/3 shares
amosavian/AMSMB2

What's New

AMSMB2 3.2.0

2023-11-21T20:21:58Z
  • feat: Set file attributes including creation/modification time.
  • feat: Append data to end of file
  • feat: Async content fetch
  • feat: Conform types to Sendable to adopt new concurrency model
  • chore: Change formatter to SwiftFormat

AMSMB2

This is small Swift library for iOS, macOS and tvOS which wraps libsmb2 and allows to connect a SMB2/3 share and do file operation.

Swift Version Platform License

Build Status Release version

Getting Started

To use AMSMB2, add the following dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/amosavian/AMSMB2", .upToNextMinor(from: "3.0.0"))
]

You can then add the specific product dependency to your target:

dependencies: [
    .product(name: "AMSMB2", package: "AMSMB2"),
]

Usage

Just read inline help to find what each function does. It's straightforward. It's thread safe and any queue.

To do listing files in directory and file operations you must use this template:

import AMSMB2

class SMBClient {
    /// connect to: `smb://guest@XXX.XXX.XX.XX/share`
    
    let serverURL = URL(string: "smb://XXX.XXX.XX.XX")!
    let credential = URLCredential(user: "guest", password: "", persistence: URLCredential.Persistence.forSession)
    let share = "share"
    
    lazy private var client = AMSMB2(url: self.serverURL, credential: self.credential)!
    
    private func connect(handler: @escaping (Result<AMSMB2, Error>) -> Void) {
        // AMSMB2 can handle queueing connection requests
        client.connectShare(name: self.share) { error in
            if let error = error {
                handler(.failure(error))
            } else {
                handler(.success(self.client))
            }
        }
    }
    
    func listDirectory(path: String) {
        connect { result in
            switch result {
            case .success(let client):
                client.contentsOfDirectory(atPath: path) { result in
                    switch result {
                    case .success(let files):
                        for entry in files {
                            print("name:", entry[.nameKey] as! String,
                                  ", path:", entry[.pathKey] as! String,
                                  ", type:", entry[.fileResourceTypeKey] as! URLFileResourceType,
                                  ", size:", entry[.fileSizeKey] as! Int64,
                                  ", modified:", entry[.contentModificationDateKey] as! Date,
                                  ", created:", entry[.creationDateKey] as! Date)
                        }
                        
                    case .failure(let error):
                        print(error)
                    }
                }
                
            case .failure(let error):
                print(error)
            }
        }
    }
    
    func moveItem(path: String, to toPath: String) {
        self.connect { result in
            switch result {
            case .success(let client):
                client.moveItem(atPath: path, toPath: toPath) { error in
                    if let error = error {
                        print(error)
                    } else {
                        print("\(path) moved successfully.")
                    }
                    
                    // Disconnecting is optional, it will be called eventually
                    // when `AMSMB2` object is freed.
                    // You may call it explicitly to detect errors.
                    client.disconnectShare(completionHandler: { (error) in
                        if let error = error {
                            print(error)
                        }
                    })
                }
                
            case .failure(let error):
                print(error)
            }
        }
    }
}

License

While source code shipped with project is MIT licensed, but it has static link to libsmb2 which is LGPL v2.1, consequently the whole project becomes LGPL v2.1.

You must link this library dynamically to your app if you intend to distribute your app on App Store.

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

Last updated: Sun Dec 03 2023 03:11:47 GMT-1000 (Hawaii-Aleutian Standard Time)