Skip to content

Commit b264792

Browse files
committed
Cached Filename of Constant Length
Previously, the name of a cached file was a sanitized version of the URI. Recently, a user starting caching a file that had a URI that was longer than 255 characters, causing issues with writing a filename of that length to the filesystem. The solution to this was to hash the URI (SHA256 in this case) in order to get a shorter and constant-length filename. [resolves cloudfoundry#484]
1 parent ffeefb9 commit b264792

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

lib/java_buildpack/util/cache/cached_file.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
require 'digest'
1617
require 'fileutils'
1718
require 'java_buildpack/util/cache'
19+
require 'java_buildpack/util/sanitizer'
1820

1921
module JavaBuildpack
2022
module Util
@@ -24,6 +26,7 @@ module Cache
2426
#
2527
# Note: this class is thread-safe, however access to the cached files is not
2628
class CachedFile
29+
include JavaBuildpack::Util
2730

2831
# Creates an instance of the cached file. Files created and expected by this class will all be rooted at
2932
# +cache_root+.
@@ -32,7 +35,7 @@ class CachedFile
3235
# @param [String] uri a uri which uniquely identifies the file in the cache
3336
# @param [Boolean] mutable whether the cached file should be mutable
3437
def initialize(cache_root, uri, mutable)
35-
key = URI.escape(uri.sanitize_uri, ':/&')
38+
key = Digest::SHA256.hexdigest uri.sanitize_uri
3639
@cached = cache_root + "#{key}.cached"
3740
@etag = cache_root + "#{key}.etag"
3841
@last_modified = cache_root + "#{key}.last_modified"

spec/java_buildpack/util/cache/cached_file_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
require 'spec_helper'
1717
require 'application_helper'
18+
require 'digest'
1819
require 'fileutils'
1920
require 'java_buildpack/util/cache/cached_file'
2021

@@ -104,7 +105,7 @@
104105
end
105106

106107
def cache_file(extension)
107-
app_dir + "http%3A%2F%2Ffoo-uri%2F.#{extension}"
108+
app_dir + "#{Digest::SHA256.hexdigest('http://foo-uri/')}.#{extension}"
108109
end
109110

110111
def touch(extension, content = '')

spec/java_buildpack/util/cache/download_cache_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'application_helper'
1818
require 'internet_availability_helper'
1919
require 'logging_helper'
20+
require 'digest'
2021
require 'fileutils'
2122
require 'java_buildpack/util/cache/download_cache'
2223
require 'net/http'
@@ -337,7 +338,7 @@
337338
end
338339

339340
def cache_file(root, extension)
340-
root + "http%3A%2F%2Ffoo-uri%2F.#{extension}"
341+
root + "#{Digest::SHA256.hexdigest('http://foo-uri/')}.#{extension}"
341342
end
342343

343344
def expect_complete_cache(root)

0 commit comments

Comments
 (0)