blob: 7af506638ebc5f27b06e46c560543ca4ec61d131 [file] [log] [blame]
Adam Lesinski16c4d152014-01-24 13:27:13 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
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
17#define LOG_TAG "misc"
18
19//
20// Miscellaneous utility functions.
21//
22#include <androidfw/misc.h>
23
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -080024#include "android-base/logging.h"
Adam Lesinski16c4d152014-01-24 13:27:13 -080025
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -080026#ifndef _WIN32
27#include <sys/statvfs.h>
28#include <sys/vfs.h>
29#endif // _WIN32
30
31#include <cstring>
32#include <cstdio>
33#include <errno.h>
34#include <sys/stat.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080035
36namespace android {
37
38/*
39 * Get a file's type.
40 */
41FileType getFileType(const char* fileName)
42{
43 struct stat sb;
44
45 if (stat(fileName, &sb) < 0) {
46 if (errno == ENOENT || errno == ENOTDIR)
47 return kFileTypeNonexistent;
48 else {
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -080049 PLOG(ERROR) << "getFileType(): stat(" << fileName << ") failed";
Adam Lesinski16c4d152014-01-24 13:27:13 -080050 return kFileTypeUnknown;
51 }
52 } else {
53 if (S_ISREG(sb.st_mode))
54 return kFileTypeRegular;
55 else if (S_ISDIR(sb.st_mode))
56 return kFileTypeDirectory;
57 else if (S_ISCHR(sb.st_mode))
58 return kFileTypeCharDev;
59 else if (S_ISBLK(sb.st_mode))
60 return kFileTypeBlockDev;
61 else if (S_ISFIFO(sb.st_mode))
62 return kFileTypeFifo;
Elliott Hughes1bf24812015-01-12 14:33:04 -080063#if defined(S_ISLNK)
Adam Lesinski16c4d152014-01-24 13:27:13 -080064 else if (S_ISLNK(sb.st_mode))
65 return kFileTypeSymlink;
Elliott Hughes1bf24812015-01-12 14:33:04 -080066#endif
67#if defined(S_ISSOCK)
Adam Lesinski16c4d152014-01-24 13:27:13 -080068 else if (S_ISSOCK(sb.st_mode))
69 return kFileTypeSocket;
70#endif
71 else
72 return kFileTypeUnknown;
73 }
74}
75
76/*
77 * Get a file's modification date.
78 */
79time_t getFileModDate(const char* fileName)
80{
81 struct stat sb;
82
83 if (stat(fileName, &sb) < 0)
84 return (time_t) -1;
85
86 return sb.st_mtime;
87}
88
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -080089#ifdef _WIN32
90// No need to implement these for Windows, the functions only matter on a device.
91bool isReadonlyFilesystem(const char*) {
92 return false;
93}
94bool isReadonlyFilesystem(int) {
95 return false;
96}
97#else // _WIN32
98bool isReadonlyFilesystem(const char* path) {
99 struct statfs sfs;
100 if (::statfs(path, &sfs)) {
101 PLOG(ERROR) << "isReadonlyFilesystem(): statfs(" << path << ") failed";
102 return false;
103 }
104 return (sfs.f_flags & ST_RDONLY) != 0;
105}
106
107bool isReadonlyFilesystem(int fd) {
108 struct statfs sfs;
109 if (::fstatfs(fd, &sfs)) {
110 PLOG(ERROR) << "isReadonlyFilesystem(): fstatfs(" << fd << ") failed";
111 return false;
112 }
113 return (sfs.f_flags & ST_RDONLY) != 0;
114}
115#endif // _WIN32
116
Adam Lesinski16c4d152014-01-24 13:27:13 -0800117}; // namespace android