abfsstats.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Licensed to Cloudera, Inc. under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. Cloudera, Inc. licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import absolute_import
  17. import stat
  18. import logging
  19. from azure.abfs.__init__ import strip_path, abfsdatetime_to_timestamp
  20. from django.utils.encoding import smart_str
  21. LOG = logging.getLogger()
  22. CHAR_TO_OCT = {"---": 0, "--x": 1, "-w-": 2, "-wx": 3, "r--": 4, "r-x": 5, "rw-": 6, "rwx": 7}
  23. class ABFSStat(object):
  24. def __init__(self, isDir, atime, mtime, size, path, owner='', group='', mode=None):
  25. self.name = strip_path(path)
  26. self.path = path
  27. self.isDir = isDir
  28. self.type = 'DIRECTORY' if isDir else 'FILE'
  29. try:
  30. self.atime = abfsdatetime_to_timestamp(atime) if atime else 0
  31. self.mtime = abfsdatetime_to_timestamp(mtime) if mtime else 0
  32. except:
  33. self.atime = 0
  34. self.mtime = 0
  35. self.size = size
  36. self.user = owner if owner else ''
  37. self.group = group if group else ''
  38. self.mode = mode or (0o777 if isDir else 0o666)
  39. if self.isDir:
  40. self.mode |= stat.S_IFDIR
  41. else:
  42. self.mode |= stat.S_IFREG
  43. def __getitem__(self, key):
  44. try:
  45. return getattr(self, key)
  46. except AttributeError:
  47. raise KeyError(key)
  48. def __setitem__(self, key, value):
  49. # What about derivable values?
  50. setattr(self, key, value)
  51. def __repr__(self):
  52. return smart_str("<abfsStat %s>" % (self.path,))
  53. @property
  54. def aclBit(self):
  55. return False
  56. @classmethod
  57. def for_root(cls, path):
  58. return cls(True, 0, 0, 0, path)
  59. @classmethod
  60. def for_filesystems(cls, headers, resp, scheme):
  61. return cls(True, headers['date'], resp['lastModified'], 0, scheme + resp['name'])
  62. @classmethod
  63. def for_directory(cls, headers, resp, path):
  64. try:
  65. size = int(resp['contentLength'])
  66. except:
  67. size = 0
  68. try:
  69. isDir = resp['isDirectory'] == 'true'
  70. except:
  71. isDir = False
  72. try:
  73. permissions = ABFSStat.char_permissions_to_oct_permissions(resp['permissions'])
  74. except:
  75. permissions = None
  76. return cls(isDir, headers['date'], resp.get('lastModified'), size, path, resp.get('owner'), resp.get('group'), mode=permissions)
  77. @classmethod
  78. def for_single(cls, resp, path):
  79. size = int(resp['Content-Length'])
  80. isDir = resp['x-ms-resource-type'] == 'directory'
  81. try:
  82. permissions = ABFSStat.char_permissions_to_oct_permissions(resp['x-ms-permissions'])
  83. except:
  84. permissions = None
  85. return cls(isDir, resp['date'], resp['Last-Modified'], size, path, resp.get('x-ms-owner'), resp.get('x-ms-group'), mode=permissions)
  86. @classmethod
  87. def for_filesystem(cls, resp, path):
  88. return cls(True, resp['date'], resp['Last-Modified'], 0, path)
  89. @staticmethod
  90. def char_permissions_to_oct_permissions(permissions):
  91. try:
  92. octal_permissions = CHAR_TO_OCT[permissions[0:3]] * 64 + CHAR_TO_OCT[permissions[3:6]] * 8 + CHAR_TO_OCT[permissions[6:]]
  93. except:
  94. return None
  95. return octal_permissions
  96. def to_json_dict(self):
  97. """
  98. Returns a dictionary for easy serialization
  99. """
  100. keys = ('path', 'size', 'atime', 'mtime', 'mode', 'user', 'group', 'aclBit')
  101. res = {}
  102. for k in keys:
  103. res[k] = self[k]
  104. return res