Example #1
0
            /// <summary>
            /// Try to link to an existing central name file.
            /// If the central name file exist, and the link limit is not exceeded, the destination file is linked to the central name file, and the return value is true.
            /// In this case, the backup of the destination file is considered finished.
            /// If the central name file does not exist, or linking fails due to an exceeded link limit, the return value is false.
            /// </summary>
            /// <returns>True if linking of the destination file to the existing central name file succeeded;, otherwise, false.</returns>
            /// <exception cref="System.IO.IOException">Error creating hard link from {_centralNameFileName} to {_destinationFileName}, ErrorCode: {hlr}</exception>
            public bool TryToLinkToExistingNameFile()
            {
                var fileInfoCentralNameFileName = new FileInfo(_centralNameFileName);

                // Compare the FileInfo of the central name file with the FileInfo of the source file
                // only if both match, we can link the destination file name to the centralNameFile
                if (fileInfoCentralNameFileName.Exists && FileUtilities.AreMetaDataMatching(fileInfoCentralNameFileName, _sourceFile))
                {
                    var hlr = FileUtilities.CreateHardLink(_centralNameFileName, _destinationFileName);
                    if (hlr == 0)
                    {
                        return(true);
                    }
                    else
                    {
                        if (hlr != FileUtilities.ERROR_TOO_MANY_LINKS) // ignore TooManyLinks error, instead let the content file be created anew
                        {
                            throw new System.IO.IOException($"Error creating hard link from {_centralNameFileName} to {_destinationFileName}, ErrorCode: {hlr}");
                        }
                    }
                }
                return(false);
            }