You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

640 lines
20 KiB

7 years ago
7 years ago
7 years ago
8 months ago
8 months ago
3 years ago
3 years ago
3 years ago
3 years ago
8 months ago
3 years ago
8 months ago
3 years ago
3 years ago
3 years ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
9 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
1 year ago
8 months ago
8 months ago
8 months ago
8 months ago
3 years ago
4 years ago
4 years ago
3 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "github.com/aws/aws-sdk-go/service/s3"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  10. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  11. "io"
  12. "net/http"
  13. "net/url"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. )
  18. type OptionalString struct {
  19. string
  20. set bool
  21. }
  22. func (o OptionalString) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  23. if !o.set {
  24. return nil
  25. }
  26. return e.EncodeElement(o.string, startElement)
  27. }
  28. type ListBucketResultV2 struct {
  29. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListBucketResult"`
  30. Name string `xml:"Name"`
  31. Prefix string `xml:"Prefix"`
  32. MaxKeys uint16 `xml:"MaxKeys"`
  33. Delimiter string `xml:"Delimiter,omitempty"`
  34. IsTruncated bool `xml:"IsTruncated"`
  35. Contents []ListEntry `xml:"Contents,omitempty"`
  36. CommonPrefixes []PrefixEntry `xml:"CommonPrefixes,omitempty"`
  37. ContinuationToken OptionalString `xml:"ContinuationToken,omitempty"`
  38. NextContinuationToken string `xml:"NextContinuationToken,omitempty"`
  39. EncodingType string `xml:"EncodingType,omitempty"`
  40. KeyCount int `xml:"KeyCount"`
  41. StartAfter string `xml:"StartAfter,omitempty"`
  42. }
  43. func (s3a *S3ApiServer) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) {
  44. // https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
  45. // collect parameters
  46. bucket, _ := s3_constants.GetBucketAndObject(r)
  47. glog.V(0).Infof("ListObjectsV2Handler %s query %+v", bucket, r.URL.Query())
  48. originalPrefix, startAfter, delimiter, continuationToken, encodingTypeUrl, fetchOwner, maxKeys := getListObjectsV2Args(r.URL.Query())
  49. if maxKeys < 0 {
  50. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxKeys)
  51. return
  52. }
  53. marker := continuationToken.string
  54. if !continuationToken.set {
  55. marker = startAfter
  56. }
  57. response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker, delimiter, encodingTypeUrl, fetchOwner)
  58. if err != nil {
  59. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  60. return
  61. }
  62. if len(response.Contents) == 0 {
  63. if exists, existErr := s3a.exists(s3a.option.BucketsPath, bucket, true); existErr == nil && !exists {
  64. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  65. return
  66. }
  67. }
  68. responseV2 := &ListBucketResultV2{
  69. XMLName: response.XMLName,
  70. Name: response.Name,
  71. CommonPrefixes: response.CommonPrefixes,
  72. Contents: response.Contents,
  73. ContinuationToken: continuationToken,
  74. Delimiter: response.Delimiter,
  75. IsTruncated: response.IsTruncated,
  76. KeyCount: len(response.Contents) + len(response.CommonPrefixes),
  77. MaxKeys: response.MaxKeys,
  78. NextContinuationToken: response.NextMarker,
  79. Prefix: response.Prefix,
  80. StartAfter: startAfter,
  81. }
  82. if encodingTypeUrl {
  83. responseV2.EncodingType = s3.EncodingTypeUrl
  84. }
  85. writeSuccessResponseXML(w, r, responseV2)
  86. }
  87. func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
  88. // https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html
  89. // collect parameters
  90. bucket, _ := s3_constants.GetBucketAndObject(r)
  91. glog.V(0).Infof("ListObjectsV1Handler %s query %+v", bucket, r.URL.Query())
  92. originalPrefix, marker, delimiter, encodingTypeUrl, maxKeys := getListObjectsV1Args(r.URL.Query())
  93. if maxKeys < 0 {
  94. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxKeys)
  95. return
  96. }
  97. response, err := s3a.listFilerEntries(bucket, originalPrefix, uint16(maxKeys), marker, delimiter, encodingTypeUrl, true)
  98. if err != nil {
  99. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  100. return
  101. }
  102. if len(response.Contents) == 0 {
  103. if exists, existErr := s3a.exists(s3a.option.BucketsPath, bucket, true); existErr == nil && !exists {
  104. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  105. return
  106. }
  107. }
  108. writeSuccessResponseXML(w, r, response)
  109. }
  110. func (s3a *S3ApiServer) listFilerEntries(bucket string, originalPrefix string, maxKeys uint16, originalMarker string, delimiter string, encodingTypeUrl bool, fetchOwner bool) (response ListBucketResult, err error) {
  111. // convert full path prefix into directory name and prefix for entry name
  112. requestDir, prefix, marker := normalizePrefixMarker(originalPrefix, originalMarker)
  113. bucketPrefix := fmt.Sprintf("%s/%s/", s3a.option.BucketsPath, bucket)
  114. reqDir := bucketPrefix[:len(bucketPrefix)-1]
  115. if requestDir != "" {
  116. reqDir = fmt.Sprintf("%s%s", bucketPrefix, requestDir)
  117. }
  118. var contents []ListEntry
  119. var commonPrefixes []PrefixEntry
  120. var doErr error
  121. var nextMarker string
  122. cursor := &ListingCursor{
  123. maxKeys: maxKeys,
  124. prefixEndsOnDelimiter: strings.HasSuffix(originalPrefix, "/") && len(originalMarker) == 0,
  125. }
  126. if s3a.option.AllowListRecursive && (delimiter == "" || delimiter == "/") {
  127. reqDir = bucketPrefix
  128. if cursor.prefixEndsOnDelimiter && delimiter == "/" {
  129. originalPrefix = originalPrefix[0 : len(originalPrefix)-1]
  130. }
  131. if idx := strings.LastIndex(originalPrefix, "/"); idx > 0 {
  132. reqDir += originalPrefix[:idx]
  133. prefix = originalPrefix[idx+1:]
  134. }
  135. // This is necessary for SQL request with WHERE `directory` || `name` > originalMarker
  136. if len(originalMarker) > 0 && originalMarker[0:1] != "/" {
  137. marker = getStartFileFromKey(originalMarker)
  138. } else {
  139. marker = originalMarker
  140. }
  141. response = ListBucketResult{
  142. Name: bucket,
  143. Prefix: originalPrefix,
  144. Marker: originalMarker,
  145. MaxKeys: maxKeys,
  146. Delimiter: delimiter,
  147. }
  148. if encodingTypeUrl {
  149. response.EncodingType = s3.EncodingTypeUrl
  150. }
  151. if maxKeys == 0 {
  152. return
  153. }
  154. glog.V(0).Infof("listFilerEntries reqDir: %s, prefix: %s[%s], delimiter: %v, cursor: %+v, mmarker: %s[%s]", reqDir, prefix, originalPrefix, delimiter, cursor, marker, originalMarker)
  155. err = s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  156. doErr = s3a.doListFilerRecursiveEntries(client, reqDir, prefix, cursor, marker, delimiter, false,
  157. func(path string, entry *filer_pb.Entry) {
  158. key := path[len(bucketPrefix):]
  159. glog.V(0).Infof("doListFilerRecursiveEntries path %s, shortDir %s, key: %+v, cursor: %+v, marker: %s[%s], nextMarker: %s, IsDirectoryKeyObject %v", path, path[len(reqDir):], key, cursor, marker, originalMarker, cursor.nextMarker, entry.IsDirectoryKeyObject())
  160. if cursor.isTruncated {
  161. nextMarker = cursor.nextMarker
  162. return
  163. }
  164. defer func() {
  165. if cursor.maxKeys == 0 {
  166. cursor.isTruncated = true
  167. cursor.nextMarker = getStartFileFromKey(key)
  168. }
  169. }()
  170. if cursor.prefixEndsOnDelimiter && originalPrefix == key && entry.IsDirectoryKeyObject() {
  171. contents = append(contents, newListEntry(entry, key+"/", "", "", bucketPrefix, fetchOwner, true))
  172. cursor.maxKeys--
  173. cursor.prefixEndsOnDelimiter = false
  174. return
  175. }
  176. if delimiter == "/" && entry.IsDirectory {
  177. glog.V(0).Infof("append commonPrefixes %s", path[len(bucketPrefix):]+"/")
  178. commonPrefixes = append(commonPrefixes, PrefixEntry{
  179. Prefix: key + "/",
  180. })
  181. cursor.maxKeys--
  182. return
  183. }
  184. contents = append(contents, newListEntry(entry, key, "", "", bucketPrefix, fetchOwner, entry.IsDirectoryKeyObject()))
  185. cursor.maxKeys--
  186. },
  187. )
  188. return nil
  189. })
  190. response.NextMarker = nextMarker
  191. response.IsTruncated = len(nextMarker) != 0
  192. response.Contents = contents
  193. response.CommonPrefixes = commonPrefixes
  194. return
  195. }
  196. // check filer
  197. err = s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  198. for {
  199. empty := true
  200. nextMarker, doErr = s3a.doListFilerEntries(client, reqDir, prefix, cursor, marker, delimiter, false, func(dir string, entry *filer_pb.Entry) {
  201. empty = false
  202. glog.V(0).Infof("doListFilerEntries dir: %s entry: %+v", dir, entry)
  203. dirName, entryName, prefixName := entryUrlEncode(dir, entry.Name, encodingTypeUrl)
  204. if entry.IsDirectory {
  205. if entry.IsDirectoryKeyObject() {
  206. contents = append(contents, newListEntry(entry, "", dirName, entryName, bucketPrefix, fetchOwner, true))
  207. cursor.maxKeys--
  208. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
  209. } else if delimiter == "/" { // A response can contain CommonPrefixes only if you specify a delimiter.
  210. commonPrefixes = append(commonPrefixes, PrefixEntry{
  211. Prefix: fmt.Sprintf("%s/%s/", dirName, prefixName)[len(bucketPrefix):],
  212. })
  213. //All of the keys (up to 1,000) rolled up into a common prefix count as a single return when calculating the number of returns.
  214. cursor.maxKeys--
  215. }
  216. } else {
  217. var delimiterFound bool
  218. if delimiter != "" {
  219. // keys that contain the same string between the prefix and the first occurrence of the delimiter are grouped together as a commonPrefix.
  220. // extract the string between the prefix and the delimiter and add it to the commonPrefixes if it's unique.
  221. undelimitedPath := fmt.Sprintf("%s/%s", dir, entry.Name)[len(bucketPrefix):]
  222. // take into account a prefix if supplied while delimiting.
  223. undelimitedPath = strings.TrimPrefix(undelimitedPath, originalPrefix)
  224. delimitedPath := strings.SplitN(undelimitedPath, delimiter, 2)
  225. if len(delimitedPath) == 2 {
  226. // S3 clients expect the delimited prefix to contain the delimiter and prefix.
  227. delimitedPrefix := originalPrefix + delimitedPath[0] + delimiter
  228. for i := range commonPrefixes {
  229. if commonPrefixes[i].Prefix == delimitedPrefix {
  230. delimiterFound = true
  231. break
  232. }
  233. }
  234. if !delimiterFound {
  235. commonPrefixes = append(commonPrefixes, PrefixEntry{
  236. Prefix: delimitedPrefix,
  237. })
  238. cursor.maxKeys--
  239. delimiterFound = true
  240. }
  241. }
  242. }
  243. if !delimiterFound {
  244. contents = append(contents, newListEntry(entry, "", dirName, entryName, bucketPrefix, fetchOwner, false))
  245. cursor.maxKeys--
  246. }
  247. }
  248. })
  249. if doErr != nil {
  250. return doErr
  251. }
  252. if cursor.isTruncated {
  253. if requestDir != "" {
  254. nextMarker = requestDir + "/" + nextMarker
  255. }
  256. break
  257. } else if empty || strings.HasSuffix(originalPrefix, "/") {
  258. nextMarker = ""
  259. break
  260. } else {
  261. // start next loop
  262. marker = nextMarker
  263. }
  264. }
  265. response = ListBucketResult{
  266. Name: bucket,
  267. Prefix: originalPrefix,
  268. Marker: originalMarker,
  269. NextMarker: nextMarker,
  270. MaxKeys: maxKeys,
  271. Delimiter: delimiter,
  272. IsTruncated: cursor.isTruncated,
  273. Contents: contents,
  274. CommonPrefixes: commonPrefixes,
  275. }
  276. if encodingTypeUrl {
  277. sort.Slice(response.CommonPrefixes, func(i, j int) bool {
  278. return response.CommonPrefixes[i].Prefix < response.CommonPrefixes[j].Prefix
  279. })
  280. response.EncodingType = s3.EncodingTypeUrl
  281. }
  282. return nil
  283. })
  284. return
  285. }
  286. type ListingCursor struct {
  287. maxKeys uint16
  288. isTruncated bool
  289. prefixEndsOnDelimiter bool
  290. nextMarker string
  291. }
  292. func (l *ListingCursor) Decrease() {
  293. l.maxKeys--
  294. if l.maxKeys == 0 {
  295. l.isTruncated = true
  296. }
  297. }
  298. func getStartFileFromKey(key string) string {
  299. idx := strings.LastIndex(key, "/")
  300. if idx == -1 {
  301. return "/" + key
  302. }
  303. return fmt.Sprintf("/%s%s", key[0:idx], key[idx+1:len(key)])
  304. }
  305. // the prefix and marker may be in different directories
  306. // normalizePrefixMarker ensures the prefix and marker both starts from the same directory
  307. func normalizePrefixMarker(prefix, marker string) (alignedDir, alignedPrefix, alignedMarker string) {
  308. // alignedDir should not end with "/"
  309. // alignedDir, alignedPrefix, alignedMarker should only have "/" in middle
  310. if len(marker) == 0 {
  311. prefix = strings.Trim(prefix, "/")
  312. } else {
  313. prefix = strings.TrimLeft(prefix, "/")
  314. }
  315. marker = strings.TrimLeft(marker, "/")
  316. if prefix == "" {
  317. return "", "", marker
  318. }
  319. if marker == "" {
  320. alignedDir, alignedPrefix = toDirAndName(prefix)
  321. return
  322. }
  323. if !strings.HasPrefix(marker, prefix) {
  324. // something wrong
  325. return "", prefix, marker
  326. }
  327. if strings.HasPrefix(marker, prefix+"/") {
  328. alignedDir = prefix
  329. alignedPrefix = ""
  330. alignedMarker = marker[len(alignedDir)+1:]
  331. return
  332. }
  333. alignedDir, alignedPrefix = toDirAndName(prefix)
  334. if alignedDir != "" {
  335. alignedMarker = marker[len(alignedDir)+1:]
  336. } else {
  337. alignedMarker = marker
  338. }
  339. return
  340. }
  341. func toDirAndName(dirAndName string) (dir, name string) {
  342. sepIndex := strings.LastIndex(dirAndName, "/")
  343. if sepIndex >= 0 {
  344. dir, name = dirAndName[0:sepIndex], dirAndName[sepIndex+1:]
  345. } else {
  346. name = dirAndName
  347. }
  348. return
  349. }
  350. func toParentAndDescendants(dirAndName string) (dir, name string) {
  351. sepIndex := strings.Index(dirAndName, "/")
  352. if sepIndex >= 0 {
  353. dir, name = dirAndName[0:sepIndex], dirAndName[sepIndex+1:]
  354. } else {
  355. name = dirAndName
  356. }
  357. return
  358. }
  359. func (s3a *S3ApiServer) doListFilerRecursiveEntries(client filer_pb.SeaweedFilerClient, dir, prefix string, cursor *ListingCursor, marker, delimiter string, inclusiveStartFrom bool, eachEntryFn func(dir string, entry *filer_pb.Entry)) (err error) {
  360. if prefix == "/" && delimiter == "/" {
  361. return
  362. }
  363. request := &filer_pb.ListEntriesRequest{
  364. Directory: dir,
  365. Prefix: prefix,
  366. Limit: uint32(cursor.maxKeys) + 1,
  367. StartFromFileName: marker,
  368. InclusiveStartFrom: inclusiveStartFrom,
  369. Recursive: true,
  370. Delimiter: delimiter == "/",
  371. }
  372. ctx, cancel := context.WithCancel(context.Background())
  373. defer cancel()
  374. stream, listErr := client.ListEntries(ctx, request)
  375. if listErr != nil {
  376. return fmt.Errorf("list entires %+v: %v", request, listErr)
  377. }
  378. for {
  379. resp, recvErr := stream.Recv()
  380. if recvErr != nil {
  381. if recvErr == io.EOF {
  382. break
  383. } else {
  384. return fmt.Errorf("iterating entires %+v: %v", request, recvErr)
  385. }
  386. }
  387. eachEntryFn(resp.Path, resp.Entry)
  388. }
  389. return
  390. }
  391. func (s3a *S3ApiServer) doListFilerEntries(client filer_pb.SeaweedFilerClient, dir, prefix string, cursor *ListingCursor, marker, delimiter string, inclusiveStartFrom bool, eachEntryFn func(dir string, entry *filer_pb.Entry)) (nextMarker string, err error) {
  392. // invariants
  393. // prefix and marker should be under dir, marker may contain "/"
  394. // maxKeys should be updated for each recursion
  395. glog.V(0).Infof("doListFilerEntries dir: %s, prefix: %s, marker %s, maxKeys: %d, prefixEndsOnDelimiter: %+v", dir, prefix, marker, cursor.maxKeys, cursor.prefixEndsOnDelimiter)
  396. if prefix == "/" && delimiter == "/" {
  397. return
  398. }
  399. if cursor.maxKeys <= 0 {
  400. return
  401. }
  402. if strings.Contains(marker, "/") {
  403. subDir, subMarker := toParentAndDescendants(marker)
  404. // println("doListFilerEntries dir", dir+"/"+subDir, "subMarker", subMarker)
  405. subNextMarker, subErr := s3a.doListFilerEntries(client, dir+"/"+subDir, "", cursor, subMarker, delimiter, false, eachEntryFn)
  406. if subErr != nil {
  407. err = subErr
  408. return
  409. }
  410. nextMarker = subDir + "/" + subNextMarker
  411. // finished processing this subdirectory
  412. marker = subDir
  413. }
  414. if cursor.isTruncated {
  415. return
  416. }
  417. // now marker is also a direct child of dir
  418. request := &filer_pb.ListEntriesRequest{
  419. Directory: dir,
  420. Prefix: prefix,
  421. Limit: uint32(cursor.maxKeys + 2), // bucket root directory needs to skip additional s3_constants.MultipartUploadsFolder folder
  422. StartFromFileName: marker,
  423. InclusiveStartFrom: inclusiveStartFrom,
  424. }
  425. if cursor.prefixEndsOnDelimiter {
  426. request.Limit = uint32(1)
  427. }
  428. ctx, cancel := context.WithCancel(context.Background())
  429. defer cancel()
  430. stream, listErr := client.ListEntries(ctx, request)
  431. if listErr != nil {
  432. err = fmt.Errorf("list entires %+v: %v", request, listErr)
  433. return
  434. }
  435. for {
  436. resp, recvErr := stream.Recv()
  437. if recvErr != nil {
  438. if recvErr == io.EOF {
  439. break
  440. } else {
  441. err = fmt.Errorf("iterating entires %+v: %v", request, recvErr)
  442. return
  443. }
  444. }
  445. if cursor.maxKeys <= 0 {
  446. cursor.isTruncated = true
  447. continue
  448. }
  449. entry := resp.Entry
  450. nextMarker = entry.Name
  451. if cursor.prefixEndsOnDelimiter {
  452. if entry.Name == prefix && entry.IsDirectory {
  453. if delimiter != "/" {
  454. cursor.prefixEndsOnDelimiter = false
  455. }
  456. } else {
  457. continue
  458. }
  459. }
  460. if entry.IsDirectory {
  461. // glog.V(4).Infof("List Dir Entries %s, file: %s, maxKeys %d", dir, entry.Name, cursor.maxKeys)
  462. if entry.Name == s3_constants.MultipartUploadsFolder { // FIXME no need to apply to all directories. this extra also affects maxKeys
  463. continue
  464. }
  465. if delimiter != "/" || cursor.prefixEndsOnDelimiter {
  466. if cursor.prefixEndsOnDelimiter {
  467. cursor.prefixEndsOnDelimiter = false
  468. if entry.IsDirectoryKeyObject() {
  469. eachEntryFn(dir, entry)
  470. }
  471. } else {
  472. eachEntryFn(dir, entry)
  473. }
  474. subNextMarker, subErr := s3a.doListFilerEntries(client, dir+"/"+entry.Name, "", cursor, "", delimiter, false, eachEntryFn)
  475. if subErr != nil {
  476. err = fmt.Errorf("doListFilerEntries2: %v", subErr)
  477. return
  478. }
  479. // println("doListFilerEntries2 dir", dir+"/"+entry.Name, "subNextMarker", subNextMarker)
  480. nextMarker = entry.Name + "/" + subNextMarker
  481. if cursor.isTruncated {
  482. return
  483. }
  484. // println("doListFilerEntries2 nextMarker", nextMarker)
  485. } else {
  486. var isEmpty bool
  487. if !s3a.option.AllowEmptyFolder && entry.IsOlderDir() {
  488. //if isEmpty, err = s3a.ensureDirectoryAllEmpty(client, dir, entry.Name); err != nil {
  489. // glog.Errorf("check empty folder %s: %v", dir, err)
  490. //}
  491. }
  492. if !isEmpty {
  493. eachEntryFn(dir, entry)
  494. }
  495. }
  496. } else {
  497. eachEntryFn(dir, entry)
  498. // glog.V(4).Infof("List File Entries %s, file: %s, maxKeys %d", dir, entry.Name, cursor.maxKeys)
  499. }
  500. if cursor.prefixEndsOnDelimiter {
  501. cursor.prefixEndsOnDelimiter = false
  502. }
  503. }
  504. return
  505. }
  506. func getListObjectsV2Args(values url.Values) (prefix, startAfter, delimiter string, token OptionalString, encodingTypeUrl bool, fetchOwner bool, maxkeys uint16) {
  507. prefix = values.Get("prefix")
  508. token = OptionalString{set: values.Has("continuation-token"), string: values.Get("continuation-token")}
  509. startAfter = values.Get("start-after")
  510. delimiter = values.Get("delimiter")
  511. encodingTypeUrl = values.Get("encoding-type") == s3.EncodingTypeUrl
  512. if values.Get("max-keys") != "" {
  513. if maxKeys, err := strconv.ParseUint(values.Get("max-keys"), 10, 16); err == nil {
  514. maxkeys = uint16(maxKeys)
  515. }
  516. } else {
  517. maxkeys = maxObjectListSizeLimit
  518. }
  519. fetchOwner = values.Get("fetch-owner") == "true"
  520. return
  521. }
  522. func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, encodingTypeUrl bool, maxkeys int16) {
  523. prefix = values.Get("prefix")
  524. marker = values.Get("marker")
  525. delimiter = values.Get("delimiter")
  526. encodingTypeUrl = values.Get("encoding-type") == "url"
  527. if values.Get("max-keys") != "" {
  528. if maxKeys, err := strconv.ParseInt(values.Get("max-keys"), 10, 16); err == nil {
  529. maxkeys = int16(maxKeys)
  530. }
  531. } else {
  532. maxkeys = maxObjectListSizeLimit
  533. }
  534. return
  535. }
  536. func (s3a *S3ApiServer) ensureDirectoryAllEmpty(filerClient filer_pb.SeaweedFilerClient, parentDir, name string) (isEmpty bool, err error) {
  537. // println("+ ensureDirectoryAllEmpty", dir, name)
  538. glog.V(4).Infof("+ isEmpty %s/%s", parentDir, name)
  539. defer glog.V(4).Infof("- isEmpty %s/%s %v", parentDir, name, isEmpty)
  540. var fileCounter int
  541. var subDirs []string
  542. currentDir := parentDir + "/" + name
  543. var startFrom string
  544. var isExhausted bool
  545. var foundEntry bool
  546. for fileCounter == 0 && !isExhausted && err == nil {
  547. err = filer_pb.SeaweedList(filerClient, currentDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  548. foundEntry = true
  549. if entry.IsOlderDir() {
  550. subDirs = append(subDirs, entry.Name)
  551. } else {
  552. fileCounter++
  553. }
  554. startFrom = entry.Name
  555. isExhausted = isExhausted || isLast
  556. glog.V(4).Infof(" * %s/%s isLast: %t", currentDir, startFrom, isLast)
  557. return nil
  558. }, startFrom, false, 8)
  559. if !foundEntry {
  560. break
  561. }
  562. }
  563. if err != nil {
  564. return false, err
  565. }
  566. if fileCounter > 0 {
  567. return false, nil
  568. }
  569. for _, subDir := range subDirs {
  570. isSubEmpty, subErr := s3a.ensureDirectoryAllEmpty(filerClient, currentDir, subDir)
  571. if subErr != nil {
  572. return false, subErr
  573. }
  574. if !isSubEmpty {
  575. return false, nil
  576. }
  577. }
  578. glog.V(1).Infof("deleting empty folder %s", currentDir)
  579. if err = doDeleteEntry(filerClient, parentDir, name, true, false); err != nil {
  580. return
  581. }
  582. return true, nil
  583. }