-
Notifications
You must be signed in to change notification settings - Fork 3
/
File.h
65 lines (51 loc) · 2.07 KB
/
File.h
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
//----------------------------------------------------------------------------------------------------------------------
// File management routines
// Copyright (c) 2024 Samuel Gomes
//----------------------------------------------------------------------------------------------------------------------
#pragma once
#include <cstdint>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <sys/stat.h>
// These must be kept in sync with FileOps.bi
#define __FILE_ATTRIBUTE_DIRECTORY 1
#define __FILE_ATTRIBUTE_READOLY 2
#define __FILE_ATTRIBUTE_HIDDEN 4
#define __FILE_ATTRIBUTE_ARCHIVE 8
#define __FILE_ATTRIBUTE_SYSTEM 16
// TODO: Implement Win32 versions of these!
/// @brief Returns some basic attributes of a file or directory
/// @param pathName The path name to get the attribute for
/// @return A 32-bit value where each bit represents an attribute (see __FILE_* above)
inline uint32_t __File_GetAttributes(const char *pathName)
{
uint32_t attributes = 0;
struct stat info;
stat(pathName, &info);
// Read-only attribute
if (!(info.st_mode & S_IWUSR))
attributes |= __FILE_ATTRIBUTE_READOLY;
// Hidden attribute (files starting with '.')
if (pathName[0] == '.')
attributes |= __FILE_ATTRIBUTE_HIDDEN;
// System attribute (character devices, block devices, FIFOs)
if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode) || S_ISFIFO(info.st_mode))
attributes |= __FILE_ATTRIBUTE_SYSTEM;
// Directory or Archive attribute
if (S_ISDIR(info.st_mode))
attributes |= __FILE_ATTRIBUTE_DIRECTORY;
else
attributes |= __FILE_ATTRIBUTE_ARCHIVE; // Archive attribute for non-directory files
return attributes;
}
/// @brief Returns the 64-bit file size without opening the file
/// @param fileName The file name to get the size for
/// @return A 64-bit integer value (size)
inline int64_t __File_GetSize(const char *fileName)
{
struct stat64 st;
if (stat64(fileName, &st) == 0 && S_ISREG(st.st_mode))
return st.st_size;
return -1; // -1 to indicate file not found or not a regular file
}