Skip to content

Commit

Permalink
winazurestorage integration, readdir, getattr
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmet alp balkan committed Jun 1, 2012
1 parent 51b1525 commit 69e918b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
*.swp
tmp/*
99 changes: 93 additions & 6 deletions azurefs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import logging
from sys import argv, exit
from stat import S_IFDIR, S_IFREG
from errno import ENOENT, ENOSYS
from os import getuid
import time
from datetime import datetime

from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
from winazurestorage import *
Expand All @@ -10,23 +15,105 @@
log.addHandler(ch)
log.setLevel(logging.DEBUG)

blobs = None

class AzureFS(LoggingMixIn, Operations):
'Azure Blob Storage filesystem'

blobs = None

dirs = {}
files = {} #key: dirname

def __init__(self, account, key):
blobs = BlobStorage(CLOUD_BLOB_HOST, account, key)
containers = blobs.list_containers()
for i in containers: print(i)
self.blobs = BlobStorage(CLOUD_BLOB_HOST, account, key)
now = time.time()
uid = getuid()

self._refresh_dirs()

def _fetch_containers(self):
r = {}
for c in self.blobs.list_containers():
r['/'+c[0]] = dict(st_mode = (S_IFDIR | 0755), st_uid = getuid(),
st_mtime = int(time.mktime(c[2])), st_size = 0,
st_nlink = 2)
return r

def _refresh_dirs(self):
self.dirs = self._fetch_containers()
max_date = sorted([self.dirs[c]['st_mtime'] for c in self.dirs])[-1]

self.dirs['/'] = dict(st_mode = (S_IFDIR | 0755), st_uid = getuid(),
st_mtime = max_date, st_size=0 , st_nlink=2)
return self.dirs

def _parse_path(self, path): # returns </dir, file(=None)>
if path.count('/') > 1: #file
return path[:path.rfind('/')], path[path.rfind('/')+1:]
else: #dir
return path[:path[1:].rfind('/')], None

def _get_container(self, path):
base_container = path[1:] #/abc/def/g --> abc
if base_container.find('/') > -1:
base_container = base_container[:base_container.find('/')]
return base_container

def _list_container_blobs(self, path):
d,f = self._parse_path(path)
if d in self.files:
log.debug("LISTING %s, found" % d)
return self.files[d]

c = self._get_container(path)
log.info("LISTING %s, CONTAINER %s, PATH %s" % (d,c, path))

blobs = self.blobs.list_blobs(c)

l = dict()
for f in blobs:
l[f[0]] = dict(st_mode=(S_IFREG | 0755), st_size=f[3],
st_mtime=int(time.mktime(f[2])), st_uid=getuid())


self.files[d] = l
return l

# FUSE
def mkdir(self, path, mode):
log.info('Create directory %s' % path)
d = dict(st_mode = (S_IFDIR | mode), st_nlink = 2, st_size = 0)
#st_ctime = time(), st_mtime = time(), st_atime = time())
self.dirs[path] = d
#TODO refresh containers needed?
#TODO PUT container

def getattr(self, path, fh=None):
log.info('Requesting attributes for %s' % path)
return None
d,f = self._parse_path(path)

if f is None: # dir
if path not in self.dirs:
raise FuseOSError(ENOENT)
return self.dirs[path]
else:
blobs = self._list_container_blobs(path)
if f not in blobs:
raise FuseOSError(ENOSYS)
else:
return blobs[f]

def getxattr(self, path, name, position=0):
return ''

def readdir(self, path, fh):
if path == '/':
return ['.', '..'] + [(x[1:]) for x in self.dirs if x != '/']
else:
blobs = self._list_container_blobs(path)
return ['.', '..'] + blobs.keys()

def chown(self, path, uid, gid): pass
def chmod(self, path, mode): pass

if __name__ == '__main__':
if len(argv) < 4:
Expand Down
8 changes: 0 additions & 8 deletions azuretest.py

This file was deleted.

5 changes: 3 additions & 2 deletions winazurestorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ def list_blobs(self, container_name, blob_prefix=None):
for blob in blobs:
blob_name = blob.getElementsByTagName("Name")[0].firstChild.data
etag = blob.getElementsByTagName("Etag")[0].firstChild.data
size = long(blob.getElementsByTagName("Content-Length")[0].firstChild.data)
last_modified = time.strptime(blob.getElementsByTagName("Last-Modified")[0].firstChild.data, TIME_FORMAT)
yield (blob_name, etag, last_modified)
yield (blob_name, etag, last_modified, size)
try: marker = dom.getElementsByTagName("NextMarker")[0].firstChild.data
except: marker = None
if marker is None: break
Expand All @@ -405,4 +406,4 @@ def main():
pass

if __name__ == '__main__':
main()
main()

0 comments on commit 69e918b

Please sign in to comment.