@@ -177,22 +177,11 @@ func (dh driveHandler) MountDrive(ctx context.Context, req *drivemount.MountDriv
177
177
logger = logger .WithField ("drive_path" , drive .Path ())
178
178
179
179
// Do a basic check that we won't be mounting over any important system directories
180
- resolvedDest , err := evalAnySymlinks (req .DestinationPath )
181
- if err != nil {
182
- return nil , errors .Wrapf (err ,
183
- "failed to evaluate any symlinks in drive mount destination %q" , req .DestinationPath )
184
- }
185
-
186
- for _ , systemDir := range bannedSystemDirs {
187
- if isOrUnderDir (resolvedDest , systemDir ) {
188
- return nil , errors .Errorf (
189
- "drive mount destination %q resolves to path %q under banned system directory %q" ,
190
- req .DestinationPath , resolvedDest , systemDir ,
191
- )
192
- }
180
+ if err := isSystemDir (req .DestinationPath ); err != nil {
181
+ return nil , err
193
182
}
194
183
195
- err = os .MkdirAll (req .DestinationPath , 0700 )
184
+ err : = os .MkdirAll (req .DestinationPath , 0700 )
196
185
if err != nil {
197
186
return nil , errors .Wrapf (err , "failed to create drive mount destination %q" , req .DestinationPath )
198
187
}
@@ -229,6 +218,40 @@ func (dh driveHandler) MountDrive(ctx context.Context, req *drivemount.MountDriv
229
218
drive .Path (), req .DestinationPath )
230
219
}
231
220
221
+ func (dh driveHandler ) UnmountDrive (ctx context.Context , req * drivemount.UnmountDriveRequest ) (* empty.Empty , error ) {
222
+ drive , ok := dh .GetDrive (req .DriveID )
223
+ if ! ok {
224
+ return nil , fmt .Errorf ("drive %q could not be found" , req .DriveID )
225
+ }
226
+
227
+ err := mount .Unmount (drive .Path (), 0 )
228
+ if err == nil {
229
+ return & empty.Empty {}, nil
230
+ }
231
+
232
+ return nil , errors .Errorf ("failed to unmount the drive %q" ,
233
+ drive .Path ())
234
+ }
235
+
236
+ func isSystemDir (path string ) error {
237
+ resolvedDest , err := evalAnySymlinks (path )
238
+ if err != nil {
239
+ return errors .Wrapf (err ,
240
+ "failed to evaluate any symlinks in drive ummount destination %q" , path )
241
+ }
242
+
243
+ for _ , systemDir := range bannedSystemDirs {
244
+ if isOrUnderDir (resolvedDest , systemDir ) {
245
+ return errors .Errorf (
246
+ "drive mount destination %q resolves to path %q under banned system directory %q" ,
247
+ path , resolvedDest , systemDir ,
248
+ )
249
+ }
250
+ }
251
+
252
+ return nil
253
+ }
254
+
232
255
// evalAnySymlinks is similar to filepath.EvalSymlinks, except it will not return an error if part of the
233
256
// provided path does not exist. It will evaluate symlinks present in the path up to a component that doesn't
234
257
// exist, at which point it will just append the rest of the provided path to what has been resolved so far.
0 commit comments