Exemplo n.º 1
0
        private static LocalDirectory CreateDirectory(LocalDirectory parent, string name, string local_path)
        {
            string fullPath;
            int    last_id = -1;

            if (String.IsNullOrEmpty(local_path))
            {
                throw new ArgumentNullException("local_path");
            }

            if (!System.IO.Directory.Exists(local_path))
            {
                throw new ArgumentException("local_path", String.Format("Directory does not exist: '{0}'", local_path));
            }

            if (parent != null)
            {
                fullPath = PathUtil.Join(parent.FullPath, name);
            }
            else
            {
                fullPath = PathUtil.Join("/", name);
            }

            if (fullPath.Length > 1 && fullPath.EndsWith("/"))
            {
                fullPath = fullPath.Substring(0, fullPath.Length - 1);
            }

            Core.FileSystem.UseConnection(delegate(IDbConnection connection) {
                IDbCommand cmd  = connection.CreateCommand();
                cmd.CommandText = @"INSERT INTO directoryitems (type, parent_id, name, local_path, full_path)
					VALUES ('D', @parent_id, @name, @local_path, @full_path);"                    ;
                Core.FileSystem.AddParameter(cmd, "@name", name);
                Core.FileSystem.AddParameter(cmd, "@local_path", local_path);
                Core.FileSystem.AddParameter(cmd, "@full_path", fullPath);

                if (parent == null)
                {
                    Core.FileSystem.AddParameter(cmd, "@parent_id", null);
                }
                else
                {
                    Core.FileSystem.AddParameter(cmd, "@parent_id", (int)parent.Id);
                }
                Core.FileSystem.ExecuteNonQuery(cmd);

                cmd             = connection.CreateCommand();
                cmd.CommandText = "SELECT last_insert_rowid()";

                last_id = Convert.ToInt32(Core.FileSystem.ExecuteScalar(cmd));
            }, true);

            if (parent != null)
            {
                parent.InvalidateCache();
            }

            int parentId = (parent == null) ? -1 : parent.Id;

            return(new LocalDirectory(last_id, parentId, name, local_path, fullPath));
        }