Skip to content

Commit

Permalink
Merge pull request #143 from kishikawakatsumi/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
kishikawakatsumi authored Sep 19, 2024
2 parents b9b77eb + 51b4322 commit 1a2683c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
jobs:
test:
runs-on: macos-14
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension QueryDirectory.Response: CustomDebugStringConvertible {
Blob Offset: \(outputBufferOffset)
Blob Length: \(outputBufferLength)
Info:
\(files.map { "\($0)".split(separator: "\n").map { " \($0)" }.joined(separator: "\n") }.joined(separator: "\n"))
\(files().map { "\($0)".split(separator: "\n").map { " \($0)" }.joined(separator: "\n") }.joined(separator: "\n"))
"""
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SMBClient/Messages/QueryDirectory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public enum QueryDirectory {
public let outputBufferOffset: UInt16
public let outputBufferLength: UInt32
public let buffer: Data
public let files: [FileDirectoryInformation]

public init(data: Data) {
let reader = ByteReader(data)
Expand All @@ -84,7 +83,9 @@ public enum QueryDirectory {
outputBufferOffset = reader.read()
outputBufferLength = reader.read()
buffer = data[UInt32(outputBufferOffset)..<UInt32(outputBufferOffset) + outputBufferLength]
}

public func files() -> [FileDirectoryInformation] {
var files = [FileDirectoryInformation]()
if outputBufferLength > 0 {
var data = Data(buffer)
Expand All @@ -95,7 +96,7 @@ public enum QueryDirectory {
} while files.last!.nextEntryOffset != 0
}

self.files = files
return files
}
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/SMBClient/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,9 @@ public class Session {
)

let createResponse = Create.Response(data: data)
var files = [FileDirectoryInformation]()

let queryDirectoryResponse = QueryDirectory.Response(data: Data(data[createResponse.header.nextCommand...]))
files.append(contentsOf: queryDirectoryResponse.files)

var files: [FileDirectoryInformation] = queryDirectoryResponse.files()

if NTStatus(createResponse.header.status) != .noMoreFiles {
repeat {
Expand All @@ -343,7 +342,7 @@ public class Session {

let data = try await send(queryDirectoryRequest.encoded())
let queryDirectoryResponse = QueryDirectory.Response(data: data)
files.append(contentsOf: queryDirectoryResponse.files)
files.append(contentsOf: queryDirectoryResponse.files())

if NTStatus(queryDirectoryResponse.header.status) == .noMoreFiles {
break
Expand Down
6 changes: 3 additions & 3 deletions Tests/SMBClientTests/SMBClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ final class SMBClientTests: XCTestCase {
try await client.createDirectory(path: directoryName)

var testFiles = [String]()
for i in 0...2000 {
for i in 0..<4000 {
let length = 1024
var data = Data(count: length)
for i in 0..<length {
data[i] = UInt8(arc4random_uniform(256))
}

let testFilename = "file-\(String(format: "%08d", i)).dat"
let testFilename = "file-\(String(format: "%0128d", i)).dat"
try await client.upload(content: data, path: "\(directoryName)/\(testFilename)")
testFiles.append(testFilename)
}
Expand All @@ -342,7 +342,7 @@ final class SMBClientTests: XCTestCase {
.filter { $0.name != "." && $0.name != ".." }
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
.map { $0.name }
XCTAssertEqual(files, testFiles)
XCTAssertEqual(files, testFiles.sorted { $0.localizedStandardCompare($1) == .orderedAscending })

try await client.deleteDirectory(path: directoryName)
try await assertDirectoryDoesNotExist(at: directoryName, client: client)
Expand Down

0 comments on commit 1a2683c

Please sign in to comment.