Пример #1
0
    def mkdir(self, path, mode):
        """
        Create folder in the database.
            path {string}: diretory path
            mode {string}: directory permissions
        """
        try:
            (folder_path, name) = _path_split(path)
            folder_path = _normalize_path(folder_path)
            path = _normalize_path(path)
            now = get_current_date()

            logger.info('create new dir %s' % path)
            folder = dbutils.get_folder(self.db, path)
            if folder is not None:
                logger.info('folder already exists %s' % path)
                return -errno.EEXIST
            else:
                self.db.create({
                    "name": name,
                    "path": folder_path,
                    "docType": "Folder",
                    'creationDate': now,
                    'lastModification': now,
                })

                self._update_parent_folder(folder_path)
                return 0

        except Exception as e:
            logger.exception(e)
            return -errno.EEXIST
Пример #2
0
    def get_st(self, path):
        self.receive()
        if path in self.cache['st']:
            logger.info('st : cache')
            return self.cache['st'][path]
        else:
            try:
                st = CouchStat()

                # Path is root
                if path is "/":
                    st.st_mode = stat.S_IFDIR | 0o775
                    st.st_nlink = 2
                    self.cache['st'][path] = st
                    return st

                else:
                    # Or path is a folder
                    folder = dbutils.get_folder(self.db, path)
                    if folder is not None:
                        st.st_mode = stat.S_IFDIR | 0o775
                        st.st_nlink = 2
                        if 'lastModification' in folder:
                            st.st_atime = get_date(folder['lastModification'])
                            st.st_ctime = st.st_atime
                            st.st_mtime = st.st_atime
                        self.cache['st'][path] = st
                        return st

                    else:
                        # Or path is a file
                        file_doc = dbutils.get_file(self.db, path)

                        if file_doc is not None:
                            st.st_mode = stat.S_IFREG | 0o664
                            st.st_nlink = 1
                            # TODO: if size is not set, get the binary
                            # and save the information.
                            st.st_size = file_doc.get('size', 4096)
                            if 'lastModification' in file_doc:
                                st.st_atime = \
                                    get_date(file_doc['lastModification'])
                                st.st_ctime = st.st_atime
                                st.st_mtime = st.st_atime
                            self.cache['st'][path] = st
                            return st

                        else:
                            print 'File does not exist: %s' % path
                            logger.info('file_not_fount')
                            return st
                self.send()

            except Exception as e:
                logger.exception(e)
                return e
Пример #3
0
    def getattr(self, path):
        """
        Return file descriptor for given_path. Useful for 'ls -la' command like.
        """
        try:
            logger.debug('getattr %s' % path)

            st = CouchStat()

            # Path is root
            if path is "/":
                st.st_mode = stat.S_IFDIR | 0o775
                st.st_nlink = 2
                return st

            else:
                # Or path is a folder
                folder = dbutils.get_folder(self.db, path)

                if folder is not None:
                    st.st_mode = stat.S_IFDIR | 0o775
                    st.st_nlink = 2
                    if 'lastModification' in folder:
                        st.st_atime = get_date(folder['lastModification'])
                        st.st_ctime = st.st_atime
                        st.st_mtime = st.st_atime
                    return st

                else:
                    # Or path is a file
                    file_doc = dbutils.get_file(self.db, path)

                    if file_doc is not None:
                        st.st_mode = stat.S_IFREG | 0o664
                        st.st_nlink = 1
                        # TODO: if size is not set, get the binary
                        # and save the information.
                        st.st_size = file_doc.get('size', 4096)
                        if 'lastModification' in file_doc:
                            st.st_atime = \
                                get_date(file_doc['lastModification'])
                            st.st_ctime = st.st_atime
                            st.st_mtime = st.st_atime
                        return st

                    else:
                        print 'File does not exist: %s' % path
                        return -errno.ENOENT
                        return st

        except Exception as e:
            logger.exception(e)
            return -errno.ENOENT
Пример #4
0
    def _update_parent_folder(self, parent_folder):
        """
        Update parent folder
            parent_folder {string}: parent folder path

        When a file or a folder is renamed/created/removed, last modification date
        of parent folder should be updated

        """
        folder = dbutils.get_folder(self.db, parent_folder)
        if folder is not None:
            folder['lastModification'] = get_current_date()
            self.db.save(folder)
Пример #5
0
    def _get_attr_from_db(self, path, isfile=None):
        '''
        Build fuse file attribute from data located in database. Check if path
        corresponds to a folder first.
        '''
        st = CouchStat()
        path = fusepath.normalize_path(path)

        if isfile is None:
            folder = dbutils.get_folder(self.db, path)
            if folder is not None:
                st.set_folder(folder)
                self.attr_cache.add(path, st)
                return st
            else:
                file_doc = dbutils.get_file(self.db, path)
                if file_doc is not None:
                    st.set_file(file_doc)
                    self.attr_cache.add(path, st)
                    return st
                else:
                    return None
        elif isfile:
            file_doc = dbutils.get_file(self.db, path)
            if file_doc is not None:
                st.set_file(file_doc)
                self.attr_cache.add(path, st)
                return st
            else:
                return None
        else:
            folder = dbutils.get_folder(self.db, path)
            if folder is not None:
                st.set_folder(folder)
                self.attr_cache.add(path, st)
                return st
            else:
                return None
