Skip to content

Commit c1dcfbb

Browse files
authored
Installer (macOS): Remove mount dir on keybase uninstall (keybase#4147)
1 parent dc98e0e commit c1dcfbb

13 files changed

Lines changed: 224 additions & 84 deletions

File tree

‎go/install/install_osx.go‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func Uninstall(context Context, components []string, log Log) keybase1.Uninstall
589589
return newUninstallResult(componentResults)
590590
}
591591

592-
// UninstallKBFS uninstalls all KBFS services and unmounts the directory
592+
// UninstallKBFS uninstalls all KBFS services, unmounts and removes the directory
593593
func UninstallKBFS(runMode libkb.RunMode, mountDir string, forceUnmount bool, log Log) error {
594594
err := uninstallKBFSServices(runMode, log)
595595
if err != nil {
@@ -618,11 +618,35 @@ func UninstallKBFS(runMode libkb.RunMode, mountDir string, forceUnmount bool, lo
618618
if !empty {
619619
return fmt.Errorf("Mount has files after unmounting: %s", mountDir)
620620
}
621-
// TODO: We should remove the mountPath via trashDir(g, mountPath) but given
622-
// permissions of /keybase we'll need the privileged tool to do it instead.
621+
622+
appPath, err := AppBundleForPath()
623+
if err != nil {
624+
return err
625+
}
626+
log.Info("Removing %s", mountDir)
627+
if err := removeMountDir(appPath, string(runMode), log); err != nil {
628+
return fmt.Errorf("Error removing mount dir: %s", err)
629+
}
630+
623631
return nil
624632
}
625633

634+
func nativeInstallerAppBundlePath(appPath string) string {
635+
return filepath.Join(appPath, "Contents/Resources/KeybaseInstaller.app")
636+
}
637+
638+
func nativeInstallerAppBundleExecPath(appPath string) string {
639+
return filepath.Join(nativeInstallerAppBundlePath(appPath), "Contents/MacOS/Keybase")
640+
}
641+
642+
func removeMountDir(appPath string, runMode string, log Log) error {
643+
// We need the installer to remove the mount directory (since it's in the root, only the helper tool can do it)
644+
cmd := exec.Command(nativeInstallerAppBundleExecPath(appPath), fmt.Sprintf("--run-mode=%s", runMode), "--uninstall-mountdir")
645+
output, err := cmd.CombinedOutput()
646+
log.Debug("Output (uninstall-mountdir): %s", string(output))
647+
return err
648+
}
649+
626650
// AutoInstallWithStatus runs the auto install and returns a result
627651
func AutoInstallWithStatus(context Context, binPath string, force bool, log Log) keybase1.InstallResult {
628652
_, res, err := autoInstall(context, binPath, force, log)

‎osx/Installer/Installer.m‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,20 @@ - (void)run {
3939

4040
[KBAppearance setCurrentAppearance:[KBUIAppearance appearance]];
4141

42+
DDLogDebug(@"Version: %@", NSBundle.mainBundle.infoDictionary[(NSString *)kCFBundleVersionKey]);
43+
4244
GBSettings *settings = [GBSettings settingsWithName:@"Settings" parent:nil];
4345
#if DEBUG
4446
[settings setObject:@"/Applications/Keybase.app" forKey:@"app-path"];
4547
// [self.settings setObject:@"/Volumes/Keybase/Keybase.app" forKey:@"app-path"];
4648
[settings setObject:@"prod" forKey:@"run-mode"];
4749
#endif
4850
_settings = [[Settings alloc] initWithSettings:settings];
51+
NSError *parseError = nil;
52+
if (![_settings parseArgs:&parseError]) {
53+
[self exit:KBExitError];
54+
return;
55+
}
4956

5057
if ([_settings isUninstall]) {
5158
[self uninstall];

‎osx/Installer/Uninstaller.m‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ + (void)uninstallWithSettings:(Settings *)settings completion:(KBCompletion)comp
2626
return;
2727
}
2828
}
29+
if (settings.uninstallOptions & UninstallOptionMountDir) {
30+
KBMountDir *mountDir = [[KBMountDir alloc] initWithConfig:environment.config helperTool:environment.helperTool];
31+
[installables addObject:mountDir];
32+
}
2933
[KBUninstaller uninstall:installables completion:completion];
3034
}
3135

‎osx/KBKit/KBKit.xcodeproj/project.pbxproj‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@
349349
006D68141C7D281400B1CA0B /* KBSharedFileList.m in Sources */ = {isa = PBXBuildFile; fileRef = 006D68121C7D281400B1CA0B /* KBSharedFileList.m */; };
350350
008949981BFBB60C00CB605A /* KBCustomEnvView.m in Sources */ = {isa = PBXBuildFile; fileRef = 008949961BFBB60C00CB605A /* KBCustomEnvView.m */; };
351351
008949991BFBB60C00CB605A /* KBCustomEnvView.h in Headers */ = {isa = PBXBuildFile; fileRef = 008949971BFBB60C00CB605A /* KBCustomEnvView.h */; };
352+
009ABE521D6E577A00C724B0 /* KBMountDir.h in Headers */ = {isa = PBXBuildFile; fileRef = 009ABE501D6E577A00C724B0 /* KBMountDir.h */; };
353+
009ABE531D6E577A00C724B0 /* KBMountDir.m in Sources */ = {isa = PBXBuildFile; fileRef = 009ABE511D6E577A00C724B0 /* KBMountDir.m */; };
352354
00B82BD21BE9424600C1FC72 /* KBTask.h in Headers */ = {isa = PBXBuildFile; fileRef = 00B82BD01BE9424600C1FC72 /* KBTask.h */; };
353355
00B82BD31BE9424600C1FC72 /* KBTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 00B82BD11BE9424600C1FC72 /* KBTask.m */; };
354356
00E206681C4DBD170095FD56 /* KBCommandLine.h in Headers */ = {isa = PBXBuildFile; fileRef = 00E206661C4DBD170095FD56 /* KBCommandLine.h */; };
@@ -714,6 +716,8 @@
714716
006D68121C7D281400B1CA0B /* KBSharedFileList.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KBSharedFileList.m; sourceTree = "<group>"; };
715717
008949961BFBB60C00CB605A /* KBCustomEnvView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KBCustomEnvView.m; sourceTree = "<group>"; };
716718
008949971BFBB60C00CB605A /* KBCustomEnvView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KBCustomEnvView.h; sourceTree = "<group>"; };
719+
009ABE501D6E577A00C724B0 /* KBMountDir.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KBMountDir.h; sourceTree = "<group>"; };
720+
009ABE511D6E577A00C724B0 /* KBMountDir.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KBMountDir.m; sourceTree = "<group>"; };
717721
00B82BD01BE9424600C1FC72 /* KBTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KBTask.h; sourceTree = "<group>"; };
718722
00B82BD11BE9424600C1FC72 /* KBTask.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KBTask.m; sourceTree = "<group>"; };
719723
00E206661C4DBD170095FD56 /* KBCommandLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KBCommandLine.h; sourceTree = "<group>"; };
@@ -832,6 +836,8 @@
832836
00010B391BE19F8800A9C285 /* KBComponent.m */,
833837
00010B3A1BE19F8800A9C285 /* KBComponentStatus.h */,
834838
00010B3B1BE19F8800A9C285 /* KBComponentStatus.m */,
839+
009ABE501D6E577A00C724B0 /* KBMountDir.h */,
840+
009ABE511D6E577A00C724B0 /* KBMountDir.m */,
835841
00010B3E1BE19F8800A9C285 /* KBFSService.h */,
836842
00010B3F1BE19F8800A9C285 /* KBFSService.m */,
837843
00010B401BE19F8800A9C285 /* KBFuseComponent.h */,
@@ -1470,6 +1476,7 @@
14701476
0033F4791C3F27D700317C79 /* KBLogFormatter.h in Headers */,
14711477
00010CFA1BE19F8900A9C285 /* KBEnvConfig.h in Headers */,
14721478
00010DA41BE19F8900A9C285 /* KBAlertView.h in Headers */,
1479+
009ABE521D6E577A00C724B0 /* KBMountDir.h in Headers */,
14731480
00010DC81BE19F8900A9C285 /* KBUserInfoLabels.h in Headers */,
14741481
00010D141BE19F8900A9C285 /* KBEnvSelectView.h in Headers */,
14751482
00010B101BE19F2100A9C285 /* KBKit.h in Headers */,
@@ -1783,6 +1790,7 @@
17831790
00010CA41BE19F8900A9C285 /* KBComponentStatus.m in Sources */,
17841791
00010D9B1BE19F8900A9C285 /* KBPGPVerifyAppView.m in Sources */,
17851792
00010D311BE19F8900A9C285 /* KBFilePreviewView.m in Sources */,
1793+
009ABE531D6E577A00C724B0 /* KBMountDir.m in Sources */,
17861794
00010CFD1BE19F8900A9C285 /* KBEnvironment.m in Sources */,
17871795
00010DD31BE19F8900A9C285 /* KBUsersAppView.m in Sources */,
17881796
00010D111BE19F8900A9C285 /* KBDebugPropertiesView.m in Sources */,

‎osx/KBKit/KBKit/Component/KBFSService.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
#import "KBComponent.h"
1212
#import "KBInstallable.h"
1313
#import "KBEnvConfig.h"
14-
#import "KBHelperTool.h"
1514

1615
@interface KBFSService : KBInstallable
1716

18-
- (instancetype)initWithConfig:(KBEnvConfig *)config helperTool:(KBHelperTool *)helperTool label:(NSString *)label servicePath:(NSString *)servicePath;
17+
- (instancetype)initWithConfig:(KBEnvConfig *)config label:(NSString *)label servicePath:(NSString *)servicePath;
1918

2019
@end

‎osx/KBKit/KBKit/Component/KBFSService.m‎

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111
#import "KBKeybaseLaunchd.h"
1212
#import "KBSemVersion.h"
1313
#import "KBTask.h"
14+
#import "KBMountDir.h"
1415

1516
@interface KBFSService ()
1617
@property NSString *label;
1718
@property NSString *servicePath;
1819
@property KBRServiceStatus *serviceStatus;
19-
@property KBHelperTool *helperTool;
2020
@property YOView *infoView;
2121
@end
2222

2323
@implementation KBFSService
2424

25-
- (instancetype)initWithConfig:(KBEnvConfig *)config helperTool:(KBHelperTool *)helperTool label:(NSString *)label servicePath:(NSString *)servicePath {
25+
- (instancetype)initWithConfig:(KBEnvConfig *)config label:(NSString *)label servicePath:(NSString *)servicePath {
2626
if ((self = [self initWithConfig:config name:@"KBFS" info:@"The filesystem service" image:[KBIcons imageForIcon:KBIconNetwork]])) {
27-
_helperTool = helperTool;
2827
_label = label;
2928
_servicePath = servicePath;
3029
}
@@ -63,50 +62,7 @@ - (KBInstallRuntimeStatus)runtimeStatus {
6362
return [NSString gh_isBlank:self.serviceStatus.pid] ? KBInstallRuntimeStatusStopped : KBInstallRuntimeStatusStarted;
6463
}
6564

66-
- (BOOL)checkMountDir {
67-
NSString *directory = self.config.mountDir;
68-
BOOL exists = [NSFileManager.defaultManager fileExistsAtPath:directory isDirectory:nil];
69-
if (exists) {
70-
NSError *error = nil;
71-
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:directory error:&error];
72-
if (attributes) {
73-
DDLogDebug(@"Mount directory=%@, attributes=%@", directory, attributes);
74-
} else {
75-
DDLogDebug(@"Mount directory error: %@", error);
76-
}
77-
} else {
78-
DDLogDebug(@"Mount directory doesn't exist: %@", directory);
79-
}
80-
return exists;
81-
}
82-
83-
- (void)createMountDir:(KBCompletion)completion {
84-
uid_t uid = getuid();
85-
gid_t gid = getgid();
86-
// Make the dir 0600 so we can't go into it while unmounted.
87-
NSNumber *permissions = [NSNumber numberWithShort:0600];
88-
NSDictionary *params = @{@"directory": self.config.mountDir, @"uid": @(uid), @"gid": @(gid), @"permissions": permissions, @"excludeFromBackup": @(YES)};
89-
DDLogDebug(@"Creating mount directory: %@", params);
90-
[self.helperTool.helper sendRequest:@"createDirectory" params:@[params] completion:^(NSError *error, id value) {
91-
completion(error);
92-
}];
93-
}
94-
9565
- (void)install:(KBCompletion)completion {
96-
if (![self checkMountDir]) {
97-
[self createMountDir:^(NSError *error) {
98-
if (error) {
99-
completion(error);
100-
return;
101-
}
102-
[self _install:completion];
103-
}];
104-
} else {
105-
[self _install:completion];
106-
}
107-
}
108-
109-
- (void)_install:(KBCompletion)completion {
11066
NSString *binPath = [self.config serviceBinPathWithPathOptions:0 servicePath:_servicePath];
11167
[KBTask executeForJSONWithCommand:binPath args:@[@"-d", @"--log-format=file", @"install", @"--format=json", @"--components=kbfs"] timeout:KBDefaultInstallableTimeout completion:^(NSError *error, id response) {
11268
if (!error) error = [KBInstallable checkForStatusErrorFromResponse:response];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// KBMountDir.h
3+
// KBKit
4+
//
5+
// Created by Gabriel on 8/24/16.
6+
// Copyright © 2016 Gabriel Handford. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
#import "KBComponent.h"
12+
#import "KBInstallable.h"
13+
#import "KBEnvConfig.h"
14+
#import "KBHelperTool.h"
15+
16+
@interface KBMountDir : KBInstallable
17+
18+
- (instancetype)initWithConfig:(KBEnvConfig *)config helperTool:(KBHelperTool *)helperTool;
19+
20+
@end
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// KBMountDir.m
3+
// KBKit
4+
//
5+
// Created by Gabriel on 8/24/16.
6+
// Copyright © 2016 Gabriel Handford. All rights reserved.
7+
//
8+
9+
#import "KBMountDir.h"
10+
11+
@interface KBMountDir ()
12+
@property KBHelperTool *helperTool;
13+
@property KBEnvConfig *config;
14+
@end
15+
16+
@implementation KBMountDir
17+
18+
@synthesize error;
19+
20+
- (instancetype)initWithConfig:(KBEnvConfig *)config helperTool:(KBHelperTool *)helperTool {
21+
if ((self = [self initWithConfig:config name:@"Mount Directory" info:@"Location for mount directory" image:nil])) {
22+
_helperTool = helperTool;
23+
}
24+
return self;
25+
}
26+
27+
- (NSString *)name {
28+
return @"Mount directory";
29+
}
30+
31+
- (BOOL)checkMountDirExists {
32+
NSString *directory = self.config.mountDir;
33+
BOOL exists = [NSFileManager.defaultManager fileExistsAtPath:directory isDirectory:nil];
34+
if (!exists) {
35+
DDLogDebug(@"Mount directory doesn't exist: %@", directory);
36+
return NO;
37+
}
38+
39+
NSError *err = nil;
40+
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:directory error:&err];
41+
if (!attributes) {
42+
DDLogDebug(@"Mount directory error: %@", error);
43+
return NO;
44+
}
45+
46+
// The mount dir might have 0700 permissions if left mounted but unattached to KBFS (owned by user).
47+
// And the dir might have 0600 permissions if unmounted (owned by user).
48+
// If mounted and attached to KBFS it will have 0755 permissions (owned by root).
49+
DDLogDebug(@"Mount directory=%@, attributes=%@", directory, attributes);
50+
return YES;
51+
}
52+
53+
- (void)removeMountDir:(NSString *)mountDir completion:(KBCompletion)completion {
54+
// Because the mount dir is in the root path, we need the helper tool to remove it, even if owned by the user
55+
NSDictionary *params = @{@"path": mountDir};
56+
DDLogDebug(@"Removing mount directory: %@", params);
57+
[self.helperTool.helper sendRequest:@"remove" params:@[params] completion:^(NSError *err, id value) {
58+
completion(err);
59+
}];
60+
}
61+
62+
- (void)createMountDir:(KBCompletion)completion {
63+
uid_t uid = getuid();
64+
gid_t gid = getgid();
65+
NSNumber *permissions = [NSNumber numberWithShort:0600];
66+
NSDictionary *params = @{@"directory": self.config.mountDir, @"uid": @(uid), @"gid": @(gid), @"permissions": permissions, @"excludeFromBackup": @(YES)};
67+
DDLogDebug(@"Creating mount directory: %@", params);
68+
[self.helperTool.helper sendRequest:@"createDirectory" params:@[params] completion:^(NSError *err, id value) {
69+
completion(err);
70+
}];
71+
}
72+
73+
- (void)install:(KBCompletion)completion {
74+
if (![self checkMountDirExists]) {
75+
[self createMountDir:^(NSError *err) {
76+
if (err) {
77+
completion(err);
78+
return;
79+
}
80+
// Run check again after create for debug info
81+
[self checkMountDirExists];
82+
completion(nil);
83+
}];
84+
} else {
85+
completion(nil);
86+
}
87+
}
88+
89+
- (void)uninstall:(KBCompletion)completion {
90+
NSString *mountDir = self.config.mountDir;
91+
if (![NSFileManager.defaultManager fileExistsAtPath:mountDir isDirectory:nil]) {
92+
DDLogInfo(@"The mount directory doesn't exist: %@", mountDir);
93+
completion(nil);
94+
return;
95+
}
96+
[self removeMountDir:mountDir completion:completion];
97+
}
98+
99+
@end

‎osx/KBKit/KBKit/KBKit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ FOUNDATION_EXPORT const unsigned char KBKitVersionString[];
5555
#import <KBKit/KBCommandLine.h>
5656
#import <KBKit/KBUninstaller.h>
5757
#import <KBKit/KBAppBundle.h>
58+
#import <KBKit/KBMountDir.h>
5859

5960
// IO
6061
#import <KBKit/KBFileReader.h>

‎osx/KBKit/KBKit/System/KBEnvironment.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ typedef NS_OPTIONS (NSUInteger, KBInstallOptions) {
2020
KBInstallOptionFuse = 1 << 3,
2121
KBInstallOptionKBFS = 1 << 4,
2222
KBInstallOptionUpdater = 1 << 5,
23+
KBInstallOptionMountDir = 1 << 6,
2324
KBInstallOptionCLI = 1 << 10,
2425

25-
KBInstallOptionAll = KBInstallOptionService | KBInstallOptionHelper | KBInstallOptionFuse | KBInstallOptionKBFS | KBInstallOptionUpdater | KBInstallOptionCLI,
26+
KBInstallOptionAll = KBInstallOptionService | KBInstallOptionHelper | KBInstallOptionFuse | KBInstallOptionMountDir | KBInstallOptionKBFS | KBInstallOptionUpdater | KBInstallOptionCLI,
2627
};
2728

2829
@interface KBEnvironment : NSObject
2930

31+
@property (readonly) KBHelperTool *helperTool;
3032
@property (readonly) KBEnvConfig *config;
3133
@property (readonly) KBService *service;
3234
@property (readonly) KBFSService *kbfs;

0 commit comments

Comments
 (0)