forked from nickzman/symboliclinker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolicLinker.m
199 lines (175 loc) · 8.78 KB
/
SymbolicLinker.m
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* SymbolicLinker.m
* SymbolicLinker
*
* Created by Nick Zitzmann on Sun Mar 21 2004.
* Copyright (c) 2004 Nick Zitzmann. All rights reserved.
*
*/
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SymbolicLinker.h"
#include <stdio.h>
#include <unistd.h>
#ifdef USE_COCOA
#import <Cocoa/Cocoa.h>
#else
#include "MoreFinderEvents.h"
#endif
inline OSStatus StandardAlertCF(AlertType inAlertType, CFStringRef inError, CFStringRef inExplanation, const AlertStdCFStringAlertParamRec *inAlertParam, SInt16 *outItemHit);
inline bool SLIsEqualToString(CFStringRef theString1, CFStringRef theString2);
void MakeSymbolicLinkToDesktop(CFURLRef url)
{
FSRef desktopFolder;
CFURLRef desktopFolderURL, destinationURL;
CFStringRef fileName, fileNameWithSymlinkExtension;
char sourcePath[PATH_MAX], destinationPath[PATH_MAX];
// Set up the destination path...
FSFindFolder(kUserDomain, kDesktopFolderType, false, &desktopFolder);
desktopFolderURL = CFURLCreateFromFSRef(kCFAllocatorDefault, &desktopFolder);
fileName = CFURLCopyLastPathComponent(url);
if (SLIsEqualToString(fileName, CFSTR("/"))) // true if the user is making a symlink to the boot volume
{
CFRelease(fileName);
fileName = CFURLCopyFileSystemPath(url, kCFURLHFSPathStyle); // use CoreFoundation to figure out the boot volume's name
}
fileNameWithSymlinkExtension = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ symlink"), fileName);
destinationURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, desktopFolderURL, fileNameWithSymlinkExtension, false);
CFURLGetFileSystemRepresentation(destinationURL, true, (UInt8 *)destinationPath, PATH_MAX);
CFURLGetFileSystemRepresentation(url, false, (UInt8 *)sourcePath, PATH_MAX);
// Check to make sure that destPath doesn't point to something that already exists.
if (access(destinationPath, F_OK) == 0)
{
int tries;
for (tries = 1 ; tries < INT_MAX && access(destinationPath, F_OK) == 0 ; tries++)
{
// We format it like how Apple does with aliases in this case.
//snprintf(destPath, PATH_MAX, "%s/%s symlink %d", cDesktopFolder, filename, tries);
CFRelease(fileNameWithSymlinkExtension);
CFRelease(destinationURL);
fileNameWithSymlinkExtension = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ symlink %d"), fileName, tries);
destinationURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, desktopFolderURL, fileNameWithSymlinkExtension, false);
CFURLGetFileSystemRepresentation(destinationURL, true, (UInt8 *)destinationPath, PATH_MAX);
}
// In the extremely unlikely event that we tried 2^31-1 tries to get a path name, and didn't succeed, then just stop...
if (tries == INT_MAX)
{
goto done;
}
}
// Now we make the link.
if (symlink(sourcePath, destinationPath) != 0)
{
CFStringRef CFMyerror = CFCopyLocalizedStringFromTableInBundle(CFSTR("Could not make the symbolic link, because the following error occurred: %d (%s)"), CFSTR("Localizable"), SLOurBundle(), "Error message");
CFStringRef CFMyerrorFormatted = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFMyerror, errno, strerror(errno));
SInt16 ignored;
// An error occurred, so set up a standard alert box and run it...
StandardAlertCF(kAlertCautionAlert, CFMyerrorFormatted, NULL, NULL, &ignored);
CFRelease(CFMyerror);
CFRelease(CFMyerrorFormatted);
}
else
{
// If all went well, then let the Finder know that the file system has changed on the user's desktop.
#ifndef USE_COCOA
MoreFEUpdateItemFSRef(&desktopFolder);
#endif
}
done:
CFRelease(desktopFolderURL);
CFRelease(fileName);
CFRelease(fileNameWithSymlinkExtension);
CFRelease(destinationURL);
}
// Here is where we make the symbolic link, given the path.
void MakeSymbolicLink(CFURLRef url)
{
CFURLRef urlNoPathComponent = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, url);
CFStringRef pathStringNoPathComponent = CFURLCopyFileSystemPath(urlNoPathComponent, kCFURLPOSIXPathStyle);
char destPath[PATH_MAX], originalDestPath[PATH_MAX], pathFolder[PATH_MAX];
// First check to see if we're making a link to a disk.
if (SLIsEqualToString(pathStringNoPathComponent, CFSTR("/Volumes")) || SLIsEqualToString(pathStringNoPathComponent, CFSTR("")) || SLIsEqualToString(pathStringNoPathComponent, CFSTR("/..")) || SLIsEqualToString(pathStringNoPathComponent, CFSTR("/")))
{
MakeSymbolicLinkToDesktop(url);
goto done; // clean up and exit the function
}
CFURLGetFileSystemRepresentation(url, false, (UInt8 *)originalDestPath, PATH_MAX);
CFURLGetFileSystemRepresentation(urlNoPathComponent, false, (UInt8 *)pathFolder, PATH_MAX);
// Set up the destination path...
snprintf(destPath, PATH_MAX, "%s symlink", originalDestPath);
// Check to make sure that destPath doesn't point to something that already exists.
if (access(destPath, F_OK) == 0)
{
int tries;
for (tries = 1 ; tries < INT_MAX && access(destPath, F_OK) == 0 ; tries++)
{
// We format it like how Apple does with aliases in this case.
snprintf(destPath, PATH_MAX, "%s symlink %d", originalDestPath, tries);
}
// In the extremely unlikely event that we tried 2^31-1 tries to get a path name, and didn't succeed, then just stop...
if (tries == INT_MAX)
{
goto done;
}
}
// Now we make the link.
if (symlink(originalDestPath, destPath) != 0)
{
if ((errno == EACCES) || (errno == EROFS))
{
// We get to this point if it was a "permission denied" or "read-only" error.
// Let's try it again, but we'll make it on the desktop.
MakeSymbolicLinkToDesktop(url);
}
else
{
CFStringRef CFMyerror = CFCopyLocalizedStringFromTableInBundle(CFSTR("Could not make the symbolic link, because the following error occurred: %d (%s)"), CFSTR("Localizable"), SLOurBundle(), "Error message");
CFStringRef CFMyerrorFormatted = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFMyerror, errno, strerror(errno));
SInt16 ignored;
// An error occurred, so set up a standard alert box and run it...
StandardAlertCF(kAlertCautionAlert, CFMyerrorFormatted, NULL, NULL, &ignored);
CFRelease(CFMyerror);
CFRelease(CFMyerrorFormatted);
}
}
else
{
#ifndef USE_COCOA
FSRef pathFolderRef;
// If the operation went OK, then we tell the Finder that the file system has changed.
FSPathMakeRef((UInt8 *)pathFolder, &pathFolderRef, NULL);
MoreFEUpdateItemFSRef(&pathFolderRef);
#endif
}
done:
CFRelease(urlNoPathComponent);
CFRelease(pathStringNoPathComponent);
}
inline CFBundleRef SLOurBundle(void)
{
CFStringRef bundleCFStringRef = CFSTR("net.comcast.home.seiryu.SymbolicLinker");
return CFBundleGetBundleWithIdentifier(bundleCFStringRef);
}
inline OSStatus StandardAlertCF(AlertType inAlertType, CFStringRef inError, CFStringRef inExplanation, const AlertStdCFStringAlertParamRec *inAlertParam, SInt16 *outItemHit)
{
OSStatus err = noErr;
#ifdef USE_COCOA
CFUserNotificationDisplayAlert(0.0, kCFUserNotificationPlainAlertLevel, NULL, NULL, NULL, (inError ? inError : CFSTR("")), inExplanation, NULL, NULL, NULL, NULL);
#else
DialogRef outAlert;
if ((err = CreateStandardAlert(inAlertType, inError, inExplanation, inAlertParam, &outAlert)) == noErr)
{
err = RunStandardAlert(outAlert, NULL, outItemHit);
}
#endif
return err;
}
inline bool SLIsEqualToString(CFStringRef theString1, CFStringRef theString2)
{
return (CFStringCompare(theString1, theString2, 0) == kCFCompareEqualTo);
}