Skip to content

Commit

Permalink
RmdirContents server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Jan 11, 2022
1 parent 8b2fd10 commit fd5fdae
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 3 deletions.
29 changes: 29 additions & 0 deletions pkg/os/filesystem/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

Expand All @@ -18,6 +19,7 @@ type API interface {
PathValid(path string) (bool, error)
Mkdir(path string) error
Rmdir(path string, force bool) error
RmdirContents(path string, force bool) error
CreateSymlink(oldname string, newname string) error
IsSymlink(path string) (bool, error)
}
Expand Down Expand Up @@ -78,6 +80,33 @@ func (filesystemAPI) Rmdir(path string, force bool) error {
return os.Remove(path)
}

// RmdirContents removes the contents of a directory with `os.Remove`, if force is true then `os.RemoveAll` is used instead.
func (filesystemAPI) RmdirContents(path string, force bool) error {
dir, err := os.Open(path)
if err != nil {
return err
}
defer dir.Close()

files, err := dir.Readdirnames(-1)
if err != nil {
return err
}
for _, file := range files {
candidatePath := filepath.Join(path, file)
if force {
err = os.RemoveAll(candidatePath)
} else {
err = os.Remove(candidatePath)
}
if err != nil {
return err
}
}

return nil
}

// CreateSymlink creates newname as a symbolic link to oldname.
func (filesystemAPI) CreateSymlink(oldname, newname string) error {
return os.Symlink(oldname, newname)
Expand Down
24 changes: 24 additions & 0 deletions pkg/server/filesystem/impl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ type RmdirRequest struct {
type RmdirResponse struct {
}

type RmdirContentsRequest struct {
// The path to remove in the host's filesystem.
// All special characters allowed by Windows in path names will be allowed
// except for restrictions noted below. For details, please check:
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
//
// Restrictions:
// Only absolute path (indicated by a drive letter prefix: e.g. "C:\") is accepted.
// Depending on the context parameter of this function, the path prefix needs
// to match the paths specified either as kubelet-csi-plugins-path
// or as kubelet-pod-path parameters of csi-proxy.
// UNC paths of the form "\\server\share\path\file" are not allowed.
// All directory separators need to be backslash character: "\".
// Characters: .. / : | ? * in the path are not allowed.
// Path cannot be a file of type symlink.
// Maximum path length will be capped to 260 characters.
Path string
// Force remove all contents under path (if any).
Force bool
}

type RmdirContentsResponse struct {
}

type CreateSymlinkRequest struct {
// The path of the existing directory to be linked.
// All special characters allowed by Windows in path names will be allowed
Expand Down
40 changes: 40 additions & 0 deletions pkg/server/filesystem/impl/v2alpha1/conversion_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pkg/server/filesystem/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ func (s *Server) Rmdir(ctx context.Context, request *internal.RmdirRequest, vers
}
return nil, err
}

func (s *Server) RmdirContents(ctx context.Context, request *internal.RmdirContentsRequest, version apiversion.Version) (*internal.RmdirContentsResponse, error) {
klog.V(2).Infof("Request: RmdirContents with path=%q", request.Path)
err := s.validatePathWindows(request.Path)
if err != nil {
klog.Errorf("failed validatePathWindows %v", err)
return nil, err
}
err = s.hostAPI.RmdirContents(request.Path, request.Force)
if err != nil {
klog.Errorf("failed RmdirContents %v", err)
return nil, err
}
return nil, err
}

func (s *Server) LinkPath(ctx context.Context, request *internal.LinkPathRequest, version apiversion.Version) (*internal.LinkPathResponse, error) {
klog.V(2).Infof("Request: LinkPath with targetPath=%q sourcePath=%q", request.TargetPath, request.SourcePath)
createSymlinkRequest := &internal.CreateSymlinkRequest{
Expand Down
3 changes: 3 additions & 0 deletions pkg/server/filesystem/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (fakeFileSystemAPI) Mkdir(path string) error {
func (fakeFileSystemAPI) Rmdir(path string, force bool) error {
return nil
}
func (fakeFileSystemAPI) RmdirContents(path string, force bool) error {
return nil
}
func (fakeFileSystemAPI) CreateSymlink(tgt string, src string) error {
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/server/smb/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (fakeFileSystemAPI) Mkdir(path string) error {
func (fakeFileSystemAPI) Rmdir(path string, force bool) error {
return nil
}
func (fakeFileSystemAPI) RmdirContents(path string, force bool) error {
return nil
}
func (fakeFileSystemAPI) CreateSymlink(tgt string, src string) error {
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions scripts/bump-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
set -o nounset
set -ex

# The bucket url of this script in Google Cloud, set in sync_scripts
: "${API_GROUP?API_GROUP is not set}"
: "${OLD_API_VERSION:?OLD_API_VERSION is not set, it needs the format vX}"
: "${NEW_API_VERSION:?NEW_API_VERSION is not set, it needs the format vX}"
Expand Down Expand Up @@ -40,11 +39,11 @@ function generate_client_files {
rm client/api/$target/api.pb.go || true
rm client/groups/$target/client_generated.go || true

# generate client_generated.go
make generate-csi-proxy-api-gen
# generate api.pb.go
# it's going to fail but it's expected :(
make generate-protobuf || true
# generate client_generated.go
make generate-csi-proxy-api-gen

# restore files from other API groups (side effect of generate-protobuf)
other_leaf_client_files=$(find client/api/ -links 2 -type d -exec echo {} \; | grep -v "$target\$")
Expand Down

0 comments on commit fd5fdae

Please sign in to comment.