Skip to content

Commit 3571adc

Browse files
committed
RmdirContents server implementation
1 parent 8b2fd10 commit 3571adc

File tree

7 files changed

+112
-3
lines changed

7 files changed

+112
-3
lines changed

pkg/os/filesystem/api.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"path/filepath"
78
"strings"
89
)
910

@@ -18,6 +19,7 @@ type API interface {
1819
PathValid(path string) (bool, error)
1920
Mkdir(path string) error
2021
Rmdir(path string, force bool) error
22+
RmdirContents(path string, force bool) error
2123
CreateSymlink(oldname string, newname string) error
2224
IsSymlink(path string) (bool, error)
2325
}
@@ -78,6 +80,28 @@ func (filesystemAPI) Rmdir(path string, force bool) error {
7880
return os.Remove(path)
7981
}
8082

83+
// Rmdir removes a dir with `os.Remove`, if force is true then `os.RemoveAll` is used instead.
84+
func (filesystemAPI) RmdirContents(path string, force bool) error {
85+
dir, err := os.Open(path)
86+
if err != nil {
87+
return err
88+
}
89+
defer dir.Close()
90+
91+
files, err := dir.Readdirnames(-1)
92+
if err != nil {
93+
return err
94+
}
95+
for _, file := range files {
96+
err = os.RemoveAll(filepath.Join(path, file))
97+
if err != nil {
98+
return err
99+
}
100+
}
101+
102+
return nil
103+
}
104+
81105
// CreateSymlink creates newname as a symbolic link to oldname.
82106
func (filesystemAPI) CreateSymlink(oldname, newname string) error {
83107
return os.Symlink(oldname, newname)

pkg/server/filesystem/impl/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ type RmdirRequest struct {
6161
type RmdirResponse struct {
6262
}
6363

64+
type RmdirContentsRequest struct {
65+
// The path to remove in the host's filesystem.
66+
// All special characters allowed by Windows in path names will be allowed
67+
// except for restrictions noted below. For details, please check:
68+
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
69+
//
70+
// Restrictions:
71+
// Only absolute path (indicated by a drive letter prefix: e.g. "C:\") is accepted.
72+
// Depending on the context parameter of this function, the path prefix needs
73+
// to match the paths specified either as kubelet-csi-plugins-path
74+
// or as kubelet-pod-path parameters of csi-proxy.
75+
// UNC paths of the form "\\server\share\path\file" are not allowed.
76+
// All directory separators need to be backslash character: "\".
77+
// Characters: .. / : | ? * in the path are not allowed.
78+
// Path cannot be a file of type symlink.
79+
// Maximum path length will be capped to 260 characters.
80+
Path string
81+
// Force remove all contents under path (if any).
82+
Force bool
83+
}
84+
85+
type RmdirContentsResponse struct {
86+
}
87+
6488
type CreateSymlinkRequest struct {
6589
// The path of the existing directory to be linked.
6690
// All special characters allowed by Windows in path names will be allowed

pkg/server/filesystem/impl/v2alpha1/conversion_generated.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/server/filesystem/server.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ func (s *Server) Rmdir(ctx context.Context, request *internal.RmdirRequest, vers
154154
}
155155
return nil, err
156156
}
157+
158+
func (s *Server) RmdirContents(ctx context.Context, request *internal.RmdirContentsRequest, version apiversion.Version) (*internal.RmdirContentsResponse, error) {
159+
klog.V(2).Infof("Request: RmdirContents with path=%q", request.Path)
160+
err := s.validatePathWindows(request.Path)
161+
if err != nil {
162+
klog.Errorf("failed validatePathWindows %v", err)
163+
return nil, err
164+
}
165+
err = s.hostAPI.RmdirContents(request.Path, request.Force)
166+
if err != nil {
167+
klog.Errorf("failed RmdirContents %v", err)
168+
return nil, err
169+
}
170+
return nil, err
171+
}
172+
157173
func (s *Server) LinkPath(ctx context.Context, request *internal.LinkPathRequest, version apiversion.Version) (*internal.LinkPathResponse, error) {
158174
klog.V(2).Infof("Request: LinkPath with targetPath=%q sourcePath=%q", request.TargetPath, request.SourcePath)
159175
createSymlinkRequest := &internal.CreateSymlinkRequest{

pkg/server/filesystem/server_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func (fakeFileSystemAPI) Mkdir(path string) error {
2525
func (fakeFileSystemAPI) Rmdir(path string, force bool) error {
2626
return nil
2727
}
28+
func (fakeFileSystemAPI) RmdirContents(path string, force bool) error {
29+
return nil
30+
}
2831
func (fakeFileSystemAPI) CreateSymlink(tgt string, src string) error {
2932
return nil
3033
}

pkg/server/smb/server_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func (fakeFileSystemAPI) Mkdir(path string) error {
4747
func (fakeFileSystemAPI) Rmdir(path string, force bool) error {
4848
return nil
4949
}
50+
func (fakeFileSystemAPI) RmdirContents(path string, force bool) error {
51+
return nil
52+
}
5053
func (fakeFileSystemAPI) CreateSymlink(tgt string, src string) error {
5154
return nil
5255
}

scripts/bump-version.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
set -o nounset
1111
set -ex
1212

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

43-
# generate client_generated.go
44-
make generate-csi-proxy-api-gen
4542
# generate api.pb.go
4643
# it's going to fail but it's expected :(
4744
make generate-protobuf || true
45+
# generate client_generated.go
46+
make generate-csi-proxy-api-gen
4847

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

0 commit comments

Comments
 (0)