Example #1
0
        /// <summary>
        ///  Create a new entry in FsBlock
        /// </summary>
        ///  <returns> new ID for the inserted node </returns>
        internal static FsID addFsNode(SQLiteDatabase db, SqlFsConst.FSTYPE type, string dirName, FsID parentID)
        {
            long          curTime       = SqlFsFunc.calToFileTime(new DateTime());
            List <object> colsAndValues = new List <object>(10);

            colsAndValues.Add(SqlFs.FSBLOCK.fsCreateTime.ToString());
            colsAndValues.Add(curTime);
            colsAndValues.Add(SqlFs.FSBLOCK.fsLastModTime.ToString());
            colsAndValues.Add(curTime);
            colsAndValues.Add(SqlFs.FSBLOCK.fsFileSize.ToString());
            colsAndValues.Add(0);
            colsAndValues.Add(SqlFs.FSBLOCK.fsType.ToString());
            colsAndValues.Add(type.v());
            colsAndValues.Add(SqlFs.FSBLOCK.fsName.ToString());
            colsAndValues.Add(dirName);
            colsAndValues.Add(SqlFs.FSBLOCK.fsParent.ToString());
            colsAndValues.Add(parentID);

            ContentValues contValues = SqlStr.genContentValues(colsAndValues);

            try
            {
                db.insert(SqlFs.DBNAMES.FsBlock.ToString(), null, contValues);
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.AddFsNodeError;
                return(SqlFsConst.INVALIDID);
            }

            // retrieve the ID of the new entry
            return(SqlFs.getLastInsertID(db));
        }
Example #2
0
        //////////////////////////FS operations ///////////////////////////////////

        ///  @param [in] id -- get entry using ID directly </param>
        internal static SqlFsNode getFsNodeByID(SQLiteDatabase db, SqlFsLocker fsLocker, FsID id)
        {
            SqlFsNode fsNode = null;

            string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", id));
            Cursor c      = null;

            try
            {
                c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { SqlFs.FSBLOCK.fsID.ToString(), SqlFs.FSBLOCK.fsType.ToString() }, @where, null, null, null, null);

                if (c.moveToFirst())
                {
                    fsNode = SqlFsNode.getFsNode(db, fsLocker, c);
                }
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.GetFieldError;
            }
            finally
            {
                SqlFsFunc.close(c);
            }

            return(fsNode);
        }
Example #3
0
        /// <summary>
        ///  Get the last (autoIncrement) rowID after a 'INSERT'
        /// </summary>
        internal static FsID getLastInsertID(SQLiteDatabase db)
        {
            // retrieve the ID of the new entry
            FsID   newID = SqlFsConst.INVALIDID;
            string sql   = "SELECT last_insert_rowid() as [id]";
            Cursor c     = null;

            try
            {
                c = db.rawQuery(sql, null);
                if (c.moveToFirst())
                {
                    newID = SqlFsFunc.getID(c, 0);
                }
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.GetLastInsertIDError;
            }
            finally
            {
                SqlFsFunc.close(c);
            }

            return(newID);
        }
Example #4
0
        /// <summary>
        ///  Write a single info
        ///  Update existing entry or add a new one if not present
        /// </summary>
        private void __writeInfo(string infoName, string infoVal)
        {
            List <object> colsAndValues = new List <object>();

            colsAndValues.Add(FSINFO.infoName.ToString());
            colsAndValues.Add(infoName);
            colsAndValues.Add(FSINFO.infoVal.ToString());
            colsAndValues.Add(infoVal);
            ContentValues contVals = SqlStr.genContentValues(colsAndValues);
            string        @where   = SqlStr.genWhere(new SqlStr.SqlSimpCond(FSINFO.infoName.ToString(), "=", infoName));

            try
            {
                // try update first
                if (db.update(DBNAMES.FsInfo.ToString(), contVals, @where, null) == 0)
                {
                    // nothing is updated, insert to table
                    db.insert(DBNAMES.FsInfo.ToString(), null, contVals);
                }
            }
            catch (SQLiteException e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.WriteFsInfoErr;
                throw e;
            }
        }
Example #5
0
        /// <summary>
        ///  Get a single info
        /// </summary>
        ///  <returns> info value </returns>
        private string __getInfo(string infoName)
        {
            string value  = null;
            string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(FSINFO.infoName.ToString(), "=", infoName));

            Cursor c = null;

            try
            {
                c = db.query(DBNAMES.FsInfo.ToString(), new string[] { FSINFO.infoVal.ToString() }, @where, null, null, null, null);

                if (c != null && c.moveToFirst())
                {
                    value = c.getString(0);
                }
            }
            catch (SQLiteException e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.GetFsInfoErr;
            }
            finally
            {
                SqlFsFunc.close(c);
            }

            return(value);
        }
