func (cmd *ls) Run(f *flag.FlagSet) error { ds, err := cmd.Datastore() if err != nil { return err } b, err := ds.Browser(context.TODO()) if err != nil { return err } args := f.Args() if len(args) == 0 { args = []string{""} } result := &listOutput{ rs: make([]types.HostDatastoreBrowserSearchResults, 0), long: cmd.long, } for _, arg := range args { spec := types.HostDatastoreBrowserSearchSpec{ MatchPattern: []string{"*"}, } if cmd.long { spec.Details = &types.FileQueryFlags{ FileType: true, FileSize: true, FileOwner: types.NewBool(true), // TODO: omitempty is generated, but seems to be required Modification: true, } } for i := 0; ; i++ { r, err := cmd.ListPath(b, arg, spec) if err != nil { // Treat the argument as a match pattern if not found as directory if i == 0 && types.IsFileNotFound(err) { spec.MatchPattern[0] = path.Base(arg) arg = path.Dir(arg) continue } return err } // Treat an empty result against match pattern as file not found if i == 1 && len(r.File) == 0 { return fmt.Errorf("File %s/%s was not found", r.FolderPath, spec.MatchPattern[0]) } result.add(r) break } } return cmd.WriteResult(result) }
func (d *Dispatcher) deleteDatastoreFiles(ds *object.Datastore, path string, force bool) (bool, error) { defer trace.End(trace.Begin(fmt.Sprintf("path %q, force %t", path, force))) // refuse to delete everything on the datstore, ignore force if path == "" { dsn, _ := ds.ObjectName(d.ctx) msg := fmt.Sprintf("refusing to remove datastore files for path \"\" on datastore %q", dsn) return false, errors.New(msg) } var empty bool dsPath := ds.Path(path) res, err := d.lsFolder(ds, dsPath) if err != nil { if !types.IsFileNotFound(err) { err = errors.Errorf("Failed to browse folder %q: %s", dsPath, err) return empty, err } log.Debugf("Folder %q is not found", dsPath) empty = true return empty, nil } if len(res.File) > 0 && !force { log.Debugf("Folder %q is not empty, leave it there", dsPath) return empty, nil } m := object.NewFileManager(ds.Client()) if err = d.deleteFilesIteratively(m, ds, dsPath); err != nil { return empty, err } return true, nil }
// Mkdir creates directories. func (d *Datastore) Mkdir(ctx context.Context, createParentDirectories bool, dirs ...string) (string, error) { upth := path.Join(dirs...) // check if it already exists _, err := d.Ls(ctx, upth) if err != nil && !types.IsFileNotFound(err) { return "", err } upth = path.Join(d.RootURL, upth) // dir already exists if err == nil { return upth, nil } log.Infof("Creating directory %s", upth) if err := d.fm.MakeDirectory(ctx, upth, d.s.Datacenter, createParentDirectories); err != nil { return "", err } return upth, nil }
func (v *ImageStore) deleteImage(op trace.Operation, storeName, ID string) error { // Delete in order of manifest (the done file), the vmdk (because VC honors // the deletable flag in the vmdk file), then the directory to get // everything else. paths := []string{ v.manifestPath(storeName, ID), v.imageDiskPath(storeName, ID), v.imageDirPath(storeName, ID), } for _, pth := range paths { err := v.ds.Rm(op, pth) // not exist is ok if err == nil || types.IsFileNotFound(err) { continue } // something isn't right. bale. op.Errorf("ImageStore: delete image error: %s", err.Error()) return err } return nil }
func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error { args := f.Args() if len(args) == 0 { return errors.New("missing operand") } c, err := cmd.Client() if err != nil { return err } var dc *object.Datacenter dc, err = cmd.Datacenter() if err != nil { return err } if cmd.isNamespace { path := args[0] nm := object.NewDatastoreNamespaceManager(c) err = nm.DeleteDirectory(ctx, dc, path) } else { var path string var task *object.Task // TODO(PN): Accept multiple args path, err = cmd.DatastorePath(args[0]) if err != nil { return err } m := object.NewFileManager(c) task, err = m.DeleteDatastoreFile(ctx, path, dc) if err != nil { return err } err = task.Wait(ctx) } if err != nil { if types.IsFileNotFound(err) && cmd.force { // Ignore error return nil } } return err }
func (d *Dispatcher) deleteFilesIteratively(m *object.FileManager, ds *object.Datastore, dsPath string) error { defer trace.End(trace.Begin(dsPath)) // Get sorted result to make sure children files listed ahead of folder. Then we can empty folder before delete it // This function specifically designed for vSan, as vSan sometimes will throw error to delete folder is the folder is not empty res, err := d.getSortedChildren(ds, dsPath) if err != nil { if !types.IsFileNotFound(err) { err = errors.Errorf("Failed to browse sub folders %q: %s", dsPath, err) return err } log.Debugf("Folder %q is not found", dsPath) return nil } for _, path := range res { if err = d.deleteVMFSFiles(m, ds, path); err != nil { return err } } return d.deleteVMFSFiles(m, ds, dsPath) }
func (cmd *rm) Run(f *flag.FlagSet) error { args := f.Args() if len(args) == 0 { return errors.New("missing operand") } c, err := cmd.Client() if err != nil { return err } dc, err := cmd.Datacenter() if err != nil { return err } // TODO(PN): Accept multiple args path, err := cmd.DatastorePath(args[0]) if err != nil { return err } m := object.NewFileManager(c) task, err := m.DeleteDatastoreFile(context.TODO(), path, dc) if err != nil { return err } err = task.Wait(context.TODO()) if err != nil { if types.IsFileNotFound(err) && cmd.force { // Ignore error return nil } } return err }
func (d *Dispatcher) deleteImages(conf *config.VirtualContainerHostConfigSpec) error { defer trace.End(trace.Begin("")) var errs []string log.Infoln("Removing image stores") for _, imageDir := range conf.ImageStores { imageDSes, err := d.session.Finder.DatastoreList(d.ctx, imageDir.Host) if err != nil { errs = append(errs, err.Error()) continue } if len(imageDSes) != 1 { errs = append(errs, fmt.Sprintf("Found %d datastores with provided datastore path %s. Provided datastore path must identify exactly one datastore.", len(imageDSes), imageDir.String())) continue } // delete images subfolder imagePath := path.Join(imageDir.Path, vsphere.StorageParentDir) if _, err = d.deleteDatastoreFiles(imageDSes[0], imagePath, true); err != nil { errs = append(errs, err.Error()) } // delete kvStores subfolder kvPath := path.Join(imageDir.Path, store.KVStoreFolder) if _, err = d.deleteDatastoreFiles(imageDSes[0], kvPath, true); err != nil { errs = append(errs, err.Error()) } dsPath, err := datastore.URLtoDatastore(&imageDir) if err != nil { errs = append(errs, err.Error()) continue } children, err := d.getChildren(imageDSes[0], dsPath) if err != nil { if !types.IsFileNotFound(err) { errs = append(errs, err.Error()) } continue } if len(children) == 0 { log.Debugf("Removing empty image store parent directory [%s] %s", imageDir.Host, imageDir.Path) if _, err = d.deleteDatastoreFiles(imageDSes[0], imageDir.Path, true); err != nil { errs = append(errs, err.Error()) } } else { log.Debugf("Image store parent directory not empty, leaving in place.") } } if len(errs) > 0 { return errors.New(strings.Join(errs, "\n")) } return nil }
func (d *Driver) Remove() error { machineState, err := d.GetState() if err != nil { return err } if machineState == state.Running { if err = d.Kill(); err != nil { return fmt.Errorf("can't stop VM: %s", err) } } ctx, cancel := context.WithCancel(context.Background()) defer cancel() c, err := d.vsphereLogin(ctx) if err != nil { return err } defer c.Logout(ctx) // Create a new finder f := find.NewFinder(c.Client, true) dc, err := f.DatacenterOrDefault(ctx, d.Datacenter) if err != nil { return err } f.SetDatacenter(dc) dss, err := f.DatastoreOrDefault(ctx, d.Datastore) if err != nil { return err } // Remove B2D Iso from VM folder m := object.NewFileManager(c.Client) task, err := m.DeleteDatastoreFile(ctx, dss.Path(fmt.Sprintf("%s/%s", d.MachineName, isoFilename)), dc) if err != nil { return err } err = task.Wait(ctx) if err != nil { if types.IsFileNotFound(err) { // Ignore error return nil } } vm, err := d.fetchVM(c, ctx, d.MachineName) if err != nil { return err } task, err = vm.Destroy(ctx) if err != nil { return err } _, err = task.WaitForResult(ctx, nil) if err != nil { return err } return nil }