Exemplo n.º 1
0
        public async Task <IFileSystemInfo> GetByNameAsync(SqlItem parentItem, string name)
        {
            if (parentItem == null)
            {
                throw new ArgumentNullException(nameof(parentItem));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(await GetSqlItemAsync(parentItem.Id, name).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        private async Task <SqlItem> CreateSqlItemAsync(SqlItem parentItem, string name, CreateOptions options = null)
        {
            if (parentItem == null)
            {
                throw new ArgumentNullException(nameof(parentItem));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            Log("Parent: " + parentItem.Id + " '" + parentItem.Name + "' name: '" + name + "'");
            options ??= new CreateOptions();

            // if a race condition occurred, it may already exists
            var item = await GetSqlItemAsync(parentItem.Id, name).ConfigureAwait(false);

            if (item == null)
            {
                var id         = Guid.NewGuid();
                var parameters = new
                {
                    id,
                    parentId = parentItem.Id,
                    name,
                    attributes = options.Attributes,
                    now        = DateTime.UtcNow.RemoveMilliseconds()
                };

                await SqlExtensions.ExecuteNonQueryAsync(ConnectionString, "INSERT INTO Item (Id, ParentId, Name, LastAccessTimeUtc, CreationTimeUtc, LastWriteTimeUtc, Attributes) VALUES (@id, @parentId, @name, @now, @now, @now, @attributes)", parameters, Logger).ConfigureAwait(false);

                item = await GetSqlItemAsync(id).ConfigureAwait(false);
            }

            if (!item.IsFolder && options.InputStream != null)
            {
                await WriteAsync(item, options.InputStream).ConfigureAwait(false);

                // refresh
                item = await GetSqlItemAsync(item.Id).ConfigureAwait(false);
            }

            if (!IsTempFile(name))
            {
                AddEvent(item.Id, item.ParentId, WatcherChangeTypes.Created);
                AddEvent(item.ParentId, (item.Parent?.ParentId).GetValueOrDefault(), WatcherChangeTypes.Changed);
            }
            return(item);
        }
Exemplo n.º 3
0
        public async Task <bool> DeleteAsync(SqlItem item, DeleteOptions options = null)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Id == Guid.Empty)
            {
                throw new UnauthorizedAccessException();
            }

            Log("Item : " + item.Id + " '" + item.Name);

            string sql;

            if (!item.IsFolder)
            {
                sql = "DELETE FROM Item WHERE Id = @id";
            }
            else // deleting a folder is always recursive
            {
                sql = @"
WITH ItemHierarchy (Id)  
AS  
(  
	SELECT Id FROM Item WHERE Id = @id
	UNION ALL
	SELECT i.Id FROM Item i INNER JOIN ItemHierarchy h ON i.ParentId = h.Id AND i.ParentId <> '00000000-0000-0000-0000-000000000000'
)  
DELETE Item FROM ItemHierarchy JOIN Item ON Item.Id = ItemHierarchy.Id";
            }

            var parent  = item.Parent; // get parent before delete
            var deleted = await SqlExtensions.ExecuteNonQueryAsync(ConnectionString, sql, new { id = item.Id }, Logger).ConfigureAwait(false) != 0;

            if (deleted)
            {
                if (!IsTempFile(item.Name))
                {
                    AddEvent(item.Id, item.ParentId, WatcherChangeTypes.Deleted);
                    if (parent != null)
                    {
                        AddEvent(parent.Id, parent.ParentId, WatcherChangeTypes.Changed);
                    }
                }
            }
            return(deleted);
        }
Exemplo n.º 4
0
        private async Task <SqlItem> CopyToAsync(SqlItem item, Guid newParentId)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var parentItem = await GetSqlItemAsync(newParentId).ConfigureAwait(false);

            if (parentItem == null)
            {
                throw new InvalidOperationException();
            }

            return(await CopyToAsync(item, parentItem).ConfigureAwait(false));
        }
Exemplo n.º 5
0
        public async IAsyncEnumerable <SqlItem> EnumerateAsync(SqlItem parentItem, EnumerateOptions options = null)
        {
            options ??= new EnumerateOptions();
            string and;

            if (options.IncludeFiles)
            {
                if (options.IncludeFolders)
                {
                    and = null;
                }
                else
                {
                    and = " AND (Attributes & " + (int)FileAttributes.Directory + ") = 0";
                }
            }
            else
            {
                if (options.IncludeFolders)
                {
                    and = " AND (Attributes & " + (int)FileAttributes.Directory + ") <> 0";
                }
                else
                {
                    yield break;
                }
            }

            if (!options.IncludeHidden)
            {
                and += " AND (Attributes & " + (int)FileAttributes.Hidden + ") = 0";
            }

            using (var reader = await SqlExtensions.ExecuteReaderAsync(ConnectionString, "SELECT Id, ParentId, Name, LastAccessTimeUtc, CreationTimeUtc, LastWriteTimeUtc, Attributes, DATALENGTH(Data) AS Length FROM Item WHERE ParentId = @id AND Id <> '00000000-0000-0000-0000-000000000000'" + and, new { id = parentItem.Id }, Logger).ConfigureAwait(false))
            {
                while (reader.Read())
                {
                    yield return(NewItem(reader));
                }
            }
        }
Exemplo n.º 6
0
        private SqlItem NewItem(SqlDataReader reader)
        {
            var item = new SqlItem();

            item.System   = this;
            item.Id       = (Guid)reader["Id"];
            item.ParentId = (Guid)reader["ParentId"];
            item.Name     = (string)reader["Name"];

            item.LastAccessTimeUtc = DateTime.SpecifyKind((DateTime)reader["LastAccessTimeUtc"], DateTimeKind.Utc);
            item.CreationTimeUtc   = DateTime.SpecifyKind((DateTime)reader["CreationTimeUtc"], DateTimeKind.Utc);
            item.LastWriteTimeUtc  = DateTime.SpecifyKind((DateTime)reader["LastWriteTimeUtc"], DateTimeKind.Utc);
            item.Attributes        = (FileAttributes)reader["Attributes"];

            var len = reader["Length"];

            if (!Convert.IsDBNull(len))
            {
                item.Length = (long)len;
            }
            return(item);
        }
Exemplo n.º 7
0
        public async Task WriteAsync(SqlItem item, Stream stream)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Id == Guid.Empty)
            {
                throw new UnauthorizedAccessException();
            }

            Log("Item : " + item.Id + " '" + item.Name + " stream: " + stream);

            if (stream == null)
            {
                await SqlExtensions.ExecuteNonQueryAsync(ConnectionString, "UPDATE Item SET Data = NULL WHERE Id = @id", new { id = item.Id }, Logger).ConfigureAwait(false);
            }
            else
            {
                await SqlExtensions.ExecuteNonQueryAsync(ConnectionString, "UPDATE Item SET Data = @data WHERE Id = @id", new { id = item.Id, data = stream }, Logger).ConfigureAwait(false);
            }
            AddEvent(item.Id, item.ParentId, WatcherChangeTypes.Changed);
        }
