1 # -*- encoding: utf-8 -*-
5 from tarfile import filemode
12 from openerp import pooler, netsvc, sql_db
13 from openerp.service import security
14 from openerp.osv import osv
16 from document.document import get_node_context
18 def _get_month_name(month):
20 if month==1:return 'Jan'
21 elif month==2:return 'Feb'
22 elif month==3:return 'Mar'
23 elif month==4:return 'Apr'
24 elif month==5:return 'May'
25 elif month==6:return 'Jun'
26 elif month==7:return 'Jul'
27 elif month==8:return 'Aug'
28 elif month==9:return 'Sep'
29 elif month==10:return 'Oct'
30 elif month==11:return 'Nov'
31 elif month==12:return 'Dec'
33 from ftpserver import _to_decode, _to_unicode
36 class abstracted_fs(object):
37 """A class used to interact with the file system, providing a high
38 level, cross-platform interface compatible with both Windows and
39 UNIX style filesystems.
41 It provides some utility methods and some wraps around operations
42 involved in file creation and file system operations like moving
43 files or removing directories.
46 - (str) root: the user home directory.
47 - (str) cwd: the current working directory.
48 - (str) rnfr: source file to be renamed.
57 self._log = logging.getLogger(__name__)
61 """Get the list of available databases, with FTPd support
63 s = netsvc.ExportService.getService('db')
64 result = s.exp_list(document=True)
65 self.db_name_list = []
66 for db_name in result:
70 db = sql_db.db_connect(db_name)
72 cr.execute("SELECT 1 FROM pg_class WHERE relkind = 'r' AND relname = 'ir_module_module'")
76 cr.execute("SELECT id FROM ir_module_module WHERE name = 'document_ftp' AND state IN ('installed', 'to install', 'to upgrade') ")
79 self.db_name_list.append(db_name)
82 self._log.warning('Cannot use db "%s".', db_name)
86 return self.db_name_list
88 def ftpnorm(self, ftppath):
89 """Normalize a "virtual" ftp pathname (tipically the raw string
92 Pathname returned is relative!.
94 p = os.path.normpath(ftppath)
95 # normalize string in a standard web-path notation having '/'
96 # as separator. xrg: is that really in the spec?
97 p = p.replace("\\", "/")
98 # os.path.normpath supports UNC paths (e.g. "//a/b/c") but we
99 # don't need them. In case we get an UNC path we collapse
100 # redundant separators appearing at the beginning of the string
108 """ return the cwd, decoded in utf"""
109 return _to_decode(self.cwd)
111 def ftp2fs(self, path_orig, data):
112 raise DeprecationWarning()
114 def fs2ftp(self, node):
115 """ Return the string path of a node, in ftp form
119 paths = node.full_path()
120 res = '/' + node.context.dbname + '/' + \
121 _to_decode(os.path.join(*paths))
125 def validpath(self, path):
126 """Check whether the path belongs to user's home directory.
127 Expected argument is a datacr tuple
129 # TODO: are we called for "/" ?
130 return isinstance(path, tuple) and path[1] and True or False
132 # --- Wrapper methods around open() and tempfile.mkstemp
134 def create(self, datacr, objname, mode):
135 """ Create a children file-node under node, open it
136 @return open node_descriptor of the created node
138 objname = _to_unicode(objname)
139 cr , node, rem = datacr
141 child = node.child(cr, objname)
143 if child.type not in ('file','content'):
144 raise OSError(1, 'Operation is not permitted.')
146 ret = child.open_data(cr, mode)
148 assert ret, "Cannot create descriptor for %r: %r." % (child, ret)
150 except EnvironmentError:
153 self._log.exception('Cannot locate item %s at node %s.', objname, repr(node))
157 child = node.create_child(cr, objname, data=None)
158 ret = child.open_data(cr, mode)
159 assert ret, "Cannot create descriptor for %r." % child
162 except EnvironmentError:
165 self._log.exception('Cannot create item %s at node %s.', objname, repr(node))
166 raise OSError(1, 'Operation is not permitted.')
168 def open(self, datacr, mode):
169 if not (datacr and datacr[1]):
170 raise OSError(1, 'Operation is not permitted.')
172 cr, node, rem = datacr
174 res = node.open_data(cr, mode)
177 raise IOError(errno.EINVAL, "No data.")
180 # ok, but need test more
182 def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
183 """A wrap around tempfile.mkstemp creating a file with a unique
184 name. Unlike mkstemp it returns an object with a file-like
187 raise NotImplementedError # TODO
189 text = not 'b' in mode
190 # for unique file , maintain version if duplicate file
194 pool = pooler.get_pool(node.context.dbname)
195 object=dir and dir.object or False
196 object2=dir and dir.object2 or False
197 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)])
199 pre = prefix.split('.')
200 prefix=pre[0] + '.v'+str(len(res))+'.'+pre[1]
201 return self.create(dir,suffix+prefix,text)
206 def chdir(self, datacr):
207 if (not datacr) or datacr == (None, None, None):
212 raise OSError(1, 'Operation is not permitted.')
213 if datacr[1].type not in ('collection','database'):
214 raise OSError(2, 'Path is not a directory.')
215 self.cwd = '/'+datacr[1].context.dbname + '/'
216 self.cwd += '/'.join(datacr[1].full_path())
217 self.cwd_node = datacr[1]
220 def mkdir(self, datacr, basename):
221 """Create the specified directory."""
222 cr, node, rem = datacr or (None, None, None)
224 raise OSError(1, 'Operation is not permitted.')
227 basename =_to_unicode(basename)
228 cdir = node.create_child_collection(cr, basename)
229 self._log.debug("Created child dir: %r", cdir)
232 self._log.exception('Cannot create dir "%s" at node %s.', basename, repr(node))
233 raise OSError(1, 'Operation is not permitted.')
235 def close_cr(self, data):
240 def get_cr(self, pathname):
241 raise DeprecationWarning()
243 def get_crdata(self, line, mode='file'):
244 """ Get database cursor, node and remainder data, for commands
246 This is the helper function that will prepare the arguments for
247 any of the subsequent commands.
248 It returns a tuple in the form of:
249 @code ( cr, node, rem_path=None )
251 @param line An absolute or relative ftp path, as passed to the cmd.
252 @param mode A word describing the mode of operation, so that this
253 function behaves properly in the different commands.
255 path = self.ftpnorm(line)
256 if self.cwd_node is None:
257 if not os.path.isabs(path):
258 path = os.path.join(self.root, path)
260 if path == '/' and mode in ('list', 'cwd'):
261 return (None, None, None )
263 if path == '..': path = self.cwd + '/..'
264 path = _to_unicode(os.path.normpath(path)) # again, for '/db/../ss'
265 if path == '.': path = ''
267 if os.path.isabs(path) and self.cwd_node is not None \
268 and path.startswith(self.cwd):
269 # make relative, so that cwd_node is used again
270 path = path[len(self.cwd):]
271 if path.startswith('/'):
274 p_parts = path.split(os.sep)
276 assert '..' not in p_parts
279 if mode in ('create',):
280 rem_path = p_parts[-1]
281 p_parts = p_parts[:-1]
283 if os.path.isabs(path):
284 # we have to start from root, again
285 while p_parts and p_parts[0] == '':
286 p_parts = p_parts[1:]
287 # self._log.debug("Path parts: %r ", p_parts)
289 raise IOError(errno.EPERM, 'Cannot perform operation at root directory.')
291 if dbname not in self.db_list():
292 raise IOError(errno.ENOENT,'Invalid database path: %s.' % dbname)
294 db = pooler.get_db(dbname)
296 raise OSError(1, 'Database cannot be used.')
299 uid = security.login(dbname, self.username, self.password)
305 raise OSError(2, 'Authentification required.')
306 n = get_node_context(cr, uid, {})
307 node = n.get_uri(cr, p_parts[1:])
308 return (cr, node, rem_path)
310 # we never reach here if cwd_node is not set
311 if p_parts and p_parts[-1] == '':
312 p_parts = p_parts[:-1]
313 cr, uid = self.get_node_cr_uid(self.cwd_node)
315 node = self.cwd_node.get_uri(cr, p_parts)
318 if node is False and mode not in ('???'):
320 raise IOError(errno.ENOENT, 'Path does not exist.')
321 return (cr, node, rem_path)
323 def get_node_cr_uid(self, node):
324 """ Get cr, uid, pool from a node
327 db = pooler.get_db(node.context.dbname)
328 return db.cursor(), node.context.uid
330 def get_node_cr(self, node):
331 """ Get the cursor for the database of a node
333 The cursor is the only thing that a node will not store
334 persistenly, so we have to obtain a new one for each call.
336 return self.get_node_cr_uid(node)[0]
338 def listdir(self, datacr):
339 """List the content of a directory."""
340 class false_node(object):
349 def __init__(self, db):
352 if datacr[1] is None:
354 for db in self.db_list():
356 result.append(false_node(db))
357 except osv.except_osv:
360 cr, node, rem = datacr
361 res = node.children(cr)
364 def rmdir(self, datacr):
365 """Remove the specified directory."""
366 cr, node, rem = datacr
371 def remove(self, datacr):
373 if datacr[1].type == 'collection':
374 return self.rmdir(datacr)
375 elif datacr[1].type == 'file':
376 return self.rmfile(datacr)
377 raise OSError(1, 'Operation is not permitted.')
379 def rmfile(self, datacr):
380 """Remove the specified file."""
386 def rename(self, src, datacr):
387 """ Renaming operation, the effect depends on the src:
388 * A file: read, create and remove
389 * A directory: change the parent and reassign children to ressource
393 nname = _to_unicode(datacr[2])
394 ret = src.move_to(cr, datacr[1], new_name=nname)
395 # API shouldn't wait for us to write the object
396 assert (ret is True) or (ret is False)
398 except EnvironmentError:
401 self._log.exception('Cannot rename "%s" to "%s" at "%s".', src, datacr[2], datacr[1])
402 raise OSError(1,'Operation is not permitted.')
404 def stat(self, node):
405 raise NotImplementedError()
407 # --- Wrapper methods around os.path.*
410 def isfile(self, node):
411 if node and (node.type in ('file','content')):
416 def islink(self, path):
417 """Return True if path is a symbolic link."""
420 def isdir(self, node):
421 """Return True if path is a directory."""
424 if node and (node.type in ('collection','database')):
428 def getsize(self, datacr):
429 """Return the size of the specified file in bytes."""
430 if not (datacr and datacr[1]):
431 raise IOError(errno.ENOENT, "No such file or directory.")
432 if datacr[1].type in ('file', 'content'):
433 return datacr[1].get_data_len(datacr[0]) or 0L
437 def getmtime(self, datacr):
438 """Return the last modified time as a number of seconds since
442 if node.write_date or node.create_date:
443 dt = (node.write_date or node.create_date)[:19]
444 result = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
446 result = time.mktime(time.localtime())
450 def realpath(self, path):
451 """Return the canonical version of path eliminating any
452 symbolic links encountered in the path (if they are
453 supported by the operating system).
458 def lexists(self, path):
459 """Return True if path refers to an existing path, including
460 a broken or circular symbolic link.
462 raise DeprecationWarning()
463 return path and True or False
467 # Ok, can be improved
468 def glob1(self, dirname, pattern):
469 """Return a list of files matching a dirname pattern
472 Unlike glob.glob1 raises exception if os.listdir() fails.
474 names = self.listdir(dirname)
475 if pattern[0] != '.':
476 names = filter(lambda x: x.path[0] != '.', names)
477 return fnmatch.filter(names, pattern)
479 # --- Listing utilities
481 # note: the following operations are no more blocking
483 def get_list_dir(self, datacr):
484 """"Return an iterator object that yields a directory listing
485 in a form suitable for LIST command.
489 elif self.isdir(datacr[1]):
490 listing = self.listdir(datacr)
491 return self.format_list(datacr[0], datacr[1], listing)
492 # if path is a file or a symlink we return information about it
493 elif self.isfile(datacr[1]):
494 par = datacr[1].parent
495 return self.format_list(datacr[0], par, [datacr[1]])
497 def get_stat_dir(self, rawline, datacr):
498 """Return an iterator object that yields a list of files
499 matching a dirname pattern non-recursively in a form
500 suitable for STAT command.
502 - (str) rawline: the raw string passed by client as command
505 ftppath = self.ftpnorm(rawline)
506 if not glob.has_magic(ftppath):
507 return self.get_list_dir(self.ftp2fs(rawline, datacr))
509 basedir, basename = os.path.split(ftppath)
510 if glob.has_magic(basedir):
511 return iter(['Directory recursion not supported.\r\n'])
513 basedir = self.ftp2fs(basedir, datacr)
514 listing = self.glob1(basedir, basename)
517 return self.format_list(basedir, listing)
519 def format_list(self, cr, parent_node, listing, ignore_err=True):
520 """Return an iterator object that yields the entries of given
521 directory emulating the "/bin/ls -lA" UNIX command output.
523 - (str) basedir: the parent directory node. Can be None
524 - (list) listing: a list of nodes
525 - (bool) ignore_err: when False raise exception if os.lstat()
528 On platforms which do not support the pwd and grp modules (such
529 as Windows), ownership is printed as "owner" and "group" as a
530 default, and number of hard links is always "1". On UNIX
531 systems, the actual owner, group, and number of links are
534 This is how output appears to client:
536 -rw-rw-rw- 1 owner group 7045120 Sep 02 3:47 music.mp3
537 drwxrwxrwx 1 owner group 0 Aug 31 18:50 e-books
538 -rw-rw-rw- 1 owner group 380 Sep 02 3:40 module.py
541 perms = filemode(node.unixperms) # permissions
543 size = node.content_length or 0L
544 uname = _to_decode(node.uuser)
545 gname = _to_decode(node.ugroup)
546 # stat.st_mtime could fail (-1) if last mtime is too old
547 # in which case we return the local time as last mtime
549 st_mtime = node.write_date or 0.0
550 if isinstance(st_mtime, basestring):
551 st_mtime = time.strptime(st_mtime, '%Y-%m-%d %H:%M:%S')
552 elif isinstance(st_mtime, float):
553 st_mtime = time.localtime(st_mtime)
554 mname=_get_month_name(time.strftime("%m", st_mtime ))
555 mtime = mname+' '+time.strftime("%d %H:%M", st_mtime)
557 mname=_get_month_name(time.strftime("%m"))
558 mtime = mname+' '+time.strftime("%d %H:%M")
560 if isinstance(fpath, (list, tuple)):
562 # formatting is matched with proftpd ls output
563 path=_to_decode(fpath)
564 yield "%s %3s %-8s %-8s %8s %s %s\r\n" %(perms, nlinks, uname, gname,
568 def format_mlsx(self, cr, basedir, listing, perms, facts, ignore_err=True):
569 """Return an iterator object that yields the entries of a given
570 directory or of a single file in a form suitable with MLSD and
573 Every entry includes a list of "facts" referring the listed
574 element. See RFC-3659, chapter 7, to see what every single
577 - (str) basedir: the absolute dirname.
578 - (list) listing: the names of the entries in basedir
579 - (str) perms: the string referencing the user permissions.
580 - (str) facts: the list of "facts" to be returned.
581 - (bool) ignore_err: when False raise exception if os.stat()
584 Note that "facts" returned may change depending on the platform
585 and on what user specified by using the OPTS command.
587 This is how output could appear to the client issuing
590 type=file;size=156;perm=r;modify=20071029155301;unique=801cd2; music.mp3
591 type=dir;size=0;perm=el;modify=20071127230206;unique=801e33; ebooks
592 type=file;size=211;perm=r;modify=20071103093626;unique=801e32; module.py
594 permdir = ''.join([x for x in perms if x not in 'arw'])
595 permfile = ''.join([x for x in perms if x not in 'celmp'])
596 if ('w' in perms) or ('a' in perms) or ('f' in perms):
600 type = size = perm = modify = create = unique = mode = uid = gid = ""
607 perm = 'perm=%s;' %permdir
612 perm = 'perm=%s;' %permfile
614 size = 'size=%s;' % (node.content_length or 0L)
615 # last modification time
616 if 'modify' in facts:
618 st_mtime = node.write_date or 0.0
619 if isinstance(st_mtime, basestring):
620 st_mtime = time.strptime(st_mtime, '%Y-%m-%d %H:%M:%S')
621 elif isinstance(st_mtime, float):
622 st_mtime = time.localtime(st_mtime)
623 modify = 'modify=%s;' %time.strftime("%Y%m%d%H%M%S", st_mtime)
625 # stat.st_mtime could fail (-1) if last mtime is too old
627 if 'create' in facts:
628 # on Windows we can provide also the creation time
630 st_ctime = node.create_date or 0.0
631 if isinstance(st_ctime, basestring):
632 st_ctime = time.strptime(st_ctime, '%Y-%m-%d %H:%M:%S')
633 elif isinstance(st_mtime, float):
634 st_ctime = time.localtime(st_ctime)
635 create = 'create=%s;' %time.strftime("%Y%m%d%H%M%S",st_ctime)
639 if 'unix.mode' in facts:
640 mode = 'unix.mode=%s;' %oct(node.unixperms & 0777)
641 if 'unix.uid' in facts:
642 uid = 'unix.uid=%s;' % _to_decode(node.uuser)
643 if 'unix.gid' in facts:
644 gid = 'unix.gid=%s;' % _to_decode(node.ugroup)
645 # We provide unique fact (see RFC-3659, chapter 7.5.2) on
646 # posix platforms only; we get it by mixing st_dev and
647 # st_ino values which should be enough for granting an
648 # uniqueness for the file listed.
649 # The same approach is used by pure-ftpd.
650 # Implementors who want to provide unique fact on other
651 # platforms should use some platform-specific method (e.g.
652 # on Windows NTFS filesystems MTF records could be used).
653 # if 'unique' in facts: todo
654 # unique = "unique=%x%x;" %(st.st_dev, st.st_ino)
656 if isinstance (path, (list, tuple)):
658 path=_to_decode(path)
659 yield "%s%s%s%s%s%s%s%s%s %s\r\n" %(type, size, perm, modify, create,
660 mode, uid, gid, unique, path)
662 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: