-
Notifications
You must be signed in to change notification settings - Fork 1
/
finder_info.cpp
182 lines (146 loc) · 4.16 KB
/
finder_info.cpp
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
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MSYS__)
#define AFP_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define XATTR_FINDERINFO_NAME "AFP_AfpInfo"
#else
#include <unistd.h>
#include <fcntl.h>
#endif
#if defined(__APPLE__)
#include <sys/xattr.h>
#endif
#if defined(__linux__)
#include <sys/xattr.h>
#define XATTR_FINDERINFO_NAME "user.com.apple.FinderInfo"
#endif
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/extattr.h>
#endif
#if defined(_AIX)
#include <sys/ea.h>
#endif
#include <string>
#include <cstring>
#include <cstdint>
#ifndef XATTR_FINDERINFO_NAME
#define XATTR_FINDERINFO_NAME "com.apple.FinderInfo"
#endif
namespace {
bool file_type_to_finder_info(uint8_t *buffer, uint16_t file_type, uint32_t aux_type) {
if (file_type > 0xff || aux_type > 0xffff) return false;
if (!file_type && aux_type == 0x0000) {
memcpy(buffer, "BINApdos", 8);
return true;
}
if (file_type == 0x04 && aux_type == 0x0000) {
memcpy(buffer, "TEXTpdos", 8);
return true;
}
if (file_type == 0xff && aux_type == 0x0000) {
memcpy(buffer, "PSYSpdos", 8);
return true;
}
if (file_type == 0xb3 && aux_type == 0x0000) {
memcpy(buffer, "PS16pdos", 8);
return true;
}
if (file_type == 0xd7 && aux_type == 0x0000) {
memcpy(buffer, "MIDIpdos", 8);
return true;
}
if (file_type == 0xd8 && aux_type == 0x0000) {
memcpy(buffer, "AIFFpdos", 8);
return true;
}
if (file_type == 0xd8 && aux_type == 0x0001) {
memcpy(buffer, "AIFCpdos", 8);
return true;
}
if (file_type == 0xe0 && aux_type == 0x0005) {
memcpy(buffer, "dImgdCpy", 8);
return true;
}
memcpy(buffer, "p pdos", 8);
buffer[1] = (file_type) & 0xff;
buffer[2] = (aux_type >> 8) & 0xff;
buffer[3] = (aux_type) & 0xff;
return true;
}
}
#if defined(AFP_WIN32)
int set_prodos_file_type(const std::string &path, uint16_t fileType, uint32_t auxType) {
#pragma pack(push, 2)
struct AFP_Info {
uint32_t magic;
uint32_t version;
uint32_t file_id;
uint32_t backup_date;
uint8_t finder_info[32];
uint16_t prodos_file_type;
uint32_t prodos_aux_type;
uint8_t reserved[6];
};
#pragma pack(pop)
HANDLE h;
BOOL ok;
AFP_Info info;
memset(&info, 0, sizeof(info));
// little endian.
info.magic = 0x00504641;
info.version = 0x00010000;
info.backup_date = 0x80000000;
info.prodos_file_type = fileType;
info.prodos_aux_type = auxType;
file_type_to_finder_info(&info.finder_info, fileType, auxType);
std::string xpath(path);
xpath += ":" XATTR_FINDERINFO_NAME;
h = CreateFileA(xpath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h == INVALID_HANDLE_VALUE) return -1;
ok = WriteFile(h, &info, sizeof(info), nullptr, nullptr);
CloseHandle(h);
return ok ? 0 : -1;
}
#elif defined(__sun__)
int set_prodos_file_type(const std::string &path, uint16_t fileType, uint32_t auxType) {
int pfd;
int fd;
int ok;
uint8_t finder_info[32];
memset(finder_info, 0, sizeof(finder_info));
if (!file_type_to_finder_info(&info.finder_info, fileType, auxType))
return -1;
pfd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
if (pfd < 0) return -1;
fd = opeant(pfd, XATTR_FINDERINFO_NAME, O_WRONLY | O_CREAT | O_TRUNC | O_XATTR, 0666);
if (fd < 0) {
close(pfd);
return -1;
}
ok = write(fd, finder_info, sizeof(finder_info));
close(fd);
close(pfd);
return ok == sizeof(finder_info);
}
#else
int set_prodos_file_type(const std::string &path, uint16_t fileType, uint32_t auxType) {
int ok;
uint8_t finder_info[32];
memset(finder_info, 0, sizeof(finder_info));
if (!file_type_to_finder_info(finder_info, fileType, auxType))
return -1;
#if defined(__APPLE__)
ok = setxattr(path.c_str(), XATTR_FINDERINFO_NAME, finder_info, sizeof(finder_info), 0, 0);
#elif defined(__linux__)
ok = setxattr(path.c_str(), XATTR_FINDERINFO_NAME, finder_info, sizeof(finder_info), 0);
#elif defined(__FreeBSD__)
ok = extattr_set_file(path.c_str(), EXTATTR_NAMESPACE_USER, XATTR_FINDERINFO_NAME, finder_info, sizeof(finder_info)) == sizeof(finder_info) ? 0 : -1;
#elif defined(_AIX)
ok = setea(path.c_str(), XATTR_FINDERINFO_NAME, finder_info, sizeof(finder_info), 0);
#else
ok = -1;
#endif
return ok;
}
#endif