Commit 6948d9a5 authored by Johannes Schriewer's avatar Johannes Schriewer

Merge pull request #12 from AnarchyTools/develop

Merge develop
parents 0dffccf9 95f6af35
Pipeline #1995 passed with stage
......@@ -89,7 +89,7 @@ public class File {
self.closeWhenDeallocated = false
self.fp = fopen(path.description, openMode)
if self.fp == nil {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
self.closeWhenDeallocated = true
}
......@@ -111,7 +111,7 @@ public class File {
self.fp = fdopen(fd, openMode)
if self.fp == nil {
throw errnoToError(errno: errno)
throw SysError(errno: errno, fd)
}
}
......@@ -178,7 +178,7 @@ public class File {
let read = fread(UnsafeMutablePointer(buffer), 1, size, self.fp)
if read == 0 {
if feof(self.fp) == 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno)
} else {
throw SysError.EndOfFile
}
......@@ -221,7 +221,7 @@ public class File {
let read = fgets(UnsafeMutablePointer(buffer), 64 * 1024, self.fp)
if read == nil {
if feof(self.fp) == 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno)
} else {
throw SysError.EndOfFile
}
......@@ -236,7 +236,7 @@ public class File {
let bytes = string.utf8.count
let written = fwrite(string, 1, bytes, self.fp)
if bytes != written {
throw errnoToError(errno: errno)
throw SysError(errno: errno)
}
}
......@@ -246,7 +246,7 @@ public class File {
public func writeLine(string: String) throws {
let bytes = fputs(string + "\n", self.fp)
if bytes < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno)
}
}
......@@ -257,7 +257,7 @@ public class File {
let bytes = data.count
let written = fwrite(data, 1, bytes, self.fp)
if bytes != written {
throw errnoToError(errno: errno)
throw SysError(errno: errno)
}
}
......@@ -272,16 +272,18 @@ public class File {
/// is bigger than the current size remaining
/// size is prefilled with zeroes.
public func truncate(size: Int) throws {
var e: SysError? = nil
repeat {
var e: SysError
while true {
fflush(self.fp)
if ftruncate(fileno(self.fp), off_t(size)) != 0 {
e = errnoToError(errno: errno)
if e! != .Interrupted {
throw e!
e = SysError(errno: errno)
if case .Interrupted = e {
continue
}
throw e
}
} while e == .Interrupted
break
}
fflush(self.fp)
}
......
......@@ -58,7 +58,7 @@ public class FS {
let _ = try File(path: path, mode: .WriteOnly)
} else {
if utimes(path.description, nil) < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
}
......@@ -72,19 +72,19 @@ public class FS {
/// - Parameter recursive: optional, set to true to recursively remove directories
public class func removeItem(path: Path, recursive: Bool = false) throws {
if !FS.fileExists(path: path) {
throw SysError.NoSuchEntity
throw SysError.NoSuchEntity(path)
}
if FS.isDirectory(path: path) {
if recursive {
try FS._rmdir_recursive(path: path)
} else {
if rmdir(path.description) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
} else {
if unlink(path.description) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
}
......@@ -97,18 +97,18 @@ public class FS {
public class func createDirectory(path: Path, intermediate: Bool = false) throws {
if !intermediate {
if mkdir(path.description, 511) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
} else {
for idx in 0..<path.components.count {
let subPath = Path(components: Array(path.components[0...idx]), absolute: path.isAbsolute)
if !FS.fileExists(path: subPath) {
if mkdir(subPath.description, 511) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, subPath)
}
} else {
if !FS.isDirectory(path: subPath) {
throw SysError.NotADirectory
throw SysError.NotADirectory(subPath)
}
}
}
......@@ -123,7 +123,7 @@ public class FS {
var sbuf = stat()
let result = stat(path.description, &sbuf)
if result < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
return FileInfo(path: path, statBuf: sbuf)
}
......@@ -144,7 +144,7 @@ public class FS {
// TODO: Test
let info = try self.getInfo(path: path)
if chown(path.description, newOwner, info.group) < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
......@@ -163,7 +163,7 @@ public class FS {
public class func setGroup(path: Path, newGroup: gid_t) throws {
let info = try self.getInfo(path: path)
if chown(path.description, info.owner, newGroup) < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
......@@ -174,7 +174,7 @@ public class FS {
/// - Parameter group: Group ID of new owner
public class func setOwnerAndGroup(path: Path, owner: uid_t, group: gid_t) throws {
if chown(path.description, owner, group) < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
......@@ -200,7 +200,7 @@ public class FS {
/// - Parameter mode: attributes to set
public class func setAttributes(path: Path, mode: FileMode) throws {
if chmod(path.description, mode.rawValue) < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
......@@ -215,7 +215,7 @@ public class FS {
let err = getgrgid_r(id, &grpBuf, &buffer, 1024, &result)
if err != 0 {
throw errnoToError(errno: err)
throw SysError(errno: err)
}
if result != nil {
return String(validatingUTF8: grpBuf.gr_name)
......@@ -235,7 +235,7 @@ public class FS {
let err = getgrnam_r(name, &grpBuf, &buffer, 1024, &result)
if err != 0 {
throw errnoToError(errno: err)
throw SysError(errno: err)
}
if result != nil {
......@@ -256,7 +256,7 @@ public class FS {
let err = getpwuid_r(id, &pwBuf, &buffer, 1024, &result)
if err != 0 {
throw errnoToError(errno: err)
throw SysError(errno: err)
}
if result != nil {
return String(validatingUTF8: pwBuf.pw_name)
......@@ -276,7 +276,7 @@ public class FS {
let err = getpwnam_r(name, &pwBuf, &buffer, 1024, &result)
if err != 0 {
throw errnoToError(errno: err)
throw SysError(errno: err)
}
if result != nil {
......@@ -292,7 +292,7 @@ public class FS {
public class func getWorkingDirectory() throws -> Path {
var buffer = [Int8](repeating: 0, count: 1025)
if getcwd(&buffer, 1024) == nil {
throw errnoToError(errno: errno)
throw SysError(errno: errno)
}
if let dir = String(validatingUTF8: buffer) {
return Path(dir)
......@@ -306,7 +306,7 @@ public class FS {
/// - Parameter path: path to change current directory to
public class func changeWorkingDirectory(path: Path) throws {
if chdir(path.description) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
......@@ -316,7 +316,7 @@ public class FS {
/// - Parameter to: destination path
public class func symlinkItem(from: Path, to: Path) throws {
if symlink(from.description, to.description) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, to, from)
}
}
......@@ -333,7 +333,7 @@ public class FS {
/// - Parameter includeHidden: optional, include hidden files, defaults to false
public class func iterateItems(path: Path, recursive: Bool = false, includeHidden: Bool = false) throws -> AnyIterator<FileInfo> {
guard let d = opendir(path.description) else {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
var deStack = [(Dir_t, dirent, Path)]()
deStack.append((d, dirent(), path))
......@@ -408,8 +408,8 @@ public class FS {
// TODO: Test
let result = rename(from.description, to.description)
if result < 0 {
let error = errnoToError(errno: errno)
if error == .CrossDeviceLink && !atomic {
let error = SysError(errno: errno, to, from)
if case .CrossDeviceLink = error where !atomic {
try FS.copyItem(from: from, to: to, recursive: true)
try FS.removeItem(path: from, recursive: true)
} else {
......@@ -428,7 +428,7 @@ public class FS {
// TODO: Test
let isDir = FS.isDirectory(path: from)
if !recursive && isDir {
throw SysError.IsDirectory
throw SysError.IsDirectory(from)
}
if isDir {
try FS._copy_recursive(from: from, to: to)
......@@ -448,7 +448,7 @@ public class FS {
var buf = Array(p.description.utf8)
buf.append(0)
if mkdtemp(UnsafeMutablePointer(buf)) == nil {
throw errnoToError(errno: errno)
throw SysError(errno: errno, p)
}
if let dirname = String(validatingUTF8: UnsafeMutablePointer(buf)) {
return Path(dirname)
......@@ -472,7 +472,7 @@ public class FS {
switch file.type {
case .FIFO:
if mkfifo(destinationPath.description, file.mode.rawValue) < 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, destinationPath)
}
try FS.setAttributes(path: destinationPath, mode: file.mode)
case .Directory:
......@@ -506,7 +506,7 @@ public class FS {
/// - Parameter path: path to remove
private class func _rmdir_recursive(path: Path) throws {
guard let d = opendir(path.description) else {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
defer {
closedir(d)
......@@ -515,8 +515,9 @@ public class FS {
var de = dirent()
var result:UnsafeMutablePointer<dirent>? = nil
repeat {
guard readdir_r(d, &de, &result) == 0 else {
throw errnoToError(errno: errno)
let status = readdir_r(d, &de, &result)
guard status == 0 else {
throw SysError(errno: status, path)
}
if result == nil {
break
......@@ -540,13 +541,13 @@ public class FS {
try FS._rmdir_recursive(path: subPath)
} else {
if unlink(subPath.description) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, subPath)
}
}
}
} while true
if rmdir(path.description) != 0 {
throw errnoToError(errno: errno)
throw SysError(errno: errno, path)
}
}
}
......@@ -182,6 +182,18 @@ public struct Path {
}
}
public func ==(lhs: Path, rhs: Path) -> Bool {
if lhs.components.count != rhs.components.count || lhs.isAbsolute != rhs.isAbsolute {
return false
}
for i in 0..<lhs.components.count {
if lhs.components[i] != rhs.components[i] {
return false
}
}
return true
}
public func +(lhs: Path, rhs: String) -> Path {
return lhs.join(Path(rhs))
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -203,7 +203,15 @@ class FSTests: XCTestCase {
func testLoadNonexistentFile() {
let p = Path("doesnotexist.file")
let _ = try? File(path: p, mode: .ReadOnly)
do {
let _ = try File(path: p, mode: .ReadOnly)
XCTFail("Should have thrown an error")
} catch SysError.NoSuchEntity(let path) {
XCTAssertNotNil(path)
XCTAssert(p == path!)
} catch {
XCTFail("Wrong error thrown: \(error)")
}
}
}
......
......@@ -19,42 +19,41 @@ class LoggerTests: XCTestCase {
func testLogStdErr() {
Log.logLevel = .Debug
Log.logTarget = .StdErr
Log.logFileAndLine = true
Log.logger = StdErrLogger()
Log.logger.logFileAndLine = true
Log.debug("debug log", Log.logTarget)
Log.info("info log", Log.logTarget)
Log.warn("warn log", Log.logTarget)
Log.error("error log", Log.logTarget)
Log.fatal("fatal log", Log.logTarget)
Log.debug("debug log", "stderr")
Log.info("info log", "stderr")
Log.warn("warn log", "stderr")
Log.error("error log", "stderr")
Log.fatal("fatal log", "stderr")
}
func testLogStdOut() {
Log.logLevel = .Debug
Log.logTarget = .StdOut
Log.logFileAndLine = true
Log.logger = StdOutLogger()
Log.logger.logFileAndLine = false
Log.debug("debug log", Log.logTarget)
Log.info("info log", Log.logTarget)
Log.warn("warn log", Log.logTarget)
Log.error("error log", Log.logTarget)
Log.fatal("fatal log", Log.logTarget)
Log.debug("debug log", "stdout")
Log.info("info log", "stdout")
Log.warn("warn log", "stdout")
Log.error("error log", "stdout")
Log.fatal("fatal log", "stdout")
}
func testLogFile() {
do {
Log.logLevel = .Debug
Log.logFileName = try FS.temporaryDirectory() + "logfile.log"
Log.logTarget = .File
Log.logFileAndLine = true
Log.logger = try FileLogger(filename: try FS.temporaryDirectory() + "logfile.log")
Log.logger.logFileAndLine = true
Log.debug("debug log", Log.logTarget)
Log.info("info log", Log.logTarget)
Log.warn("warn log", Log.logTarget)
Log.error("error log", Log.logTarget)
Log.fatal("fatal log", Log.logTarget)
Log.debug("debug log", "file")
Log.info("info log", "file")
Log.warn("warn log", "file")
Log.error("error log", "file")
Log.fatal("fatal log", "file")
let file = try File(path: Log.logFileName!, mode: .ReadOnly)
let file = try File(path: (Log.logger as! FileLogger).logFileName!, mode: .ReadOnly)
let content = ["debug", "info", "warn", "error", "fatal"]
for idx in 0..<5 {
let line = try file.readLine()!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment