Skip to content

Commit

Permalink
Use network URL for adding
Browse files Browse the repository at this point in the history
Auto mounts the share if possible.
If the share is already mounted tries to guess the mounting path.
  • Loading branch information
kkapsner committed Mar 24, 2015
1 parent df48477 commit 7877ae9
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 14 deletions.
4 changes: 4 additions & 0 deletions addNetworkFavorite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
8A69CC751AC05C710053BE5A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8A69CC741AC05C710053BE5A /* main.m */; };
8A69CC801AC07BF90053BE5A /* NetFS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A69CC7F1AC07BF90053BE5A /* NetFS.framework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -25,13 +26,15 @@
/* Begin PBXFileReference section */
8A69CC711AC05C710053BE5A /* addNetworkFavorite */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = addNetworkFavorite; sourceTree = BUILT_PRODUCTS_DIR; };
8A69CC741AC05C710053BE5A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
8A69CC7F1AC07BF90053BE5A /* NetFS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetFS.framework; path = System/Library/Frameworks/NetFS.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
8A69CC6E1AC05C710053BE5A /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
8A69CC801AC07BF90053BE5A /* NetFS.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -41,6 +44,7 @@
8A69CC681AC05C710053BE5A = {
isa = PBXGroup;
children = (
8A69CC7F1AC07BF90053BE5A /* NetFS.framework */,
8A69CC731AC05C710053BE5A /* addNetworkFavorite */,
8A69CC721AC05C710053BE5A /* Products */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>B3151D2C-0E7F-43BF-BD75-E73EBAC6D574</string>
<key>IDESourceControlProjectName</key>
<string>addNetworkFavorite</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>906DA6B62C16D7F803D237D4712D6257117C4A5C</key>
<string>https://github.com/kkapsner/addNetworkFavorite.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>addNetworkFavorite.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>906DA6B62C16D7F803D237D4712D6257117C4A5C</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/kkapsner/addNetworkFavorite.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>906DA6B62C16D7F803D237D4712D6257117C4A5C</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>906DA6B62C16D7F803D237D4712D6257117C4A5C</string>
<key>IDESourceControlWCCName</key>
<string>addNetworkFavorite</string>
</dict>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
151 changes: 137 additions & 14 deletions addNetworkFavorite/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,157 @@
//

#import <Foundation/Foundation.h>
@import NetFS;

int addFavorite(NSString *path, NSString *displayName){
@autoreleasepool {
NSLog(@"Adding favorite: %@ (%@)", path, displayName);

LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);

CFURLRef mountURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];

LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(
favoriteItems,
kLSSharedFileListItemLast,
(__bridge CFStringRef)(displayName),
NULL,
mountURL,
NULL,
NULL
);

if (item){
CFRelease(item);
return 0;
}
else {
NSLog(@"Unable to add favorite.");
return 1;
}
}
}

int checkDirectoryExists(NSString *dir){
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath: dir isDirectory: &isDirectory] && isDirectory){
return 0;
}
else {
return 1;
}
}

void removeFirstPathPart(NSString **path, NSString **part){
NSRange backPartStartRange = [*path
rangeOfString: @"/"
options: NSLiteralSearch
range: NSMakeRange(1, (*path).length - 1)
];

if (backPartStartRange.location == NSNotFound && backPartStartRange.length == 0){
*part = *path;
*path = @"";
}
else {
*part = [*path substringToIndex: backPartStartRange.location];
*path = [*path substringFromIndex: backPartStartRange.location];
}
}

int getPath(NSString *base, NSString *tryPath, NSString **finalPath){
NSString *backPart = [NSString stringWithFormat: @"%@", tryPath];
NSString *pathPart = NULL;
NSString *path = @"";
NSString *testPath;

while (backPart.length){
removeFirstPathPart(&backPart, &pathPart);
testPath = [NSString stringWithFormat:@"%@%@%@/", base, path, pathPart];
NSLog(@" checking %@", testPath);
if (!checkDirectoryExists(testPath)){
path = [path stringByAppendingString: pathPart];
}
}
if (path.length){
*finalPath = [NSString stringWithFormat:@"%@%@/", base, path];
return checkDirectoryExists(*finalPath);
}
else {
return 1;
}
}
int guessDefaultMountPoint(NSString *sharePath, NSString **mountPoint){
NSURL *url = [NSURL URLWithString: sharePath];
return getPath(@"/Volumes", url.path, mountPoint);
}
int getMountPoint(NSString *sharePath, NSString *mountBase, NSString **mountPoint){
NSURL *url = [NSURL URLWithString: sharePath];
return getPath(mountBase, url.path, mountPoint);
}

int main(int argc, const char * argv[]) {
@autoreleasepool {
if (argc < 2){
NSLog(@"One arguments required: mounted volume path. Second argument (display name) optional.");
if (argc < 3){
NSLog(@"Two arguments required: shared network path and folder to add to favorites (use \"\" for the root). Third argument (display name) is optional.");
return 1;
}
else {
NSString *mountPath = [NSString stringWithCString:argv[1] encoding:[NSString defaultCStringEncoding]];
NSString *sharePath = [NSString stringWithCString:argv[1] encoding:[NSString defaultCStringEncoding]];
NSString *sharedFolder = [NSString stringWithCString:argv[2] encoding:[NSString defaultCStringEncoding]];
NSString *displayName;
if (argc < 3){
if (argc < 4){
displayName = NULL;
}
else {
displayName = [NSString stringWithCString:argv[2] encoding:[NSString defaultCStringEncoding]];
displayName = [NSString stringWithCString:argv[3] encoding:[NSString defaultCStringEncoding]];
}
NSLog(@"Adding favorite: %@ (%@)", mountPath, displayName);

LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);


CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:mountPath];
NSLog(@"Mounting share: %@", sharePath);

NSURL *shareURL = [NSURL URLWithString: sharePath];
CFArrayRef mountpoints = NULL;
UInt32 error = NetFSMountURLSync(
(__bridge CFURLRef)(shareURL),
NULL,
NULL,
NULL,
NULL,
NULL,
&mountpoints
);

LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemLast, (__bridge CFStringRef)(displayName), NULL, url, NULL, NULL);

if (item){
CFRelease(item);
if (error){
NSLog(@"Unable to mount (%d)", error);
if (error == 17){ // already mounted
if (mountpoints && CFArrayGetCount(mountpoints)){
NSLog(@"%@", CFArrayGetValueAtIndex(mountpoints, 0));
}
else {
NSLog(@"Already mounted but mount point unknown trying to guess...");
NSString *mountPoint;
if (guessDefaultMountPoint(sharePath, &mountPoint)){
NSLog(@"... unable to guess.");
return 1;
}
else {

NSLog(@"... %@", mountPoint);
return addFavorite([NSString stringWithFormat: @"%@%@", mountPoint, sharedFolder], displayName);
}
}
}
}
else {
NSLog(@"Mounted");
if (CFArrayGetCount(mountpoints)){
NSString *mountPoint = CFArrayGetValueAtIndex(mountpoints, 0);
return addFavorite([NSString stringWithFormat: @"%@/%@", mountPoint, sharedFolder], displayName);
}
else {
NSLog(@"No mount point found.");
return 1;
}
}
}
}
Expand Down
Binary file modified bin/addNetworkFavorite
Binary file not shown.

0 comments on commit 7877ae9

Please sign in to comment.