@@ -20,6 +20,7 @@ import (
2020 "github.com/moby/sys/user"
2121
2222 "github.com/moby/go-archive/compression"
23+ "github.com/moby/go-archive/tarheader"
2324)
2425
2526// ImpliedDirectoryMode represents the mode (Unix permissions) applied to directories that are implied by files in a
@@ -234,71 +235,11 @@ func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModi
234235 return pipeReader
235236}
236237
237- // assert that we implement [tar.FileInfoNames].
238- var _ tar.FileInfoNames = (* nosysFileInfo )(nil )
239-
240- // nosysFileInfo hides the system-dependent info of the wrapped FileInfo to
241- // prevent tar.FileInfoHeader from introspecting it and potentially calling into
242- // glibc.
243- //
244- // It implements [tar.FileInfoNames] to further prevent [tar.FileInfoHeader]
245- // from performing any lookups on go1.23 and up. see https://go.dev/issue/50102
246- type nosysFileInfo struct {
247- os.FileInfo
248- }
249-
250- // Uname stubs out looking up username. It implements [tar.FileInfoNames]
251- // to prevent [tar.FileInfoHeader] from loading libraries to perform
252- // username lookups.
253- func (fi nosysFileInfo ) Uname () (string , error ) {
254- return "" , nil
255- }
256-
257- // Gname stubs out looking up group-name. It implements [tar.FileInfoNames]
258- // to prevent [tar.FileInfoHeader] from loading libraries to perform
259- // username lookups.
260- func (fi nosysFileInfo ) Gname () (string , error ) {
261- return "" , nil
262- }
263-
264- func (fi nosysFileInfo ) Sys () interface {} {
265- // A Sys value of type *tar.Header is safe as it is system-independent.
266- // The tar.FileInfoHeader function copies the fields into the returned
267- // header without performing any OS lookups.
268- if sys , ok := fi .FileInfo .Sys ().(* tar.Header ); ok {
269- return sys
270- }
271- return nil
272- }
273-
274- // sysStat, if non-nil, populates hdr from system-dependent fields of fi.
275- var sysStat func (fi os.FileInfo , hdr * tar.Header ) error
276-
277238// FileInfoHeaderNoLookups creates a partially-populated tar.Header from fi.
278239//
279- // Compared to the archive/tar.FileInfoHeader function, this function is safe to
280- // call from a chrooted process as it does not populate fields which would
281- // require operating system lookups. It behaves identically to
282- // tar.FileInfoHeader when fi is a FileInfo value returned from
283- // tar.Header.FileInfo().
284- //
285- // When fi is a FileInfo for a native file, such as returned from os.Stat() and
286- // os.Lstat(), the returned Header value differs from one returned from
287- // tar.FileInfoHeader in the following ways. The Uname and Gname fields are not
288- // set as OS lookups would be required to populate them. The AccessTime and
289- // ChangeTime fields are not currently set (not yet implemented) although that
290- // is subject to change. Callers which require the AccessTime or ChangeTime
291- // fields to be zeroed should explicitly zero them out in the returned Header
292- // value to avoid any compatibility issues in the future.
240+ // Deprecated: use [tarheader.FileInfoHeaderNoLookups].
293241func FileInfoHeaderNoLookups (fi os.FileInfo , link string ) (* tar.Header , error ) {
294- hdr , err := tar .FileInfoHeader (nosysFileInfo {fi }, link )
295- if err != nil {
296- return nil , err
297- }
298- if sysStat != nil {
299- return hdr , sysStat (fi , hdr )
300- }
301- return hdr , nil
242+ return tarheader .FileInfoHeaderNoLookups (fi , link )
302243}
303244
304245// FileInfoHeader creates a populated Header from fi.
@@ -309,7 +250,7 @@ func FileInfoHeaderNoLookups(fi os.FileInfo, link string) (*tar.Header, error) {
309250// precision, and the Uname and Gname fields are only set when fi is a FileInfo
310251// value returned from tar.Header.FileInfo().
311252func FileInfoHeader (name string , fi os.FileInfo , link string ) (* tar.Header , error ) {
312- hdr , err := FileInfoHeaderNoLookups (fi , link )
253+ hdr , err := tarheader . FileInfoHeaderNoLookups (fi , link )
313254 if err != nil {
314255 return nil , err
315256 }
@@ -1177,7 +1118,7 @@ func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
11771118 }
11781119 defer srcF .Close ()
11791120
1180- hdr , err := FileInfoHeaderNoLookups (srcSt , "" )
1121+ hdr , err := tarheader . FileInfoHeaderNoLookups (srcSt , "" )
11811122 if err != nil {
11821123 return err
11831124 }
0 commit comments