Пример #1
0
        ///  @param [in] absolute dirPath -- e.g. "/path/to/dir" </param>
        private SqlDir __getDir(string dirPath)
        {
            if (SqlFsFunc.isNullOrEmpty(dirPath))
            {
                SqlFsErrCode.CurrentError = FsErr.EmptyString;
                return(null);
            }

            if (!dirPath.StartsWith(SqlFsConst.STRPATHSEP))        // must start with '/'
            {
                SqlFsErrCode.CurrentError = FsErr.MustUseAbsolutePath;
                return(null);
            }

            dirPath = SqlFsFunc.Trim(dirPath, new char[] { SqlFsConst.PATHSEP });
            SqlDir rootDir = RootDir;

            if (rootDir == null)
            {
                SqlFsErrCode.CurrentError = FsErr.CannotAccessRoot;
                return(null);
            }

            if (SqlFsFunc.isNullOrEmpty(dirPath))        // if empty after trim, it refers to root
            {
                return(rootDir);
            }

            return(rootDir.getDir(dirPath));
        }
Пример #2
0
        /// <summary>
        ///   Add an empty dir
        /// </summary>
        ///   <returns> the newly created dir </returns>
        private SqlDir __addDir(string dirName)
        {
            if ((dirName = checkInvalidChars(dirName)) == null)
            {
                SqlFsErrCode.CurrentError = FsErr.InvalidChars;
                return(null);
            }

            if (__isAlreadyExist(dirName))
            {
                SqlFsErrCode.CurrentError = FsErr.NameAlreadyExists;
                return(null);
            }

            FsID newID = SqlDir.addDir(db, dirName, this.ID);

            if (newID.compare(SqlFsConst.INVALIDID) <= 0)
            {
                SqlFsErrCode.CurrentError = FsErr.NoNewIDForNewFsNode;
                return(null);
            }

            if (!updateChildList(SqlFsConst.FSOP.ADD, newID, FsID.toFsID(0)))
            {
                // delete entry just created
                SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), newID);
                return(null);
            }

            return(SqlDir.getDir(db, fsLocker, newID));
        }
Пример #3
0
        /// <summary>
        ///  Check if passed in 'dir' is one of its ancestor
        /// </summary>
        private bool __isAncestor(SqlDir dir)
        {
            bool      found   = false;
            SqlFsNode curNode = this;

            do
            {
                curNode = curNode.Parent;
                if (curNode == null)
                {
                    break;
                }

                if (curNode.ID.Equals(dir.ID))
                {
                    found = true;
                    break;
                }

                if (curNode.ID.Equals(SqlFsConst.ROOTDIRID))           // reach root
                {
                    break;
                }
            } while (true);

            return(found);
        }
Пример #4
0
        /// <summary>
        ///  Rename dir/file
        /// </summary>
        private bool __rename(string newName)
        {
            if ((newName = checkInvalidChars(newName)) == null)
            {
                SqlFsErrCode.CurrentError = FsErr.InvalidChars;
                return(false);
            }

            if (this.ID.Equals(SqlFsConst.ROOTDIRID))        // can't rename root
            {
                SqlFsErrCode.CurrentError = FsErr.CannotRenameRoot;
                return(false);
            }

            SqlDir parentDir = this.Parent;

            if (parentDir == null)
            {
                SqlFsErrCode.CurrentError = FsErr.NoParent;
                return(false);
            }

            if (parentDir.isAlreadyExist(newName))        // already exists or not ?
            {
                SqlFsErrCode.CurrentError = FsErr.NameAlreadyExists;
                return(false);
            }

            setField(SqlFs.FSBLOCK.fsName, newName);

            return(true);
        }
Пример #5
0
        /// <summary>
        ///  Test if a absolute path (dir/file) exists
        /// </summary>
        private bool __exists(string path)
        {
            if (SqlFsFunc.isNullOrEmpty(path))
            {
                SqlFsErrCode.CurrentError = FsErr.EmptyString;
                return(false);
            }

            if (!path.StartsWith(SqlFsConst.STRPATHSEP))        // must start with '/'
            {
                SqlFsErrCode.CurrentError = FsErr.MustUseAbsolutePath;
                return(false);
            }

            path = SqlFsFunc.Trim(path, new char[] { SqlFsConst.PATHSEP });
            if (SqlFsFunc.isNullOrEmpty(path))        // if empty after trim, it refers to root
            {
                return(true);
            }

            SqlDir rootDir = RootDir;

            if (rootDir == null)
            {
                SqlFsErrCode.CurrentError = FsErr.CannotAccessRoot;
                return(false);
            }

            SqlFsNode fsNode = rootDir.getFsNode(path);

            return(fsNode != null);
        }
