Skip to content

Commit

Permalink
Probable fix #41, Fix a race condition in service()
Browse files Browse the repository at this point in the history
  • Loading branch information
amosavian committed Nov 12, 2019
1 parent cc23570 commit 9decc3f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion AMSMB2.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "AMSMB2"
s.version = "2.4.0"
s.version = "2.5.0"
s.summary = "Swift framework to connect SMB2/3 shares"

# This description is used to generate tags and improve search results.
Expand Down
4 changes: 2 additions & 2 deletions AMSMB2.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
"@loader_path/../Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/libsmb2/lib";
MARKETING_VERSION = 2.4.0;
MARKETING_VERSION = 2.5.0;
OTHER_LDFLAGS = "";
"OTHER_LDFLAGS[sdk=appletvos*]" = "-lsmb2-tvos";
"OTHER_LDFLAGS[sdk=appletvsimulator*]" = "-lsmb2-tvos";
Expand Down Expand Up @@ -446,7 +446,7 @@
"@loader_path/../Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/libsmb2/lib";
MARKETING_VERSION = 2.4.0;
MARKETING_VERSION = 2.5.0;
"OTHER_LDFLAGS[sdk=appletvos*]" = "-lsmb2-tvos";
"OTHER_LDFLAGS[sdk=appletvsimulator*]" = "-lsmb2-tvos";
"OTHER_LDFLAGS[sdk=iphoneos*]" = "-lsmb2-ios";
Expand Down
37 changes: 17 additions & 20 deletions AMSMB2/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SMB2.Raw
/// Provides synchronous operation on SMB2
final class SMB2Context: CustomDebugStringConvertible, CustomReflectable {
var context: UnsafeMutablePointer<smb2_context>?
private var _context_lock = NSLock()
private var _context_lock = NSRecursiveLock()
var timeout: TimeInterval

init(timeout: TimeInterval) throws {
Expand Down Expand Up @@ -53,7 +53,7 @@ final class SMB2Context: CustomDebugStringConvertible, CustomReflectable {
c.append((label: "securityMode", value: securityMode))
c.append((label: "authentication", value: authentication))
clientGuid.map { c.append((label: "clientGuid", value: $0)) }
user.map { c.append((label: "user", value: $0)) }
c.append((label: "user", value: user))
c.append((label: "version", value: version))
}
c.append((label: "isConnected", value: isConnected))
Expand Down Expand Up @@ -187,16 +187,14 @@ extension SMB2Context {
}

func service(revents: Int32) throws {
let result = try withThreadSafeContext { (context) in
return smb2_service(context, revents)
}
if result < 0 {
_context_lock.lock()
smb2_destroy_context(context)
context = nil
_context_lock.unlock()
try withThreadSafeContext { (context) in
let result = smb2_service(context, revents)
if result < 0 {
self.context = nil
smb2_destroy_context(context)
}
try POSIXError.throwIfError(result, description: error)
}
try POSIXError.throwIfError(result, description: error)
}
}

Expand Down Expand Up @@ -359,16 +357,15 @@ extension SMB2Context {
@discardableResult
func async_await(execute handler: ContextHandler<Int32>) throws -> Int32
{
var cb = CBData()

let result = try withThreadSafeContext { (context) -> Int32 in
return try handler(context, &cb)
return try withThreadSafeContext { (context) -> Int32 in
var cb = CBData()
let result = try handler(context, &cb)
try POSIXError.throwIfError(result, description: error)
try wait_for_reply(&cb)
let cbResult = cb.result
try POSIXError.throwIfError(cbResult, description: error)
return cbResult
}
try POSIXError.throwIfError(result, description: error)
try wait_for_reply(&cb)
let cbResult = cb.result
try POSIXError.throwIfError(cbResult, description: error)
return cbResult
}

@discardableResult
Expand Down
27 changes: 17 additions & 10 deletions AMSMB2/Directory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ final class SMB2Directory: Collection {
}

deinit {
let handle = self.handle
try? context.withThreadSafeContext { (context) in
smb2_closedir(context, handle)
}
}

func makeIterator() -> AnyIterator<smb2dirent> {
smb2_rewinddir(context.context, handle)
let context = self.context.context
let handle = self.handle
smb2_rewinddir(context, handle)
return AnyIterator {
return smb2_readdir(self.context.context, self.handle)?.pointee
return smb2_readdir(context, self.handle)?.pointee
}
}

Expand All @@ -47,26 +50,30 @@ final class SMB2Directory: Collection {
}

var count: Int {
let currentPos = smb2_telldir(context.context, handle)
let context = self.context.context
let handle = self.handle
let currentPos = smb2_telldir(context, handle)
defer {
smb2_seekdir(context.context, handle, currentPos)
smb2_seekdir(context, handle, currentPos)
}

smb2_rewinddir(context.context, handle)
smb2_rewinddir(context, handle)
var i = 0
while smb2_readdir(context.context, handle) != nil {
while smb2_readdir(context, handle) != nil {
i += 1
}
return i
}

subscript(position: Int) -> smb2dirent {
let currentPos = smb2_telldir(context.context, handle)
smb2_seekdir(context.context, handle, 0)
let context = self.context.context
let handle = self.handle
let currentPos = smb2_telldir(context, handle)
smb2_seekdir(context, handle, 0)
defer {
smb2_seekdir(context.context, handle, currentPos)
smb2_seekdir(context, handle, currentPos)
}
return smb2_readdir(context.context, handle).move()
return smb2_readdir(context, handle).move()
}

func index(after i: Int) -> Int {
Expand Down
9 changes: 6 additions & 3 deletions AMSMB2/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ final class SMB2FileHandle {
}

deinit {
_ = try? context.withThreadSafeContext { (context) in
try smb2_close(context, handle.unwrap())
}
do {
let handle = try self.handle.unwrap()
try context.async_await { (context, cbPtr) -> Int32 in
smb2_close_async(context, handle, SMB2Context.generic_handler, cbPtr)
}
} catch { }
}

var fileId: smb2_file_id {
Expand Down
1 change: 0 additions & 1 deletion AMSMB2Tests/AMSMB2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ class AMSMB2Tests: XCTestCase {
smb.connectShare(name: share, encrypted: encrypted) { (error) in
XCTAssertNil(error)

smb.removeDirectory(atPath: "removeTest", recursive: true, completionHandler: nil)
smb.createDirectory(atPath: "removeTest") { (error) in
XCTAssertNil(error)

Expand Down

0 comments on commit 9decc3f

Please sign in to comment.