-
Notifications
You must be signed in to change notification settings - Fork 8
/
Dir.swift
211 lines (171 loc) · 6.24 KB
/
Dir.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// Dir.swift
// SwiftRuby
//
// Created by John Holdsworth on 28/09/2015.
// Copyright © 2015 John Holdsworth. All rights reserved.
//
// $Id: //depot/SwiftRuby/Dir.swift#15 $
//
// Repo: https://github.com/RubyNative/SwiftRuby
//
// See: http://ruby-doc.org/core-2.2.3/Dir.html
//
import Darwin
open class Dir: RubyObject, array_like {
let dirpath: String
var unixDIR: UnsafeMutablePointer<DIR>?
// Dir[ string [, string ...] ] → array
init?(dirname: string_like, file: StaticString = #file, line: UInt = #line) {
dirpath = dirname.to_s
unixDIR = opendir(dirpath)
super.init()
if unixDIR == nil {
SRError("opendir '\(dirpath.to_s)' failed", file: file, line: line)
return nil
}
}
// MARK: Class Methods
open class func new(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Dir? {
return Dir(dirname: string, file: file, line: line)
}
open class func open(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Dir? {
return new(string, file: file, line: line)
}
open class func chdir(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return unixOK("Dir.chdir '\(string.to_s)", Darwin.chdir(string.to_s), file: file, line: line)
}
open class func chroot(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return unixOK("Dir.chroot '\(string.to_s)", Darwin.chroot(string.to_s), file: file, line: line)
}
open class func delete(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return unixOK("Dir.rmdir '\(string.to_s)", Darwin.rmdir(string.to_s), file: file, line: line)
}
open class func entries(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> [String] {
var out = [String]()
foreach(dirname) {
(name) in
out.append(name)
}
return out
}
open class func exist(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return File.exist(dirname, file: file, line: line)
}
open class func exists(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return exist(dirname, file: file, line: line)
}
open class func foreach(_ dirname: string_like, _ block: (String) -> ()) {
if let dir = Dir(dirname: dirname, file: #file, line: #line) {
dir.each {
(name) in
block(name)
}
}
}
open class var getwd: String? {
var cwd = [Int8](repeating: 0, count: Int(PATH_MAX))
if !unixOK("Dir.getwd", Darwin.getcwd(&cwd, cwd.count) != nil ? 0 : 1, file: #file, line: #line) {
return nil
}
return String(validatingUTF8: cwd)
}
open class func glob(_ pattern: string_like, _ flags: Int32 = 0, file: StaticString = #file, line: UInt = #line) -> [String]? {
return pattern.to_s.withCString {
var pglob = glob_t()
if (unixOK("Dir.glob", Darwin.glob($0, flags, nil, &pglob), file: file, line: line)) {
defer { globfree(&pglob) }
return (0..<Int(pglob.gl_matchc)).map { String(cString: U(U(pglob.gl_pathv)[$0])) }
}
return nil
}
}
open class func home(_ user: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? {
var user = user?.to_s
var buff = [Int8](repeating: 0, count: Int(PATH_MAX))
var ret: UnsafeMutablePointer<passwd>?
var info = passwd()
if user == nil || user == "" {
if !unixOK("Dir.getpwuid", getpwuid_r(geteuid(), &info, &buff, buff.count, &ret), file: file, line: line) {
return nil
}
user = String(validatingUTF8: info.pw_name)
}
if !unixOK("Dir.getpwnam \(user!.to_s)", getpwnam_r(user!, &info, &buff, buff.count, &ret), file: file, line: line) {
return nil
}
return String(validatingUTF8: info.pw_dir)
}
open class func mkdir(_ string: string_like, _ mode: Int = 0o755, file: StaticString = #file, line: UInt = #line) -> Bool {
return unixOK("Dir.mkdir '\(string.to_s)", Darwin.mkdir(string.to_s, mode_t(mode)), file: file, line: line)
}
open class var pwd: String? {
return getwd
}
open class func rmdir(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return delete(string, file: file, line: line)
}
open class func unlink(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
return delete(string, file: file, line: line)
}
// MARK: Instance methods
@discardableResult
open func close(_ file: StaticString = #file, line: UInt = #line) -> Bool {
let ok = unixOK("Dir.closedir '\(dirpath)'", closedir(unixDIR), file: file, line: line)
unixDIR = nil
return ok
}
@discardableResult
open func each(_ block: (String) -> ()) -> Dir {
while let name = read() {
block(name)
}
return self
}
open var fileno: Int {
return Int(dirfd(unixDIR))
}
open var inspect: String {
return dirpath
}
open var path: String {
return dirpath
}
open var pos: Int {
return Int(telldir(unixDIR))
}
open func read() -> String? {
let ent = readdir(unixDIR)
if ent != nil {
return withUnsafeMutablePointer (to: &ent!.pointee.d_name) {
String(validatingUTF8: UnsafeMutableRawPointer($0).assumingMemoryBound(to: CChar.self))
}
}
return nil
}
open func rewind() {
Darwin.rewinddir(unixDIR)
}
open func seek(_ pos: Int) {
seekdir(unixDIR, pos)
}
open var tell: Int {
return pos
}
open var to_a: [String] {
var out = [String]()
each {
(entry) in
out .append(entry)
}
return out
}
open var to_path: String {
return dirpath
}
deinit {
if unixDIR != nil {
close()
}
}
}