@@ -13,7 +13,7 @@ func parseBasePath (file: NSFileHandle) -> String {
1313 let pathMaxLen = 512
1414 let readBuffer = UnsafeMutablePointer< UInt8> . alloc( pathMaxLen)
1515 data. getBytes ( readBuffer, range: NSMakeRange ( pathOffset, pathMaxLen) )
16-
16+
1717 let buf = UnsafeMutablePointer < CChar > ( readBuffer)
1818 buf [ pathMaxLen - 1 ] = 0
1919 var str = " "
@@ -24,26 +24,27 @@ func parseBasePath (file: NSFileHandle) -> String {
2424 return str
2525}
2626
27- func updateBasePath ( file: NSFileHandle , newPath: String ) -> Int {
28- if newPath. characters. count > 256
29- {
30- print ( " Path too long " )
31- return 1
27+ enum UpdateError : ErrorType {
28+ case TooLong
29+ }
30+
31+ func updateBasePath ( file: NSFileHandle , newPath: String ) throws {
32+ if newPath. characters. count > 256 {
33+ print ( " Path too long " )
34+ throw ( UpdateError . TooLong)
3235 }
3336 // Write length byte
3437 let lengthByteOffset : UInt64 = 0x13
3538 file. seekToFileOffset ( lengthByteOffset)
3639 let lengthByte = NSData ( bytes: [ UInt8 ( newPath. characters. count) ] , length: 1 )
3740 file. writeData ( lengthByte)
38-
41+
3942 // Write path
4043 let pathOffset : UInt64 = 0xA0 + 24
4144 file. seekToFileOffset ( pathOffset)
4245 let strData = NSMutableData ( data: newPath. dataUsingEncoding ( NSUTF8StringEncoding) !)
4346 strData. appendBytes ( [ UInt8 ( 0 ) ] , length: 1 )
4447 file. writeData ( strData)
45-
46- return 0
4748}
4849
4950// Entry point
@@ -74,25 +75,23 @@ if let modeStr = String.fromCString(Process.arguments[1]) {
7475func main ( ) -> Int32 {
7576 if let path = String . fromCString ( Process . arguments [ 2 ] ) {
7677 if let file = NSFileHandle ( forUpdatingAtPath: path) {
77- // defer {
78- // file.closeFile()
79- // }
80- let error : Int
81- if mode == Mode . Patch {
82- print ( parseBasePath ( file) )
83- error = updateBasePath ( file, newPath: " C: \\ ece391 \\ devel \\ ece391.qcow " )
84- } else {
85- if let newPath = Process . arguments. count >= 4 ? String . fromCString ( Process . arguments [ 3 ] ) : nil {
86- error = updateBasePath ( file, newPath: newPath)
78+ defer {
79+ file. closeFile ( )
80+ }
81+ do {
82+ if mode == Mode . Patch {
83+ print ( parseBasePath ( file) )
84+ try updateBasePath ( file, newPath: " C: \\ ece391 \\ devel \\ ece391.qcow " )
8785 } else {
88- print ( " Please specify new backing image path " )
89- file. closeFile ( )
90- return 1
86+ if let newPath = Process . arguments. count >= 4 ? String . fromCString ( Process . arguments [ 3 ] ) : nil {
87+ try updateBasePath ( file, newPath: newPath)
88+ } else {
89+ print ( " Please specify new backing image path " )
90+ return 1
91+ }
9192 }
92- }
93- if error != 0 {
93+ } catch {
9494 print ( " Error occurred " )
95- file. closeFile ( )
9695 return 1
9796 }
9897 } else {
0 commit comments