xref: /aosp_15_r20/external/okio/okio/src/linuxMain/kotlin/okio/LinuxPosixVariant.kt (revision f9742813c14b702d71392179818a9e591da8620c)
1 /*
2  * Copyright (C) 2020 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio
17 
18 import kotlinx.cinterop.alloc
19 import kotlinx.cinterop.memScoped
20 import kotlinx.cinterop.ptr
21 import platform.posix.ENOENT
22 import platform.posix.S_IFDIR
23 import platform.posix.S_IFMT
24 import platform.posix.S_IFREG
25 import platform.posix.errno
26 import platform.posix.lstat
27 import platform.posix.stat
28 
variantMetadataOrNullnull29 internal actual fun PosixFileSystem.variantMetadataOrNull(path: Path): FileMetadata? {
30   return memScoped {
31     val stat = alloc<stat>()
32     if (lstat(path.toString(), stat.ptr) != 0) {
33       if (errno == ENOENT) return null
34       throw errnoToIOException(errno)
35     }
36     return@memScoped FileMetadata(
37       isRegularFile = stat.st_mode.toInt() and S_IFMT == S_IFREG,
38       isDirectory = stat.st_mode.toInt() and S_IFMT == S_IFDIR,
39       symlinkTarget = symlinkTarget(stat, path),
40       size = stat.st_size,
41       createdAtMillis = stat.st_ctim.epochMillis,
42       lastModifiedAtMillis = stat.st_mtim.epochMillis,
43       lastAccessedAtMillis = stat.st_atim.epochMillis,
44     )
45   }
46 }
47