Пример #6
0
        ///  @param [in] absolute filePath -- e.g. "/path/to/file" </param>
        private SqlFile __getFile(string filePath)
        {
            if (SqlFsFunc.isNullOrEmpty(filePath))
            {
                SqlFsErrCode.CurrentError = FsErr.EmptyString;
                return(null);
            }

            if (!filePath.StartsWith(SqlFsConst.STRPATHSEP))        // must start with '/'
            {
                SqlFsErrCode.CurrentError = FsErr.MustUseAbsolutePath;
                return(null);
            }

            filePath = SqlFsFunc.Trim(filePath, new char[] { SqlFsConst.PATHSEP });

            SqlDir rootDir = RootDir;

            if (rootDir == null)
            {
                SqlFsErrCode.CurrentError = FsErr.CannotAccessRoot;
                return(null);
            }

            return(rootDir.getFile(filePath));
        }
Пример #7
0
        public virtual SqlDir addDir(string dirName)
        {
            SqlFsErrCode.CurrentError = FsErr.OK;

            SqlDir childDir = null;

            fsLocker.FsLock;
            SqlFsTransaction fsTran = new SqlFsTransaction(db);

            try
            {
                childDir = __addDir(dirName);
                if (childDir != null)
                {
                    fsTran.fsOpSuccess();
                }
            }
            finally
            {
                fsTran.dispose();
                fsLocker.dispose();
            }

            return(childDir);
        }
Пример #8
0
        public virtual bool move(SqlDir destDir)
        {
            SqlFsErrCode.CurrentError = FsErr.OK;

            bool isOK = false;

            fsLocker.FsLock;
            SqlFsTransaction fsTran = new SqlFsTransaction(db);

            try
            {
                isOK = __move(destDir);
                if (isOK)
                {
                    fsTran.fsOpSuccess();
                }
            }
            finally
            {
                fsTran.dispose();
                fsLocker.dispose();
            }

            return(isOK);
        }
Пример #9
0
        /// <summary>
        ///  Move itself to a destination dir
        /// </summary>
        private bool __move(SqlDir destDir)
        {
            do
            {
                if (this.ID.Equals(SqlFsConst.ROOTDIRID))           // can't move root
                {
                    SqlFsErrCode.CurrentError = FsErr.CannotMoveRoot;
                    break;
                }

                if (this.ID.Equals(destDir.ID))           // can't move to itself
                {
                    SqlFsErrCode.CurrentError = FsErr.CannotMoveToSelf;
                    break;
                }

                if (this.Dir && destDir.isAncestor((SqlDir)this))           // if it is a DIR, can't move to its subdir
                {
                    SqlFsErrCode.CurrentError = FsErr.CannotMoveToSubdir;
                    break;
                }

                if (destDir.isAlreadyExist(this.Name))           // can't move if there is one with the same name
                {
                    SqlFsErrCode.CurrentError = FsErr.NameAlreadyExists;
                    break;
                }

                // unlink itself from parent
                SqlDir parent = this.Parent;
                if (parent == null)
                {
                    SqlFsErrCode.CurrentError = FsErr.NoParent;
                    break;
                }

                if (!parent.updateChildList(SqlFsConst.FSOP.DEL, this.ID, FsID.toFsID(0)))
                {
                    break;
                }

                // update parent ID
                if (!setField(SqlFs.FSBLOCK.fsParent, destDir.ID))
                {
                    break;
                }

                // add itself to dest dir
                if (!destDir.updateChildList(SqlFsConst.FSOP.ADD, this.ID, FsID.toFsID(0)))
                {
                    break;
                }

                return(true);
            } while (false);

            return(false);
        }
Пример #10
0
        /// <summary>
        ///  delete all dirs and files recursively
        /// </summary>
        private bool __delete()
        {
            bool isOK = false;

            do
            {
                // delete itself from parent
                if (!this.ID.Equals(SqlFsConst.ROOTDIRID))           // root has no parent
                {
                    SqlDir parent = this.Parent;
                    if (parent == null)
                    {
                        SqlFsErrCode.CurrentError = FsErr.NoParent;
                        break;
                    }

                    if (!parent.updateChildList(SqlFsConst.FSOP.DEL, this.ID, FsID.toFsID(0)))
                    {
                        break;
                    }
                }

                // delete underlying subdirs and files
                List <SqlFsNode> childList = this.ChildList;

                if (childList != null)
                {
                    // delete children one by one
                    foreach (SqlFsNode fsNode in childList)
                    {
                        fsNode.delete();
                    }
                }

                // delete itself
                if (this.ID.Equals(SqlFsConst.ROOTDIRID))
                {
                    // for root, just clear all children
                    this.setField(SqlFs.FSBLOCK.fsChild, null);
                }
                else
                {
                    if (!SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), this.ID))
                    {
                        SqlFsErrCode.CurrentError = FsErr.CannotDeleteFsEntry;
                        break;
                    }
                }

                isOK = true;
            } while (false);

            return(isOK);
        }
Пример #11
0
        public virtual bool isAncestor(SqlDir dir)
        {
            SqlFsErrCode.CurrentError = FsErr.OK;

            fsLocker.FsLock;
            try
            {
                return(__isAncestor(dir));
            }
            finally
            {
                fsLocker.dispose();
            }
        }
