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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Linux) || os(Android)
import SystemPackage
#if canImport(Darwin)
import Darwin
import CNIODarwin
#elseif canImport(Glibc)
import Glibc
import CNIOLinux
#elseif canImport(Musl)
import Musl
import CNIOLinux
#endif
// MARK: - system
/// openat(2): Open or create a file for reading or writing
func system_fdopenat(
_ fd: FileDescriptor.RawValue,
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, path, oflag)
}
#endif
return openat(fd, path, oflag)
}
/// openat(2): Open or create a file for reading or writing
func system_fdopenat(
_ fd: FileDescriptor.RawValue,
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, path, oflag, mode)
}
#endif
return openat(fd, path, oflag, mode)
}
/// stat(2): Get file status
func system_stat(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ info: inout CInterop.Stat
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return stat(path, &info)
}
/// lstat(2): Get file status
internal func system_lstat(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ info: inout CInterop.Stat
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return lstat(path, &info)
}
/// fstat(2): Get file status
internal func system_fstat(
_ fd: FileDescriptor.RawValue,
_ info: inout CInterop.Stat
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd)
}
#endif
return fstat(fd, &info)
}
/// fchmod(2): Change mode of file
internal func system_fchmod(
_ fd: FileDescriptor.RawValue,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, mode)
}
#endif
return fchmod(fd, mode)
}
/// fsync(2): Synchronize modifications to a file to permanent storage
internal func system_fsync(
_ fd: FileDescriptor.RawValue
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd)
}
#endif
return fsync(fd)
}
/// mkdir(2): Make a directory file
internal func system_mkdir(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path, mode)
}
#endif
return mkdir(path, mode)
}
/// symlink(2): Make symolic link to a file
internal func system_symlink(
_ destination: UnsafePointer<CInterop.PlatformChar>,
_ source: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(destination, source)
}
#endif
return symlink(destination, source)
}
/// readlink(2): Read value of a symolic link
internal func system_readlink(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
_ bufferSize: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(path, buffer, bufferSize)
}
#endif
return readlink(path, buffer, bufferSize)
}
/// flistxattr(2): List extended attribute names
internal func system_flistxattr(
_ fd: FileDescriptor.RawValue,
_ namebuf: UnsafeMutablePointer<CChar>?,
_ size: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(fd, namebuf, size)
}
#endif
#if canImport(Darwin)
// The final parameter is 'options'; there is no equivalent on Linux.
return flistxattr(fd, namebuf, size, 0)
#elseif canImport(Glibc) || canImport(Musl)
return flistxattr(fd, namebuf, size)
#endif
}
/// fgetxattr(2): Get an extended attribute value
internal func system_fgetxattr(
_ fd: FileDescriptor.RawValue,
_ name: UnsafePointer<CChar>,
_ value: UnsafeMutableRawPointer?,
_ size: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(fd, name, value, size)
}
#endif
#if canImport(Darwin)
// Penultimate parameter is position which is reserved and should be zero.
// The final parameter is 'options'; there is no equivalent on Linux.
return fgetxattr(fd, name, value, size, 0, 0)
#elseif canImport(Glibc) || canImport(Musl)
return fgetxattr(fd, name, value, size)
#endif
}
/// fsetxattr(2): Set an extended attribute value
internal func system_fsetxattr(
_ fd: FileDescriptor.RawValue,
_ name: UnsafePointer<CChar>,
_ value: UnsafeRawPointer?,
_ size: Int
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, name, value, size)
}
#endif
// The final parameter is 'options'/'flags' on Darwin/Linux respectively.
#if canImport(Darwin)
// Penultimate parameter is position which is reserved and should be zero.
return fsetxattr(fd, name, value, size, 0, 0)
#elseif canImport(Glibc) || canImport(Musl)
return fsetxattr(fd, name, value, size, 0)
#endif
}
/// fremovexattr(2): Remove an extended attribute value
internal func system_fremovexattr(
_ fd: FileDescriptor.RawValue,
_ name: UnsafePointer<CChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, name)
}
#endif
#if canImport(Darwin)
// The final parameter is 'options'; there is no equivalent on Linux.
return fremovexattr(fd, name, 0)
#elseif canImport(Glibc) || canImport(Musl)
return fremovexattr(fd, name)
#endif
}
/// rename(2): Change the name of a file
internal func system_rename(
_ old: UnsafePointer<CInterop.PlatformChar>,
_ new: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(old, new)
}
#endif
return rename(old, new)
}
#if canImport(Darwin)
internal func system_renamex_np(
_ old: UnsafePointer<CInterop.PlatformChar>,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CUnsignedInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(old, new, flags)
}
#endif
return renamex_np(old, new, flags)
}
#endif
#if canImport(Glibc) || canImport(Musl)
internal func system_renameat2(
_ oldFD: FileDescriptor.RawValue,
_ old: UnsafePointer<CInterop.PlatformChar>,
_ newFD: FileDescriptor.RawValue,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CUnsignedInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(oldFD, old, newFD, new, flags)
}
#endif
return CNIOLinux_renameat2(oldFD, old, newFD, new, flags)
}
#endif
/// link(2): Creates a new link for a file.
#if canImport(Glibc) || canImport(Musl)
internal func system_linkat(
_ oldFD: FileDescriptor.RawValue,
_ old: UnsafePointer<CInterop.PlatformChar>,
_ newFD: FileDescriptor.RawValue,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(oldFD, old, newFD, new, flags)
}
#endif
return linkat(oldFD, old, newFD, new, flags)
}
#endif
/// link(2): Creates a new link for a file.
internal func system_link(
_ old: UnsafePointer<CInterop.PlatformChar>,
_ new: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(old, new)
}
#endif
return link(old, new)
}
/// unlink(2): Remove a directory entry.
internal func system_unlink(
_ path: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return unlink(path)
}
#if canImport(Glibc) || canImport(Musl)
/// sendfile(2): Transfer data between descriptors
internal func system_sendfile(
_ outFD: CInt,
_ inFD: CInt,
_ offset: off_t,
_ count: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(outFD, inFD, offset, count)
}
#endif
var offset = offset
return sendfile(outFD, inFD, &offset, count)
}
#endif
// MARK: - libc
/// fdopendir(3): Opens a directory stream for the file descriptor
internal func libc_fdopendir(
_ fd: FileDescriptor.RawValue
) -> CInterop.DirPointer {
return fdopendir(fd)!
}
/// readdir(3): Returns a pointer to the next directory entry
internal func libc_readdir(
_ dir: CInterop.DirPointer
) -> UnsafeMutablePointer<CInterop.DirEnt>? {
return readdir(dir)
}
/// readdir(3): Closes the directory stream and frees associated structures
internal func libc_closedir(
_ dir: CInterop.DirPointer
) -> CInt {
return closedir(dir)
}
/// remove(3): Remove directory entry
internal func libc_remove(
_ path: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return remove(path)
}
#if canImport(Darwin)
/// copyfile(3): Copy a file from one file to another.
internal func libc_fcopyfile(
_ from: CInt,
_ to: CInt,
_ state: copyfile_state_t?,
_ flags: copyfile_flags_t
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(from, to, state, flags)
}
#endif
return fcopyfile(from, to, state, flags)
}
#endif
/// getcwd(3): Get working directory pathname
internal func libc_getcwd(
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
_ size: Int
) -> UnsafeMutablePointer<CInterop.PlatformChar>? {
return getcwd(buffer, size)
}
/// confstr(3)
#if !os(Android)
internal func libc_confstr(
_ name: CInt,
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
_ size: Int
) -> Int {
return confstr(name, buffer, size)
}
#endif
/// fts(3)
#if os(Android)
internal func libc_fts_open(
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>],
_ options: CInt
) -> UnsafeMutablePointer<CInterop.FTS> {
return fts_open(path, options, nil)!
}
#else
internal func libc_fts_open(
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>?],
_ options: CInt
) -> UnsafeMutablePointer<CInterop.FTS> {
return fts_open(path, options, nil)
}
#endif
/// fts(3)
internal func libc_fts_read(
_ fts: UnsafeMutablePointer<CInterop.FTS>
) -> UnsafeMutablePointer<CInterop.FTSEnt>? {
return fts_read(fts)
}
/// fts(3)
internal func libc_fts_close(
_ fts: UnsafeMutablePointer<CInterop.FTS>
) -> CInt {
return fts_close(fts)
}
#endif
|