예제 #1
0
파일: sbpfs.py 프로젝트: gtxx3600/SBPFS
def get():
	try:
		path = '/'
		import sys
		if len(sys.argv[1:]) > 1:
			if sys.argv[2].startswith('/'):
				path = sys.argv[2]
				if path.endswith('/'):
					path = path[:-1]
		
		else:
			print 'arg err get failed'
			return
		filename = path.split("/")[-1]
		if not filename:
			print 'Invalid filename'
			return
		fd = sbpfs.open(path, 1, 0)
		if fd < 0: 
			print 'Make sure your request path exist and file-type must be file'
			return
		dict = sbpfs.stat(path)
		length = int(dict['size'])
		if os.path.exists(filename):
			print 'File : %s already exists' % filename
			sbpfs.close(fd)
			return
		f = file(filename, 'wb')
		bufsize = 1024*1024
		for i in range(0, length / bufsize):
			r = sbpfs.read(fd, bufsize)
			if r and len(r) == bufsize:
				f.write(r)
				print "received : %.2f%% of %d bytes" % (((float(i*bufsize)/length)*100) ,length)
			else:
				print 'Error occur'
				sbpfs.perror('')
				sbpfs.close(fd)
				return
		if length % bufsize:
			r = sbpfs.read(fd, length % bufsize)
			if r and len(r) == length % bufsize:
				f.write(r)
			print "received : %.2f%%" % float(100.0)
		f.close()
		sbpfs.close(fd)
				
					
	except:
		print 'Unknow error occur'
		import traceback
		import sys
		traceback.print_exc(file=sys.stdout)
예제 #2
0
파일: sbpfs.py 프로젝트: gtxx3600/SBPFS
def ls():
	path = '/'
	detail = False
	import sys 
	if len(sys.argv[1:]) > 1:
		if sys.argv[2].startswith('/'):
			path = sys.argv[2]
			if path.endswith('/'):
				path = path[:-1]
		if "-l" in sys.argv[1:]:
			detail = True
	
	fd = sbpfs.opendir(path)
	if(fd < 0):
		print 'Opendir Failed \nMake sure your request path exist and file-type must be directory'
		return
	if detail:
		print '%s %s %s %s %s' % ('name'.ljust(15), 'length'.ljust(15), 'mode'.ljust(10), 'owner'.ljust(8), 'last modify time'.ljust(33))
	try:
		while True:
			r = sbpfs.readdir(fd)
			if r:
				offset, type, name = r
				disp = ''
				def getmode(type, mod):
					ret = ''
					if(type == 'directory'):
					    ret += 'd'
					else:
						ret += '-'
					if mod & 0x04:
						ret += 'w'
					else :
						ret += '-'
					if mod & 0x08:
						ret += 'r'
					else:
						ret += '-'
					ret += ' '
					if mod & 0x01:
						ret += 'w'
					else:
						ret += '-'
					if mod & 0x02:
						ret += 'r'
					else:
						ret += '-'
					return ret
				if detail:
					import time
					dict = sbpfs.stat(path + '/' + name)
#					disp += '%.15' % name
#					disp += '%.10s ' % dict['size']
#					disp += '%.10s ' % getmode(type,int(dict['mode']))
#					disp += '%.8s' % dict['owner']
#					disp += '%.33s' % time.strftime("%D %H:%M:%S", time.gmtime(int(dict['modify_time'])))
					print name.ljust(15),str(dict['size']).ljust(15),getmode(type,int(dict['mode'])).ljust(10),str(dict['owner']).ljust(8),time.strftime("%Y-%m-%d %X", time.gmtime(int(dict['modify_time']))).ljust(33)

				else:
					print name
				
			else:
				break;
	except:
		import traceback
		import sys
		traceback.print_exc(file=sys.stdout)
	sbpfs.closedir(fd)