Esempio n. 1
0
        /// <summary>
        ///  Check if a dir/file name contains invalid character
        /// </summary>
        ///  @param [in] name -- contains dir/file name to be checked
        ///                      on return, it contains a whitespace-trimmed version
        /// </param>
        ///  <returns> null -- contains invalid characters </returns>
        ///  <returns> String -- may be modified name </returns>
        protected internal virtual string checkInvalidChars(string name)
        {
            if (SqlFsFunc.isNullOrEmpty(name))
            {
                return(null);
            }

            string trimmedStr = SqlFsFunc.Trim(name, SqlFsConst.CHARSTOTRIM);

            if (SqlFsFunc.isNullOrEmpty(trimmedStr))        // check once again
            {
                return(null);
            }

            if (SqlFsFunc.indexOfAny(trimmedStr, SqlFsConst.INVALIDCHARS) >= 0)
            {
                return(null);
            }

            if (trimmedStr.Equals(SqlFsConst.CURDIR))        // can't be '.'
            {
                return(null);
            }

            if (trimmedStr.Equals(SqlFsConst.PARENTDIR))        // can't be '..'
            {
                return(null);
            }

            return(trimmedStr);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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));
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
        ///  @param [in] relative 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) || filePath.EndsWith(SqlFsConst.STRPATHSEP))        // must *NOT* start or end with '/'
            {
                SqlFsErrCode.CurrentError = FsErr.MustNotStartOrEndWithPathSeparator;
                return(null);
            }

            string[] dirSeg = filePath.Split(SqlFsConst.STRPATHSEP, true);
            if (dirSeg == null || dirSeg.Length <= 0)
            {
                SqlFsErrCode.CurrentError = FsErr.SplitPathErr;
                return(null);
            }

            SqlFsNode curNode = this;

            // start looping to target file
            for (int i = 0; i < dirSeg.Length; ++i)
            {
                if (SqlFsFunc.isNullOrEmpty(dirSeg[i]))           // to prevent empty space between separator
                {
                    continue;
                }

                curNode = ((SqlDir)curNode).__getChild(dirSeg[i]);
                if (i == dirSeg.Length - 1)
                {
                    if (curNode == null || curNode.Dir)              // last one must be a file
                    {
                        curNode = null;
                        SqlFsErrCode.CurrentError = FsErr.ChildNotFound;
                        break;
                    }
                }
                else
                {
                    if (curNode == null || !curNode.Dir)              // others' should be dir
                    {
                        curNode = null;
                        SqlFsErrCode.CurrentError = FsErr.NotDirInPath;
                        break;
                    }
                }
            }

            return((SqlFile)curNode);
        }
Esempio n. 6
0
        ///  @param [in] relative dirPath -- e.g. "path/to/dir" </param>
        private SqlFsNode __getFsNode(string path)
        {
            if (SqlFsFunc.isNullOrEmpty(path))
            {
                SqlFsErrCode.CurrentError = FsErr.EmptyString;
                return(null);
            }

            if (path.StartsWith(SqlFsConst.STRPATHSEP))        // must *NOT* start with '/'
            {
                SqlFsErrCode.CurrentError = FsErr.MustUseRelativePath;
                return(null);
            }

            if (path.EndsWith(SqlFsConst.STRPATHSEP))        // trim trailing '/'
            {
                path = SqlFsFunc.trimEnd(path, new char[] { SqlFsConst.PATHSEP });
            }

            string[] pathSeg = path.Split(SqlFsConst.STRPATHSEP, true);
            if (pathSeg == null || pathSeg.Length <= 0)
            {
                SqlFsErrCode.CurrentError = FsErr.SplitPathErr;
                return(null);
            }

            SqlFsNode curNode = this;

            // start looping to target node
            for (int i = 0; i < pathSeg.Length; ++i)
            {
                if (SqlFsFunc.isNullOrEmpty(pathSeg[i]))           // to prevent empty space between separator
                {
                    continue;
                }

                curNode = ((SqlDir)curNode).__getChild(pathSeg[i]);
                if (curNode == null)
                {
                    SqlFsErrCode.CurrentError = FsErr.ChildNotFound;
                    break;
                }

                if (!curNode.Dir && i != pathSeg.Length - 1)           // if a file but not reach the end yet
                {
                    curNode = null;
                    SqlFsErrCode.CurrentError = FsErr.NotDirInPath;
                    break;
                }
            }

            return(curNode);
        }
Esempio n. 7
0
        ///  @param [in] relative 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 *NOT* start with '/'
            {
                SqlFsErrCode.CurrentError = FsErr.MustUseRelativePath;
                return(null);
            }

            if (dirPath.EndsWith(SqlFsConst.STRPATHSEP))        // trim trailing '/'
            {
                dirPath = SqlFsFunc.trimEnd(dirPath, new char[] { SqlFsConst.PATHSEP });
            }

            string[] dirSeg = dirPath.Split(SqlFsConst.STRPATHSEP, true);
            if (dirSeg == null || dirSeg.Length <= 0)
            {
                SqlFsErrCode.CurrentError = FsErr.SplitPathErr;
                return(null);
            }

            SqlFsNode curNode = this;

            // start looping to target dir
            foreach (string dir in dirSeg)
            {
                if (SqlFsFunc.isNullOrEmpty(dir))           // to prevent empty space between separator
                {
                    continue;
                }

                curNode = ((SqlDir)curNode).__getChild(dir);
                if (curNode == null || !curNode.Dir)
                {
                    curNode = null;
                    SqlFsErrCode.CurrentError = FsErr.ChildNotFound;
                    break;
                }
            }


            return((SqlDir)curNode);
        }
Esempio n. 8
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);
        }