-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.cpccFileSystemMiniOSX.h
144 lines (117 loc) · 4.41 KB
/
io.cpccFileSystemMiniOSX.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
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
/* *****************************************
* File: cpccFileSystemMiniOSX.h
* Version: see function getClassVersion()
* Purpose: Portable (cross-platform), light-weight, file system library
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2013 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com
* http://www.24hsoftware.com/portable-cpp-filesystem-library
* Download: https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#include <string>
#include <pwd.h>
#include <unistd.h> // for access on unix, mac
#include <TargetConditionals.h>
#if TARGET_OS_MAC
#if TARGET_OS_IPHONE
#include <Foundation/Foundation.h>
#else
#include <libproc.h>
#include <Cocoa/Cocoa.h>
#endif
#endif
#include "cpccUnicodeSupport.h"
#include "fs.cpccUserFolders.h"
class fileSystemOSX_helper
{
public:
static const bool createFolder(const cpcc_char *aFilename, const mode_t permissions)
{
NSString *path = [NSString stringWithCString:aFilename encoding:NSUTF8StringEncoding];
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
return false;
}
return true;
}
#if !(TARGET_OS_IPHONE)
static const bool createFolder_Linux(const cpcc_char *aFilename, const mode_t permissions)
{
/* tutorial:
int mkdir(const char *pathname, mode_t mode);
The argument mode specifies the permissions to use.
It is modified by the processís umask in the usual way:
the permissions of the created directory are (mode & ~umask & 0777).
*/
mode_t previous_umask = umask(0);
int rc=mkdir( aFilename, permissions );
// chmod(): This POSIX function is deprecated. Use the ISO C++ conformant _chmod instead.
// http://msdn.microsoft.com/en-us/library/1z319a54.aspx
// chmod(aFilename, permissions);
umask(previous_umask);
if (rc!=0)
std::cerr << "Error no " << rc << " (" << strerror(rc) << ") while creating directory:" << aFilename << "\n";
return (rc == 0);
}
#endif
static const bool startsWithTilde_OSX(const char *aPath)
{
if (aPath)
if (strlen(aPath)>0)
if (aPath[0]=='~')
return true;
return false;
}
static const std::string expandTilde_OSX(const char *aPath)
{
std::string result(aPath);
if (result.length()>0)
if (result.at(0)=='~')
result.replace(0,1, cpccUserFolders::getUserHomeDir());
return result;
}
static const mode_t getFileOrFolderPermissions_OSX(const cpcc_char *aFilename)
{
#if TARGET_OS_IOS
NSString *path = [NSString stringWithCString:aFilename encoding:NSUTF8StringEncoding];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
// Get the current permissions
NSDictionary *currentPerms = [fm attributesOfFileSystemForPath:path error:&error];
if (currentPerms)
{
// The corresponding value is an NSNumber object.
// Use the shortValue method to retrieve the integer value for the permissions.
mode_t permissions = [currentPerms[NSFilePosixPermissions] shortValue];
return permissions;
/*
// Update the permissions with the new permission
NSMutableDictionary *attributes = [currentPerms mutableCopy];
attributes[NSFilePosixPermissions] = @(0444);
if (![fm setAttributes:attributes ofItemAtPath:path error:&error]) {
NSLog(@"Unable to make %@ read-only: %@", path, error);
*/
}
else
NSLog(@"Unable to read permissions for %@: %@", path, error);
return 0;
#else
cpcc_string finalPath(expandTilde_OSX(aFilename));
struct stat statInfo;
stat(finalPath.c_str(), &statInfo);
// Octal numbers begin with 0. hex numbers begin with 0x.
return statInfo.st_mode & 0777;
#endif
}
};