Exemplo n.º 8
0
        public async Task <IFileSystemInfo> MoveToAsync(SqlItem item, Guid newParentId, MoveOptions options = null)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Id == Guid.Empty)
            {
                throw new UnauthorizedAccessException();
            }

            options ??= new MoveOptions();
            if (options.Copy)
            {
                return(await CopyToAsync(item, newParentId).ConfigureAwait(false));
            }

            Log("New Parent: " + newParentId + " item: " + item.Id + " '" + item.Name + "'");

            var oldParent = item.Parent;
            var sql       = "UPDATE Item SET ParentId = @parentId WHERE Id = @id";
            await SqlExtensions.ExecuteNonQueryAsync(ConnectionString, sql, new { id = item.Id, parentId = newParentId }, Logger).ConfigureAwait(false);

            var newItem = await GetItemAsync(item.Id).ConfigureAwait(false);

            if (!IsTempFile(item.Name))
            {
                if (oldParent != null)
                {
                    AddEvent(oldParent.Id, oldParent.ParentId, WatcherChangeTypes.Changed);
                }
                AddEvent(newItem.ParentId, (newItem.Parent?.ParentId).GetValueOrDefault(), WatcherChangeTypes.Changed);
            }
            return(newItem);
        }
Exemplo n.º 9
0
        public async Task <IFileSystemInfo> UpdateAsync(SqlItem item, UpdateOptions options)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Id == Guid.Empty)
            {
                throw new UnauthorizedAccessException();
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Log("Item : " + item.Id + " '" + item.Name + "' attributes: " + item.Attributes);

            var sql        = "UPDATE Item SET ";
            var parameters = new Dictionary <string, object>();

            parameters["id"] = item.Id;

            var    sets    = new List <string>();
            string newName = null;
            var    changed = false;

            if (options.Name != null && options.Name != item.Name)
            {
                newName = options.Name;
                if (options.EnsureUniqueName && item.Parent is SqlItem parent)
                {
                    newName = await GetNewChildNameAsync(parent, newName).ConfigureAwait(false);
                }

                parameters["name"] = newName;
                sets.Add("Name = @name");
            }

            if (options.CreationTimeUtc.HasValue)
            {
                parameters["creationTimeUtc"] = options.CreationTimeUtc.Value.RemoveMilliseconds();
                sets.Add("CreationTimeUtc = @creationTimeUtc");
                changed = true;
            }

            if (options.LastAccessTimeUtc.HasValue)
            {
                parameters["lastAccessTimeUtc"] = options.LastAccessTimeUtc.Value.RemoveMilliseconds();
                sets.Add("LastAccessTimeUtc = @lastAccessTimeUtc");
                changed = true;
            }

            if (options.LastWriteTimeUtc.HasValue)
            {
                parameters["lastWriteTimeUtc"] = options.LastWriteTimeUtc.Value.RemoveMilliseconds();
                sets.Add("LastWriteTimeUtc = @lastWriteTimeUtc");
                changed = true;
            }

            if (options.Attributes.HasValue)
            {
                parameters["attributes"] = (int)options.Attributes.Value;
                sets.Add("Attributes = @attributes");
                changed = true;
            }

            if (sets.Count == 0) // nothing to do
            {
                return(item);
            }

            sql += string.Join(", ", sets) + " WHERE Id = @id";
            await SqlExtensions.ExecuteNonQueryAsync(ConnectionString, sql, parameters, Logger).ConfigureAwait(false);

            var newItem = await GetItemAsync(item.Id).ConfigureAwait(false);

            if (newName != null)
            {
                AddEvent(newItem.Id, newItem.ParentId, WatcherChangeTypes.Renamed, newName);
            }

            if (changed)
            {
                AddEvent(newItem.Id, newItem.ParentId, WatcherChangeTypes.Changed);
            }
            AddEvent(newItem.ParentId, (newItem.Parent?.ParentId).GetValueOrDefault(), WatcherChangeTypes.Changed);
            return(newItem);
        }
Exemplo n.º 10
0
        public async Task <IFileSystemInfo> CreateAsync(SqlItem parentItem, string name, CreateOptions options = null)
        {
            var item = await CreateSqlItemAsync(parentItem, name, options).ConfigureAwait(false);

            return(item);
        }