Пример #6
0
    def rmdir(self, path):
        """
        Delete folder from database.
            path {string}: diretory path
        """
        try:
            path = _normalize_path(path)
            folder = dbutils.get_folder(self.db, path)
            self.db.delete(self.db[folder['_id']])
            return 0

        except Exception as e:
            logger.exception(e)
            return -errno.ENOENT
Пример #7
0
    def rmdir(self, path):
        """
        Delete folder from database and clean caches accordingly.
            path {string}: folder path
        """
        logger.info('rmdir %s' % path)
        try:
            path = fusepath.normalize_path(path)
            folder = dbutils.get_folder(self.db, path)
            dbutils.delete_folder(self.db, folder)
            self._clean_cache(path)
            return 0

        except Exception as e:
            logger.exception(e)
            return -errno.ENOENT
Пример #8
0
    def rmdir(self, path):
        """
        Delete folder from database.
            path {string}: diretory path
        """
        try:
            path = _normalize_path(path)
            logger.info('rmdir %s' % path)
            folder = dbutils.get_folder(self.db, path)
            self.db.delete(self.db[folder['_id']])

            self.dirs.pop(path, None)
            self.descriptors.pop(path, None)
            return 0

        except Exception, e:
            logger.exception(e)
            return -errno.ENOENT
Пример #9
0
    def mkdir(self, path, mode):
        """
        Create folder in current FS add a folder in the database and update
        cache accordingly.
            path {string}: diretory path
            mode {string}: directory permissions
        """
        logger.info('mkdir %s' % path)
        try:
            path = fusepath.normalize_path(path)
            parent_path, name = fusepath.split(path)

            now = fusepath.get_current_date()
            folder = dbutils.get_folder(self.db, path)

            # Check folder existence.
            if folder is not None:
                logger.info('folder already exists %s' % path)
                return -errno.EEXIST

            # Create folder.
            else:
                logger.info('folder creation... %s %s' % (parent_path, name))
                folder = dbutils.create_folder(self.db, {
                    "name": name,
                    "path": parent_path,
                    "docType": "Folder",
                    'creationDate': now,
                    'lastModification': now,
                })

                self._update_parent_folder(parent_path)
                self._add_to_cache(path)

                return 0

        except Exception as e:
            logger.exception(e)
            return -errno.EEXIST
Пример #10
0
    def rename(self, pathfrom, pathto, root=True):
        """
        Rename file and subfiles (if it's a folder) in device.
        """
        logger.info("rename %s -> %s: " % (pathfrom, pathto))
        try:
            pathfrom = fusepath.normalize_path(pathfrom)
            pathto = fusepath.normalize_path(pathto)

            file_doc = dbutils.get_file(self.db, pathfrom)
            if file_doc is not None:
                file_path, name = fusepath.split(pathto)

                file_doc.update({
                    "name": name,
                    "path": file_path,
                    "lastModification": fusepath.get_current_date()
                })
                dbutils.update_file(self.db, file_doc)

            folder_doc = dbutils.get_folder(self.db, pathfrom)
            if folder_doc is not None:
                folder_path, name = fusepath.split(pathto)
                folder_doc.update({
                    "name": name,
                    "path": folder_path,
                    "lastModification": fusepath.get_current_date()
                })

                # Rename all subfiles
                for res in self.db.view("file/byFolder", key=pathfrom):
                    child_pathfrom = os.path.join(
                        res.value['path'],
                        res.value['name']
                    )
                    child_pathto = os.path.join(folder_path, name, res.value['name'])
                    self.rename(child_pathfrom, child_pathto, False)

                for res in self.db.view("folder/byFolder", key=pathfrom):
                    child_pathfrom = os.path.join(
                        res.value['path'],
                        res.value['name'])
                    child_pathto = os.path.join(folder_path, name, res.value['name'])
                    self.rename(child_pathfrom, child_pathto, False)

                dbutils.update_folder(self.db, folder_doc)

            parent_path_from, namefrom = fusepath.split(pathfrom)
            parent_path_to, nameto = fusepath.split(pathto)

            if root:
                self._update_parent_folder(parent_path_from)
                self._update_parent_folder(parent_path_to)

            names = dbutils.name_cache.get(parent_path_from)
            if names is not None and namefrom in names:
                names.remove(namefrom)
                names.add(parent_path_from, names)

            names = dbutils.name_cache.get(parent_path_to)
            if names is not None:
                names.append(nameto)
                names.add(parent_path_to, names)

            self._clean_cache(pathfrom)
            self._add_to_cache(pathto)

            if folder_doc is None and file_doc is None:
                return -errno.ENOENT
            else:
                return 0

        except Exception as e:
            logger.exception(e)
            return -errno.ENOENT