Exemplo n.º 1
0
        async Task TestDeleteDirectory(CancellationToken cancellationToken)
        {
            //try to delete non-existent
            var TestDir = new ConfigurationFileRequest
            {
                Path = "/TestDeleteDir"
            };

            await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);

            //try to delete non-empty
            const string TestString = "Hello world!";

            using var uploadMs = new MemoryStream(Encoding.UTF8.GetBytes(TestString));
            var file = await configurationClient.Write(new ConfigurationFileRequest
            {
                Path = TestDir.Path + "/test.txt"
            }, uploadMs, cancellationToken).ConfigureAwait(false);

            Assert.IsTrue(FileExists(file));
            Assert.IsNull(file.LastReadHash);

            var updatedFileTuple = await configurationClient.Read(file, cancellationToken).ConfigureAwait(false);

            var updatedFile = updatedFileTuple.Item1;

            Assert.IsNotNull(updatedFile.LastReadHash);
            using (var downloadMemoryStream = new MemoryStream())
            {
                using (var downloadStream = updatedFileTuple.Item2)
                {
                    var requestStream = downloadStream as CachedResponseStream;
                    Assert.IsNotNull(requestStream);
                    var response = (HttpResponseMessage)requestStream.GetType().GetField("response", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(requestStream);
                    Assert.AreEqual(response.Content.Headers.ContentType.MediaType, MediaTypeNames.Application.Octet);
                    await downloadStream.CopyToAsync(downloadMemoryStream);
                }
                Assert.AreEqual(TestString, Encoding.UTF8.GetString(downloadMemoryStream.ToArray()).Trim());
            }

            await ApiAssert.ThrowsException <ConflictException>(() => configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken), ErrorCode.ConfigurationDirectoryNotEmpty).ConfigureAwait(false);

            file.FileTicket = null;
            await configurationClient.Write(new ConfigurationFileRequest
            {
                Path         = updatedFile.Path,
                LastReadHash = updatedFile.LastReadHash
            }, null, cancellationToken).ConfigureAwait(false);

            Assert.IsFalse(FileExists(file));

            await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);

            var tmp  = (TestDir.Path?.StartsWith('/') ?? false) ? '.' + TestDir.Path : TestDir.Path;
            var path = Path.Combine(instance.Path, "Configuration", tmp);

            Assert.IsFalse(Directory.Exists(path));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Update([FromBody] ConfigurationFileRequest model, CancellationToken cancellationToken)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (ForbidDueToModeConflicts(model.Path, out var systemIdentity))
            {
                return(Forbid());
            }

            try
            {
                return(await WithComponentInstance(
                           async instance =>
                {
                    var newFile = await instance
                                  .Configuration
                                  .Write(
                        model.Path,
                        systemIdentity,
                        model.LastReadHash,
                        cancellationToken)
                                  .ConfigureAwait(false);

                    return model.LastReadHash == null ? (IActionResult)Accepted(newFile) : Json(newFile);
                })
                       .ConfigureAwait(false));
            }
            catch (IOException e)
            {
                Logger.LogInformation("IOException while updating file {0}: {1}", model.Path, e);
                return(Conflict(new ErrorMessageResponse(ErrorCode.IOError)
                {
                    AdditionalData = e.Message,
                }));
            }
            catch (NotImplementedException)
            {
                return(RequiresPosixSystemIdentity());
            }
        }