Skip to content

Commit fa881f3

Browse files
committed
feat(serverHandler/mkdir): provide mkdir with parent directories
1 parent b479889 commit fa881f3

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

doc/en-US/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ curl -X POST -d 'name=subdir1&name=subdir2/subdir21&name=file1&name=subdir3/file
9898
# Create directories in specific path
9999
Only work when "mkdir" is enabled.
100100
```
101-
GET <path>?mkdir[&json]&name=<dir1>&name=<dir2>&...name=<dirN>
101+
GET <path>?mkdir[&json]&name=<dir1path>&name=<dir2path>&...name=<dirNpath>
102102
```
103103
```
104104
POST <path>?mkdir[&json]
105105
106-
name=<dir1>&name=<dir2>&...name=<dirN>
106+
name=<dir1path>&name=<dir2path>&...name=<dirNpath>
107107
```
108108

109109
Example:
110110
```sh
111-
curl -X POST -d 'name=dir1&name=dir2&name=dir3' http://localhost/tmp/?mkdir
111+
curl -X POST -d 'name=dir1&name=dir2&name=foo/bar/baz' http://localhost/tmp/?mkdir
112112
```
113113

114114
# Upload files to specific path

doc/zh-CN/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ curl -X POST -d 'name=subdir1&name=subdir2/subdir21&name=file1&name=subdir3/file
9696
# 在指定路径下创建目录
9797
仅在“mkdir”选项启用时有效。
9898
```
99-
GET <path>?mkdir[&json]&name=<dir1>&name=<dir2>&...name=<dirN>
99+
GET <path>?mkdir[&json]&name=<dir1path>&name=<dir2path>&...name=<dirNpath>
100100
```
101101
```
102102
POST <path>?mkdir[&json]
103103
104-
name=<dir1>&name=<dir2>&...name=<dirN>
104+
name=<dir1path>&name=<dir2path>&...name=<dirNpath>
105105
```
106106

107107
举例:
108108
```sh
109-
curl -X POST -d 'name=dir1&name=dir2&name=dir3' http://localhost/tmp/?mkdir
109+
curl -X POST -d 'name=dir1&name=dir2&name=foo/bar/baz' http://localhost/tmp/?mkdir
110110
```
111111

112112
# 上传文件到指定路径

src/serverHandler/mkdir.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package serverHandler
33
import (
44
"errors"
55
"os"
6+
"strings"
67
)
78

89
func (h *handler) mkdirs(fsPrefix string, files []string, aliasSubItems []os.FileInfo) bool {
@@ -15,14 +16,20 @@ func (h *handler) mkdirs(fsPrefix string, files []string, aliasSubItems []os.Fil
1516

1617
var filename string
1718
var ok bool
18-
if filename, ok = getCleanFilePath(inputFilename); !ok {
19-
errs = append(errs, errors.New("mkdir: illegal directory name "+inputFilename))
19+
if filename, ok = getCleanDirFilePath(inputFilename); !ok {
20+
errs = append(errs, errors.New("mkdir: illegal directory path "+inputFilename))
2021
continue
2122
}
22-
if containsItem(aliasSubItems, filename) {
23+
24+
filenamePart1 := filename
25+
if prefixEndIndex := strings.IndexByte(filenamePart1, '/'); prefixEndIndex > 0 {
26+
filenamePart1 = filenamePart1[0:prefixEndIndex]
27+
}
28+
if containsItem(aliasSubItems, filenamePart1) {
29+
errs = append(errs, errors.New("mkdir: ignore path shadowed by alias "+filename))
2330
continue
2431
}
25-
err := os.Mkdir(fsPrefix+"/"+filename, 0755)
32+
err := os.MkdirAll(fsPrefix+"/"+filename, 0755)
2633
if err != nil {
2734
errs = append(errs, err)
2835
}

test/case/018.mkdirp.bash

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
cleanup() {
4+
rm -rf "$fs"/uploaded/[12]/*.tmp
5+
}
6+
7+
source "$root"/lib.bash
8+
9+
"$ghfs" -l 3003 -r "$fs"/uploaded/ --mkdir /1 &> /dev/null &
10+
sleep 0.05 # wait server ready
11+
cleanup
12+
13+
file1="$fs"/uploaded/1/foo/bar/baz.tmp
14+
curl_head_status 'http://127.0.0.1:3003/1/?mkdir&name=foo/bar/baz.tmp' > /dev/null
15+
[ -d "$file1" ] || fail "$file1 should exists as directory"
16+
17+
cleanup
18+
jobs -p | xargs kill

test/case/019.alias.mkdirp.bash

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
cleanup() {
4+
rm -rf "$fs"/uploaded/[12]/*.tmp
5+
}
6+
7+
source "$root"/lib.bash
8+
9+
"$ghfs" -l 3003 -r "$fs"/uploaded/ --mkdir /1 -a :/1/my1.tmp:"$fs"/uploaded/2 -a :/1/my2.tmp/backup:"$fs"/uploaded/2 &> /dev/null &
10+
sleep 0.05 # wait server ready
11+
cleanup
12+
13+
file1="$fs"/uploaded/1/my1.tmp
14+
curl_head_status 'http://127.0.0.1:3003/1/?mkdir&name=my1.tmp' > /dev/null
15+
[ -e "$file1" ] && fail "$file1 should not exists"
16+
17+
file2="$fs"/uploaded/2/my2.tmp
18+
curl_head_status 'http://127.0.0.1:3003/1/?mkdir&name=my2.tmp/test' > /dev/null
19+
[ -e "$file2" ] && fail "$file2 should not exists"
20+
21+
cleanup
22+
jobs -p | xargs kill

0 commit comments

Comments
 (0)