Skip to content

Commit 7a8ab51

Browse files
committed
Improve Windows packaging experience
Previously, when the DownloadCache was used on a Windows machine, it could not read and write files properly. This was due to it using the default system encoding which differed from the expected UTF-8. This change updates the DownloadCache to always read and write files as binary in order to get around the encoding issue. Previously, when files were cached by the DownloadCache, the file names were escaped to ensure that there were no forward slashes in them. On Windows, this escaping is not sufficient, as colons must be escaped as well. This change updates the escaping to cover this additional case. [#71925232][resolves #53]
1 parent 836c59b commit 7a8ab51

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

lib/java_buildpack/util/cache/cached_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CachedFile
3434
def initialize(cache_root, uri)
3535
FileUtils.mkdir_p cache_root
3636

37-
key = URI.escape(uri, '/')
37+
key = URI.escape(uri, ':/')
3838
@cached = cache_root + "#{key}.cached"
3939
@etag = cache_root + "#{key}.etag"
4040
@last_modified = cache_root + "#{key}.last_modified"

lib/java_buildpack/util/cache/download_cache.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get(uri, &block)
6363
cached_file, downloaded = from_immutable_caches(uri), false unless cached_file
6464

6565
fail "Unable to find cached file for #{uri}" unless cached_file
66-
cached_file.cached(File::RDONLY, downloaded, &block)
66+
cached_file.cached(File::RDONLY | File::BINARY, downloaded, &block)
6767
end
6868

6969
# Removes an item from the mutable cache.
@@ -135,7 +135,7 @@ def attempt(http, request, cached_file)
135135
end
136136

137137
def cache_content(response, cached_file)
138-
cached_file.cached(File::CREAT | File::WRONLY) do |f|
138+
cached_file.cached(File::CREAT | File::WRONLY | File::BINARY) do |f|
139139
@logger.debug { "Persisting content to #{f.path}" }
140140

141141
f.truncate(0)
@@ -153,7 +153,7 @@ def cache_etag(response, cached_file)
153153

154154
@logger.debug { "Persisting etag: #{etag}" }
155155

156-
cached_file.etag(File::CREAT | File::WRONLY) do |f|
156+
cached_file.etag(File::CREAT | File::WRONLY | File::BINARY) do |f|
157157
f.truncate(0)
158158
f.write etag
159159
f.fsync
@@ -167,7 +167,7 @@ def cache_last_modified(response, cached_file)
167167

168168
@logger.debug { "Persisting last-modified: #{last_modified}" }
169169

170-
cached_file.last_modified(File::CREAT | File::WRONLY) do |f|
170+
cached_file.last_modified(File::CREAT | File::WRONLY | File::BINARY) do |f|
171171
f.truncate(0)
172172
f.write last_modified
173173
f.fsync
@@ -223,11 +223,11 @@ def request(uri, cached_file)
223223
request = Net::HTTP::Get.new(uri.request_uri)
224224

225225
if cached_file.etag?
226-
cached_file.etag(File::RDONLY) { |f| request['If-None-Match'] = File.read(f) }
226+
cached_file.etag(File::RDONLY | File::BINARY) { |f| request['If-None-Match'] = File.read(f) }
227227
end
228228

229229
if cached_file.last_modified?
230-
cached_file.last_modified(File::RDONLY) { |f| request['If-Modified-Since'] = File.read(f) }
230+
cached_file.last_modified(File::RDONLY | File::BINARY) { |f| request['If-Modified-Since'] = File.read(f) }
231231
end
232232

233233
@logger.debug { "Request: #{request.path}, #{request.to_hash}" }

spec/java_buildpack/util/cache/cached_file_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
end
8181

8282
def cache_file(extension)
83-
app_dir + "http:%2F%2Ffoo-uri%2F.#{extension}"
83+
app_dir + "http%3A%2F%2Ffoo-uri%2F.#{extension}"
8484
end
8585

8686
def touch(extension, content = '')

spec/java_buildpack/util/cache/download_cache_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@
241241
end
242242

243243
def cache_file(root, extension)
244-
root + "http:%2F%2Ffoo-uri%2F.#{extension}"
244+
root + "http%3A%2F%2Ffoo-uri%2F.#{extension}"
245245
end
246246

247247
def credential_cache_file(root, extension)
248-
root + "http:%2F%2Ftest-username:test-password@foo-uri%2F.#{extension}"
248+
root + "http%3A%2F%2Ftest-username%3Atest-password@foo-uri%2F.#{extension}"
249249
end
250250

251251
def expect_complete_cache(root)

0 commit comments

Comments
 (0)