Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Storage/Storage/Common/BlobToFileNameResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -281,8 +281,11 @@ private static bool IsReservedFileName(string fileName)

private string ResolveFileNameConflict(string baseFileName)
{
// TODO - MaxFileNameLength could be <= 0.
int maxFileNameLength = this.getMaxFileNameLength();
if (maxFileNameLength <= 0)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Max file name length must be greater than zero. Actual value: {0}.", maxFileNameLength));
}

Func<string, bool> conflict = delegate (string fileName)
{
Expand All @@ -295,12 +298,26 @@ private string ResolveFileNameConflict(string baseFileName)
{
string postfixString = string.Format(" ({0})", count);

// TODO - trimLength could be be larger than pathAndFilename.Length, what do we do in this case?
// If the postfix and extension alone exceed the limit, we cannot
// safely construct a unique conflict-resolved file name without
// truncating away the counter value. Failing fast avoids generating the same
// candidate filename for multiple count values and prevents the
// conflict-resolution loop from failing to converge.
if (postfixString.Length + extension.Length >= maxFileNameLength)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Max file name length '{0}' is too small to generate a unique conflict-resolved file name for extension '{1}'.",
maxFileNameLength,
extension));
}
Comment on lines +306 to +314

int trimLength = (fileName.Length + postfixString.Length + extension.Length) - maxFileNameLength;

if (trimLength > 0)
{
fileName = fileName.Remove(fileName.Length - trimLength);
fileName = fileName.Remove(Math.Max(0, fileName.Length - trimLength));
}

return string.Format("{0}{1}{2}", fileName, postfixString, extension);
Comment on lines 299 to 323
Expand Down