Пример #12
0
        private bool __delete()
        {
            bool isOK = false;

            do
            {
                // delete itself from parent
                SqlDir parent = this.Parent;
                if (parent == null)
                {
                    SqlFsErrCode.CurrentError = FsErr.NoParent;
                    break;
                }

                if (!parent.updateChildList(SqlFsConst.FSOP.DEL, this.ID, FsID.toFsID(0)))
                {
                    break;
                }

                // delete entry in data block table
                FsID dataBlockID = this.getDataBlockID();
                if (dataBlockID.compare(SqlFsConst.INVALIDID) <= 0)
                {
                    //SqlFsLog.debug("+++ dataBlockID = " + dataBlockID.getVal());
                    SqlFsErrCode.CurrentError = FsErr.DataBlockIDNotValid;
                    break;
                }

                if (!SqlFs.deleteEntryByID(db, IFileData.DTABLENAME, IFileData.IDCOL, dataBlockID))
                {
                    SqlFsErrCode.CurrentError = FsErr.CannotDeleteDataBlockEntry;
                    break;
                }

                // delete its own entry
                if (!SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), this.ID))
                {
                    SqlFsErrCode.CurrentError = FsErr.CannotDeleteFsEntry;
                    break;
                }

                isOK = true;
            } while (false);

            return(isOK);
        }
Пример #13
0
        /// <summary>
        ///  Get a fsNode from the result
        /// </summary>
        internal static SqlFsNode getFsNode(SQLiteDatabase db, SqlFsLocker fsLocker, Cursor c)
        {
            SqlFsNode fsNode = null;

            SqlFsConst.FSTYPE type = SqlFsConst.FSTYPE.toFSTYPE(c.getInt(SqlFs.FSBLOCK.fsType.ordinal()));

            if (type == SqlFsConst.FSTYPE.DIR)
            {
                fsNode = SqlDir.getDir(db, fsLocker, c);
            }
            else if (type == SqlFsConst.FSTYPE.FILE)
            {
                fsNode = SqlFile.getFile(db, fsLocker, c);
            }

            return(fsNode);
        }
Пример #14
0
        /// <summary>
        ///  Get list of subdirs only
        /// </summary>
        private List <SqlDir> __getSubDirs()
        {
            List <SqlDir> dirList = null;
            Cursor        c       = getEntryByName(null, SqlFsConst.FSTYPE.DIR);

            if (c != null)
            {
                c.moveToFirst();
                dirList = new List <SqlDir>(c.Count);
                do
                {
                    SqlDir dir = SqlDir.getDir(db, fsLocker, c);
                    if (dir != null)
                    {
                        dirList.Add(dir);
                    }
                } while (c.moveToNext());
                c.close();
            }

            return(dirList);
        }
Пример #15
0
        /// <summary>
        ///  Move itself to a destination path (absolute or relative)
        /// </summary>
        private bool __move(string destPath)
        {
            if (SqlFsFunc.isNullOrEmpty(destPath))
            {
                SqlFsErrCode.CurrentError = FsErr.EmptyString;
                return(false);
            }

            // determine destination dir
            SqlDir destDir = null;

            if (destPath.StartsWith(SqlFsConst.STRPATHSEP))
            {
                // absolute path
                SqlDir rootDir = (SqlDir)SqlFs.getFsNodeByID(db, fsLocker, SqlFsConst.ROOTDIRID);           // get root
                destPath = SqlFsFunc.Trim(destPath, new char[] { SqlFsConst.PATHSEP });
                // if empty after trim, it refers to root
                destDir = SqlFsFunc.isNullOrEmpty(destPath) ? rootDir : rootDir.getDir(destPath);
            }
            else
            {
                // relative path
                SqlDir parent = this.Parent;
                if (parent != null)
                {
                    destDir = parent.getDir(destPath);
                }
            }

            if (destDir != null)
            {
                return(__move(destDir));
            }

            SqlFsErrCode.CurrentError = FsErr.DestDirNotFound;
            return(false);
        }
Пример #16
0
 /// <summary>
 ///  Create root dir entry, which will have an ID of 1 and parent 0
 /// </summary>
 private void createRootDir()
 {
     SqlDir.addDir(db, SqlFsConst.ROOTDIRNAME, SqlFsConst.ROOTPARENTID);
 }
Пример #17
0
        internal static SqlDir getDir(SQLiteDatabase db, SqlFsLocker fsLocker, FsID id)
        {
            SqlDir d = new SqlDir(db, fsLocker, id);

            return(d);
        }
Пример #18
0
        internal static SqlDir getDir(SQLiteDatabase db, SqlFsLocker fsLocker, Cursor c)
        {
            FsID id = SqlFsFunc.getID(c, SqlFs.FSBLOCK.fsID.ordinal());

            return(SqlDir.getDir(db, fsLocker, id));
        }