Exemplo n.º 1
0
        /// <inheritdoc/>
        private void MoveToDatabase(string temporaryFilePath, ObjectId objId, bool forceWrite = false)
        {
            string fileUrl = BuildUrl(vfsRootUrl, objId);

            lock (LockOnMove)
            {
                var fileExists = virtualFileProvider.FileExists(fileUrl);

                // File may already exists, in this case we decide to not override it.
                if (!fileExists || forceWrite)
                {
                    try
                    {
                        if (fileExists)
                        {
                            // In case we force write, delete old file so that move succeed
                            virtualFileProvider.FileDelete(fileUrl);
                        }
                        else
                        {
                            // Remove the second part of ObjectId to get the path (cf BuildUrl)
                            virtualFileProvider.CreateDirectory(fileUrl.Substring(0, fileUrl.Length - (ObjectId.HashStringLength - 2)));
                        }

                        virtualFileProvider.FileMove(temporaryFilePath, fileUrl);
                    }
                    catch (IOException e)
                    {
                        // Ignore only IOException "The destination file already exists."
                        // because other exceptions that we want to catch might inherit from IOException.
                        // This happens if two FileMove were performed at the same time.
                        if (e.GetType() != typeof(IOException))
                        {
                            throw;
                        }

                        // But we should still clean our temporary file
                        virtualFileProvider.FileDelete(temporaryFilePath);
                    }
                }
                else
                {
                    // But we should still clean our temporary file
                    virtualFileProvider.FileDelete(temporaryFilePath);
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        private ObjectId SaveStream(OdbStreamWriter stream)
        {
            ObjectId objId   = stream.CurrentHash;
            string   fileUrl = BuildUrl(vfsRootUrl, objId);

            string temporaryFilePath = stream.TemporaryName;

            lock (LockOnMove)
            {
                // File may already exists, in this case we decide to not override it.
                if (!virtualFileProvider.FileExists(fileUrl))
                {
                    try
                    {
                        // Remove the second part of ObjectId to get the path (cf BuildUrl)
                        virtualFileProvider.CreateDirectory(fileUrl.Substring(0, fileUrl.Length - (ObjectId.HashStringLength - 2)));
                        virtualFileProvider.FileMove(temporaryFilePath, BuildUrl(vfsRootUrl, objId));
                    }
                    catch (IOException e)
                    {
                        // Ignore only IOException "The destination file already exists."
                        // because other exceptions that we want to catch might inherit from IOException.
                        // This happens if two FileMove were performed at the same time.
                        if (e.GetType() != typeof(IOException))
                        {
                            throw;
                        }

                        // But we should still clean our temporary file
                        virtualFileProvider.FileDelete(temporaryFilePath);
                    }
                }
                else
                {
                    // But we should still clean our temporary file
                    virtualFileProvider.FileDelete(temporaryFilePath);
                }
            }

            return(objId);
        }