Example #6
0
        public static SqlFs create(string dbPath, IFileData dummyInst, Context ctxt, AtomicBoolean isNewTableCreated)
        {
            SqlFsErrCode.CurrentError = FsErr.OK;

            SqlFs fs = null;

            try
            {
                if (dbPath != null)
                {
                    fs = new SqlFs(ctxt, dbPath);
                    if (fs != null)
                    {
                        fs.fsLocker.FsLock;
                        try
                        {
                            fs.open();
                            fs.prepare(dummyInst, isNewTableCreated);
                        }
                        finally
                        {
                            fs.fsLocker.dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                SqlFsLog.debug("ERROR: Cannot open database, " + e.Message);
                SqlFsErrCode.CurrentError = FsErr.CannotOpenDB;
            }

            return(fs);
        }
Example #7
0
        /// <summary>
        ///  Generate a hash from a source string
        /// </summary>
        internal static string genHash(string strSrc)
        {
            string strHash = "";

            try
            {
                MessageDigest md    = MessageDigest.getInstance("SHA-1");
                sbyte[]       inBuf = strSrc.GetBytes("UTF-8");
                md.update(inBuf);
                sbyte[] finalVal = md.digest();

                // convert the hash value to string
                StringBuilder sb = new StringBuilder(finalVal.Length * 2);
                for (int i = 0; i < finalVal.Length; ++i)
                {
                    sb.Append(hex2Ascii(((int)finalVal[i] >> 4) & 0x0f));
                    sb.Append(hex2Ascii((int)finalVal[i] & 0x0f));
                }

                strHash = sb.ToString();
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
            }

            return(strHash);
        }
Example #8
0
        ///  @param [in] name -- if null, get all entry </param>
        ///  @param [in] type -- dir, file or any ? </param>
        private Cursor getEntryByName(string name, SqlFsConst.FSTYPE type)
        {
            // conditions
            List <object> conds = new List <object>(10);

            conds.Add(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsParent.ToString(), "=", this.ID));

            if (name != null)
            {
                conds.Add("and");
                conds.Add(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsName.ToString(), "=", name));
            }

            if (type != SqlFsConst.FSTYPE.ANY)
            {
                conds.Add("and");
                conds.Add(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsType.ToString(), "=", type.v()));
            }

            string @where = SqlStr.genWhere(conds);

            // query from DB
            Cursor c = null;

            try
            {
                c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { SqlFs.FSBLOCK.fsID.ToString(), SqlFs.FSBLOCK.fsType.ToString() }, @where, null, null, null, null);
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = SqlFsErrCode.FsErr.NoEntryByName;
            }

            // close and set to null if no rows at all
            if (c != null && c.Count == 0)
            {
                c.close();
                c = null;
            }

            return(c);
        }
Example #9
0
        /// <summary>
        ///  Save field to DB using ID
        /// </summary>
        private bool __setField(SqlFs.FSBLOCK field, object val)
        {
            List <object> colsAndValues = new List <object>(4);

            switch (field)
            {
            case com.sss.sqlfs.SqlFs.FSBLOCK.fsFileSize:
            case com.sss.sqlfs.SqlFs.FSBLOCK.fsType:
            case com.sss.sqlfs.SqlFs.FSBLOCK.fsName:
            case com.sss.sqlfs.SqlFs.FSBLOCK.fsParent:
                colsAndValues.Add(field.ToString());
                colsAndValues.Add(val);
                break;

            case com.sss.sqlfs.SqlFs.FSBLOCK.fsChild:
                sbyte[] blob = idList2Blob((List <FsID>)val);
                colsAndValues.Add(field.ToString());
                colsAndValues.Add(blob);
                break;
            }

            // update last mod time as well
            colsAndValues.Add(SqlFs.FSBLOCK.fsLastModTime.ToString());
            colsAndValues.Add(SqlFsFunc.calToFileTime(new DateTime()));
            ContentValues contValues = SqlStr.genContentValues(colsAndValues);

            string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", this.ID));

            int rowAffected = 0;

            try
            {
                rowAffected = db.update(SqlFs.DBNAMES.FsBlock.ToString(), contValues, @where, null);
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.SetFieldError;
            }

            return(rowAffected > 0);
        }
Example #10
0
        /// <summary>
        ///  Delete entry in a table using ID
        /// </summary>
        ///  @param [in] tableName -- table name </param>
        ///  @param [in] idColName -- ID column name </param>
        ///  @param [in] id -- the actual ID </param>
        internal static bool deleteEntryByID(SQLiteDatabase db, string tableName, string idColName, FsID id)
        {
            string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(idColName, "=", id));

            bool isOK = false;

            try
            {
                if (db.delete(tableName, @where, null) > 0)
                {
                    isOK = true;
                }
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.DeleteFsEntryError;
            }

            return(isOK);
        }
Example #11
0
        /// <summary>
        ///  Get a field from DB using ID
        /// </summary>
        private object __getField(SqlFs.FSBLOCK field)
        {
            string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", this.ID));

            // little adjustment
            SqlFs.FSBLOCK tField = (field == SqlFs.FSBLOCK.fsChildCount) ? SqlFs.FSBLOCK.fsChild : field;
            Cursor        c      = null;
            object        val    = null;

            try
            {
                c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { tField.ToString() }, @where, null, null, null, null);

                if (c.moveToFirst() && !c.isNull(0))
                {
                    switch (field)
                    {
                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsCreateTime:
                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsLastModTime:
                        val = c.getLong(0);
                        break;

                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsFileSize:
                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsType:
                        val = c.getInt(0);
                        break;

                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsName:
                        val = c.getString(0);
                        break;

                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsParent:
                        val = SqlFsFunc.getID(c, 0);
                        break;

                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsChild:
                    {
                        sbyte[] buf = c.getBlob(0);
                        val = blob2idList(buf);
                    }
                    break;

                    case com.sss.sqlfs.SqlFs.FSBLOCK.fsChildCount:
                    {
                        sbyte[] buf = c.getBlob(0);
                        val = buf.Length / FsID.IDSize;
                    }
                    break;
                    }
                }
                else
                {
                    val = __getDefaultValue(field);
                }
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
                SqlFsErrCode.CurrentError = FsErr.GetFieldError;
                val = __getDefaultValue(field);                 // return a default value here
            }
            finally
            {
                SqlFsFunc.close(c);
            }

            return(val);
        }