1 # -*- encoding: utf-8 -*-
4 from tarfile import filemode
16 from service import security
18 #from document.nodes import node_res_dir, node_res_obj
19 from document.nodes import get_node_context
22 def _get_month_name(month):
24 if month==1:return 'Jan'
25 elif month==2:return 'Feb'
26 elif month==3:return 'Mar'
27 elif month==4:return 'Apr'
28 elif month==5:return 'May'
29 elif month==6:return 'Jun'
30 elif month==7:return 'Jul'
31 elif month==8:return 'Aug'
32 elif month==9:return 'Sep'
33 elif month==10:return 'Oct'
34 elif month==11:return 'Nov'
35 elif month==12:return 'Dec'
39 return s.decode('utf-8')
42 return s.decode('latin')
45 return s.encode('ascii')
51 return s.encode('utf-8')
54 return s.encode('latin')
57 return s.decode('ascii')
61 class abstracted_fs(object):
62 """A class used to interact with the file system, providing a high
63 level, cross-platform interface compatible with both Windows and
64 UNIX style filesystems.
66 It provides some utility methods and some wraps around operations
67 involved in file creation and file system operations like moving
68 files or removing directories.
71 - (str) root: the user home directory.
72 - (str) cwd: the current working directory.
73 - (str) rnfr: source file to be renamed.
82 self._log = logging.getLogger('FTP.fs')
86 """Get the list of available databases, with FTPd support
88 s = netsvc.ExportService.getService('db')
89 result = s.exp_list(document=True)
90 self.db_name_list = []
91 for db_name in result:
95 db = pooler.get_db_only(db_name)
97 cr.execute("SELECT 1 FROM pg_class WHERE relkind = 'r' AND relname = 'ir_module_module'")
101 cr.execute("SELECT id FROM ir_module_module WHERE name = 'document_ftp' AND state='installed' ")
104 self.db_name_list.append(db_name)
107 self._log.warning('Cannot use db "%s"', db_name)
112 # pooler.close_db(db_name)
113 return self.db_name_list
115 def ftpnorm(self, ftppath):
116 """Normalize a "virtual" ftp pathname (tipically the raw string
119 Pathname returned is relative!.
121 p = os.path.normpath(ftppath)
122 # normalize string in a standard web-path notation having '/'
123 # as separator. xrg: is that really in the spec?
124 p = p.replace("\\", "/")
125 # os.path.normpath supports UNC paths (e.g. "//a/b/c") but we
126 # don't need them. In case we get an UNC path we collapse
127 # redundant separators appearing at the beginning of the string
135 """ return the cwd, decoded in utf"""
136 return _to_decode(self.cwd)
138 def ftp2fs(self, path_orig, data):
139 raise DeprecationWarning()
141 def fs2ftp(self, node):
142 """ Return the string path of a node, in ftp form
146 paths = node.full_path()
147 res = '/' + node.context.dbname + '/' + \
148 _to_decode(os.path.join(*paths))
152 def validpath(self, path):
153 """Check whether the path belongs to user's home directory.
154 Expected argument is a datacr tuple
156 # TODO: are we called for "/" ?
157 return isinstance(path, tuple) and path[1] and True or False
159 # --- Wrapper methods around open() and tempfile.mkstemp
161 def create(self, datacr, objname, mode):
162 """ Create a children file-node under node, open it
163 @return open node_descriptor of the created node
165 objname = _to_unicode(objname)
166 cr , node, rem = datacr
168 child = node.child(cr, objname)
170 if child.type not in ('file','content'):
171 raise OSError(1, 'Operation not permited.')
173 ret = child.open_data(cr, mode)
175 except EnvironmentError:
178 self._log.exception('Cannot locate item %s at node %s', objname, repr(node))
182 child = node.create_child(cr, objname, data=None)
183 return child.open_data(cr, mode)
185 self._log.exception('Cannot create item %s at node %s', objname, repr(node))
186 raise OSError(1, 'Operation not permited.')
188 def open(self, datacr, mode):
189 if not (datacr and datacr[1]):
190 raise OSError(1, 'Operation not permited.')
192 cr, node, rem = datacr
193 res = node.open_data(cr, mode)
196 # ok, but need test more
198 def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
199 """A wrap around tempfile.mkstemp creating a file with a unique
200 name. Unlike mkstemp it returns an object with a file-like
203 raise NotImplementedError
205 text = not 'b' in mode
206 # for unique file , maintain version if duplicate file
211 pool = pooler.get_pool(node.context.dbname)
212 object=dir and dir.object or False
213 object2=dir and dir.object2 or False
214 res=pool.get('ir.attachment').search(cr,uid,[('name','like',prefix),('parent_id','=',object and object.type in ('directory','ressource') and object.id or False),('res_id','=',object2 and object2.id or False),('res_model','=',object2 and object2._name or False)])
216 pre = prefix.split('.')
217 prefix=pre[0] + '.v'+str(len(res))+'.'+pre[1]
218 #prefix = prefix + '.'
219 return self.create(dir,suffix+prefix,text)
224 def chdir(self, datacr):
225 if (not datacr) or datacr == (None, None, None):
230 raise OSError(1, 'Operation not permitted')
231 if datacr[1].type not in ('collection','database'):
232 raise OSError(2, 'Path is not a directory')
233 self.cwd = '/'+datacr[1].context.dbname + '/'
234 self.cwd += '/'.join(datacr[1].full_path())
235 self.cwd_node = datacr[1]
238 def mkdir(self, datacr, basename):
239 """Create the specified directory."""
240 cr, node, rem = datacr or (None, None, None)
242 raise OSError(1, 'Operation not permited.')
245 basename =_to_unicode(basename)
246 cdir = node.create_child_collection(cr, basename)
247 self._log.debug("Created child dir: %r", cdir)
250 self._log.exception('Cannot create dir "%s" at node %s', basename, repr(node))
251 raise OSError(1, 'Operation not permited.')
253 def close_cr(self, data):
258 def get_cr(self, pathname):
259 raise DeprecationWarning()
261 def get_crdata(self, line, mode='file'):
262 """ Get database cursor, node and remainder data, for commands
264 This is the helper function that will prepare the arguments for
265 any of the subsequent commands.
266 It returns a tuple in the form of:
267 @code ( cr, node, rem_path=None )
269 @param line An absolute or relative ftp path, as passed to the cmd.
270 @param mode A word describing the mode of operation, so that this
271 function behaves properly in the different commands.
273 path = self.ftpnorm(line)
274 if self.cwd_node is None:
275 if not os.path.isabs(path):
276 path = os.path.join(self.root, path)
278 if path == '/' and mode in ('list', 'cwd'):
279 return (None, None, None )
281 path = os.path.normpath(path) # again, for '/db/../ss'
282 if path == '.': path = ''
284 if os.path.isabs(path) and self.cwd_node is not None \
285 and path.startswith(self.cwd):
286 # make relative, so that cwd_node is used again
287 path = path[len(self.cwd):]
288 if path.startswith('/'):
291 p_parts = path.split('/') # hard-code the unix sep here, by spec.
293 assert '..' not in p_parts
296 if mode in ('create',):
297 rem_path = p_parts[-1]
298 p_parts = p_parts[:-1]
300 if os.path.isabs(path):
301 # we have to start from root, again
302 p_parts = p_parts[1:]
304 raise IOError(errno.EPERM, 'Cannot perform operation at root dir')
306 if dbname not in self.db_list():
307 return IOError(errno.ENOENT,'Invalid database path')
309 db = pooler.get_db(dbname)
311 raise OSError(1, 'Database cannot be used.')
314 uid = security.login(dbname, self.username, self.password)
320 raise OSError(2, 'Authentification Required.')
321 n = get_node_context(cr, uid, {})
322 node = n.get_uri(cr, p_parts[1:])
323 return (cr, node, rem_path)
325 # we never reach here if cwd_node is not set
326 if p_parts and p_parts[-1] == '':
327 p_parts = p_parts[:-1]
328 cr, uid = self.get_node_cr_uid(self.cwd_node)
330 node = self.cwd_node.get_uri(cr, p_parts)
333 if node is False and mode not in ('???'):
335 raise IOError(errno.ENOENT, 'Path does not exist')
336 return (cr, node, rem_path)
338 def get_node_cr_uid(self, node):
339 """ Get cr, uid, pool from a node
342 db = pooler.get_db(node.context.dbname)
343 return db.cursor(), node.context.uid
345 def get_node_cr(self, node):
346 """ Get the cursor for the database of a node
348 The cursor is the only thing that a node will not store
349 persistenly, so we have to obtain a new one for each call.
351 return self.get_node_cr_uid(node)[0]
353 def listdir(self, datacr):
354 """List the content of a directory."""
355 class false_node(object):
364 def __init__(self, db):
367 if datacr[1] is None:
369 for db in self.db_list():
371 result.append(false_node(db))
372 except osv.except_osv:
375 cr, node, rem = datacr
376 res = node.children(cr)
379 def rmdir(self, datacr):
380 """Remove the specified directory."""
381 cr, node, rem = datacr
383 cr = self.get_node_cr(node)
387 def remove(self, datacr):
389 if datacr[1].type == 'collection':
390 return self.rmdir(datacr)
391 elif datacr[1].type == 'file':
392 return self.rmfile(datacr)
393 raise OSError(1, 'Operation not permited.')
395 def rmfile(self, datacr):
396 """Remove the specified file."""
402 def rename(self, src, datacr):
403 """ Renaming operation, the effect depends on the src:
404 * A file: read, create and remove
405 * A directory: change the parent and reassign childs to ressource
409 nname = _to_unicode(datacr[2])
410 ret = src.move_to(cr, datacr[1], new_name=nname)
411 # API shouldn't wait for us to write the object
412 assert (ret is True) or (ret is False)
414 except Exception,err:
415 self._log.exception('Cannot rename "%s" to "%s" at "%s"', src, dst_basename, dst_basedir)
416 raise OSError(1,'Operation not permited.')
418 def stat(self, node):
419 raise NotImplementedError()
421 # --- Wrapper methods around os.path.*
424 def isfile(self, node):
425 if node and (node.type in ('file','content')):
430 def islink(self, path):
431 """Return True if path is a symbolic link."""
434 def isdir(self, node):
435 """Return True if path is a directory."""
438 if node and (node.type in ('collection','database')):
442 def getsize(self, datacr):
443 """Return the size of the specified file in bytes."""
444 if not (datacr and datacr[1]):
446 if datacr[1].type in ('file', 'content'):
447 return datacr[1].content_length or 0L
451 def getmtime(self, datacr):
452 """Return the last modified time as a number of seconds since
456 if node.write_date or node.create_date:
457 dt = (node.write_date or node.create_date)[:19]
458 result = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
460 result = time.mktime(time.localtime())
464 def realpath(self, path):
465 """Return the canonical version of path eliminating any
466 symbolic links encountered in the path (if they are
467 supported by the operating system).
472 def lexists(self, path):
473 """Return True if path refers to an existing path, including
474 a broken or circular symbolic link.
476 raise DeprecationWarning()
477 return path and True or False
481 # Ok, can be improved
482 def glob1(self, dirname, pattern):
483 """Return a list of files matching a dirname pattern
486 Unlike glob.glob1 raises exception if os.listdir() fails.
488 names = self.listdir(dirname)
489 if pattern[0] != '.':
490 names = filter(lambda x: x.path[0] != '.', names)
491 return fnmatch.filter(names, pattern)
493 # --- Listing utilities
495 # note: the following operations are no more blocking
497 def get_list_dir(self, datacr):
498 """"Return an iterator object that yields a directory listing
499 in a form suitable for LIST command.
503 elif self.isdir(datacr[1]):
504 listing = self.listdir(datacr)
506 return self.format_list(datacr[0], datacr[1], listing)
507 # if path is a file or a symlink we return information about it
508 elif self.isfile(datacr[1]):
509 par = datacr[1].parent
510 return self.format_list(datacr[0], par, [datacr[1]])
512 def get_stat_dir(self, rawline, datacr):
513 """Return an iterator object that yields a list of files
514 matching a dirname pattern non-recursively in a form
515 suitable for STAT command.
517 - (str) rawline: the raw string passed by client as command
520 ftppath = self.ftpnorm(rawline)
521 if not glob.has_magic(ftppath):
522 return self.get_list_dir(self.ftp2fs(rawline, datacr))
524 basedir, basename = os.path.split(ftppath)
525 if glob.has_magic(basedir):
526 return iter(['Directory recursion not supported.\r\n'])
528 basedir = self.ftp2fs(basedir, datacr)
529 listing = self.glob1(basedir, basename)
532 return self.format_list(basedir, listing)
534 def format_list(self, cr, parent_node, listing, ignore_err=True):
535 """Return an iterator object that yields the entries of given
536 directory emulating the "/bin/ls -lA" UNIX command output.
538 - (str) basedir: the parent directory node. Can be None
539 - (list) listing: a list of nodes
540 - (bool) ignore_err: when False raise exception if os.lstat()
543 On platforms which do not support the pwd and grp modules (such
544 as Windows), ownership is printed as "owner" and "group" as a
545 default, and number of hard links is always "1". On UNIX
546 systems, the actual owner, group, and number of links are
549 This is how output appears to client:
551 -rw-rw-rw- 1 owner group 7045120 Sep 02 3:47 music.mp3
552 drwxrwxrwx 1 owner group 0 Aug 31 18:50 e-books
553 -rw-rw-rw- 1 owner group 380 Sep 02 3:40 module.py
556 perms = filemode(node.unixperms) # permissions
558 size = node.content_length or 0L
561 # stat.st_mtime could fail (-1) if last mtime is too old
562 # in which case we return the local time as last mtime
564 st_mtime = node.write_date or 0.0
565 if isinstance(st_mtime, basestring):
566 st_mtime = time.strptime(st_mtime, '%Y-%m-%d %H:%M:%S')
567 elif isinstance(st_mtime, float):
568 st_mtime = time.localtime(st_mtime)
569 mname=_get_month_name(time.strftime("%m", st_mtime ))
570 mtime = mname+' '+time.strftime("%d %H:%M", st_mtime)
572 mname=_get_month_name(time.strftime("%m"))
573 mtime = mname+' '+time.strftime("%d %H:%M")
575 if isinstance(fpath, (list, tuple)):
577 # formatting is matched with proftpd ls output
578 path=_to_decode(fpath)
579 yield "%s %3s %-8s %-8s %8s %s %s\r\n" %(perms, nlinks, uname, gname,
583 def format_mlsx(self, cr, basedir, listing, perms, facts, ignore_err=True):
584 """Return an iterator object that yields the entries of a given
585 directory or of a single file in a form suitable with MLSD and
588 Every entry includes a list of "facts" referring the listed
589 element. See RFC-3659, chapter 7, to see what every single
592 - (str) basedir: the absolute dirname.
593 - (list) listing: the names of the entries in basedir
594 - (str) perms: the string referencing the user permissions.
595 - (str) facts: the list of "facts" to be returned.
596 - (bool) ignore_err: when False raise exception if os.stat()
599 Note that "facts" returned may change depending on the platform
600 and on what user specified by using the OPTS command.
602 This is how output could appear to the client issuing
605 type=file;size=156;perm=r;modify=20071029155301;unique=801cd2; music.mp3
606 type=dir;size=0;perm=el;modify=20071127230206;unique=801e33; ebooks
607 type=file;size=211;perm=r;modify=20071103093626;unique=801e32; module.py
609 permdir = ''.join([x for x in perms if x not in 'arw'])
610 permfile = ''.join([x for x in perms if x not in 'celmp'])
611 if ('w' in perms) or ('a' in perms) or ('f' in perms):
615 type = size = perm = modify = create = unique = mode = uid = gid = ""
622 perm = 'perm=%s;' %permdir
627 perm = 'perm=%s;' %permfile
629 size = 'size=%s;' % (node.content_length or 0L)
630 # last modification time
631 if 'modify' in facts:
633 st_mtime = node.write_date or 0.0
634 if isinstance(st_mtime, basestring):
635 st_mtime = time.strptime(st_mtime, '%Y-%m-%d %H:%M:%S')
636 elif isinstance(st_mtime, float):
637 st_mtime = time.localtime(st_mtime)
638 modify = 'modify=%s;' %time.strftime("%Y%m%d%H%M%S", st_mtime)
640 # stat.st_mtime could fail (-1) if last mtime is too old
642 if 'create' in facts:
643 # on Windows we can provide also the creation time
645 st_ctime = node.create_date or 0.0
646 if isinstance(st_ctime, basestring):
647 st_ctime = time.strptime(st_ctime, '%Y-%m-%d %H:%M:%S')
648 elif isinstance(st_mtime, float):
649 st_ctime = time.localtime(st_ctime)
650 create = 'create=%s;' %time.strftime("%Y%m%d%H%M%S",st_ctime)
654 if 'unix.mode' in facts:
655 mode = 'unix.mode=%s;' %oct(node.unixperms & 0777)
656 if 'unix.uid' in facts:
657 uid = 'unix.uid=%s;' % node.uuser
658 if 'unix.gid' in facts:
659 gid = 'unix.gid=%s;' % node.ugroup
660 # We provide unique fact (see RFC-3659, chapter 7.5.2) on
661 # posix platforms only; we get it by mixing st_dev and
662 # st_ino values which should be enough for granting an
663 # uniqueness for the file listed.
664 # The same approach is used by pure-ftpd.
665 # Implementors who want to provide unique fact on other
666 # platforms should use some platform-specific method (e.g.
667 # on Windows NTFS filesystems MTF records could be used).
668 # if 'unique' in facts: todo
669 # unique = "unique=%x%x;" %(st.st_dev, st.st_ino)
671 if isinstance (path, (list, tuple)):
673 path=_to_decode(path)
674 yield "%s%s%s%s%s%s%s%s%s %s\r\n" %(type, size, perm, modify, create,
675 mode, uid, gid, unique, path)
677 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: