Swiftpack.co - Package - amosavian/AMSMB2

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 CocoaPods version Carthage compatible

Install

Cocoapods / Carthage

Add this line to your pods file:

pod "AMSMB2"

Or add this to Cartfile:

github "amosavian/AMSMB2"

Manually

To have latest updates with ease, use this command on terminal to get a clone:

git clone https://github.com/amosavian/AMSMB2

You can update your library using this command in AMSMB2 folder:

git pull

if you have a git based project, use this command in your project's directory to add this project as a submodule to your project:

git submodule add https://github.com/amosavian/AMSMB2

Then drop AMSMB2.xcodeproj to you Xcode workspace and add the framework to your Embeded Binaries in target.

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.

Github

link
Stars: 58
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

2.6.0 - Nov 28, 2019

  • Refactoring async_await()
  • [URLResourceKey: Any] extensions in order to easier access to values
  • Updated libsmb2 to fix memory corruption errors

2.5.0 - Nov 12, 2019

  • Add removeItem() function. removeDirectory() and removeFile() are still accessible for performance reasons.
  • Fix errors on Xcode 10.2.
  • Added test for error.
  • Probable fix #41.
  • Fix a race condition in service().
  • Updated libsmb2.
  • Minor refactors.

2.4.0 - Oct 31, 2019

  • Support SMB3 encrypted connection
  • Fix server error may cause waiting until timeout
  • Fix a critical memory management issue
  • Fix EXC_BAD_ACCESS crash in async handler
  • Update libsmb2 (Better error code for connect)
  • Refactoring

2.3.1 - Oct 27, 2019

Previous release fixes:

  • Update libsmb2 library
  • New method to find destination of symbolic
  • Support for symlink file type
  • Authentication set to NTLMSSP
  • Better error text
  • Removal of Accio package manager support
  • Fix Objective-C inline help data types
  • Fix #33

2.3.0 - Oct 25, 2019

  • Update libsmb2 library
  • New method to find destination of symbolic
  • Support for symlink file type
  • Authentication set to NTLMSSP
  • Better error text
  • Removal of Accio package manager support
  • Fix Objective-C inline help data types
  • Fix #33