directory: simplify newImageDestination#2520
directory: simplify newImageDestination#2520kolyshkin wants to merge 2 commits intocontainers:mainfrom
Conversation
|
LGTM |
mtrmac
left a comment
There was a problem hiding this comment.
Thanks!
The optimization makes sense.
I’m not entirely sure about inlining all of the logic — most of this is really not so core to the newImageDestination “constructor” to be the primary focus of the function. Maybe the removeDirContents loop could stay out of line, it’s pretty self-contained. Or maybe all of this should be in a separate initializeDestinationDir… but let’s get the logic precisely correct first, and then find the right abstraction boundary.
As discussed elsewhere, I’d prefer to keep the error wrapping with "%q", path, throughout.
f841ecb to
58e46d0
Compare
The current code is unnecessarily complicated: - it does unnecessary checks if a file/directory exists before opening it; - it uses three helper functions not used anywhere else; - directory contents is read twice. Untangle all this, making the code simpler. Also, move the logic of initializing a destination directory to a separate function. Now, since the logic of handing version file is now encapsulated in a single function, remove versionPath method and const version, as well as the corresponding test. This also fixes the subtle issue of using ref.path for constructing the version file path, but ref.resolvedPath for all other operations. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This needs to be "require" rather than "assert", as if error is returned, test needs to fail now. Otherwise, close will panic. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
58e46d0 to
e77668f
Compare
mtrmac
left a comment
There was a problem hiding this comment.
ACK on the logic, just some maintenance/organization nits.
| return ErrNotContainerImageDir | ||
| } | ||
| // Indeed an image directory. Reuse by removing all the old contents | ||
| // (including the versionFile, which will be recreated below). |
There was a problem hiding this comment.
It seems worth it to preserve the rationale “so that only one image is in the directory at a time”.
(OTOH containers/skopeo#1237 exists, but that’s clearly not this PR.)
| } | ||
|
|
||
| // versionPath returns a path for the version file within a directory using our conventions. | ||
| func (ref dirReference) versionPath() string { |
There was a problem hiding this comment.
Keeping the "version" path logic in this place, beside the other …Path locations, might be a good way to sort-of document the file format? Also, eventually, https://github.com/containers/image/issues/1876 will need this to live outside of initDestDir.
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const version = "Directory Transport Version: 1.1\n" |
There was a problem hiding this comment.
The way I think about it, this is really a constant of the file format, not a private implementation detail of initDestDir (we can’t just change the contents to make initDestDir individually nicer, for example); so it should be a global constant in the module. Also, eventually, https://github.com/containers/image/issues/1876 will need this to live outside of initDestDir.
[Well, we should eventually actually parse the numbers and do comparisons, but that’s not yet necessary.]
Based on patches from #2512.
The current code is unnecessarily complicated:
Untangle all this, making the code simpler.
Also, move the logic of initializing a destination directory to a
separate function.
Now, since the logic of handing version file is now encapsulated in a
single function, remove versionPath method and const version, as well
as the corresponding test.
This also fixes the subtle issue of using ref.path for constructing the
version file path, but ref.resolvedPath for all other operations.