Skip to content

Commit 240f9aa

Browse files
committed
Revert atomic_writer to original state
Signed-off-by: joshvanl <[email protected]>
1 parent 1e7b555 commit 240f9aa

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

third_party/util/atomic_writer.go

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ type AtomicWriter struct {
6060

6161
// FileProjection contains file Data and access Mode
6262
type FileProjection struct {
63-
Data []byte
64-
Mode int32
65-
FsUser *int64
66-
FsGroup *int64
63+
Data []byte
64+
Mode int32
65+
FsUser *int64
6766
}
6867

6968
// NewAtomicWriter creates a new AtomicWriter configured to write to the given
@@ -96,14 +95,14 @@ const (
9695
// data to determine if an update is required.
9796
// 5. A new timestamped dir is created
9897
// 6. The payload is written to the new timestamped directory
99-
// 7. Symlinks and directory for new user-group-visible files are created (if needed).
98+
// 7. Symlinks and directory for new user-visible files are created (if needed).
10099
//
101100
// For example, consider the files:
102101
// <target-dir>/podName
103-
// <target-dir>/user-group/labels
102+
// <target-dir>/user/labels
104103
// <target-dir>/k8s/annotations
105104
//
106-
// The user-group visible files are symbolic links into the internal data directory:
105+
// The user visible files are symbolic links into the internal data directory:
107106
// <target-dir>/podName -> ..data/podName
108107
// <target-dir>/usr -> ..data/usr
109108
// <target-dir>/k8s -> ..data/k8s
@@ -114,7 +113,7 @@ const (
114113
// 8. A symlink to the new timestamped directory ..data_tmp is created that will
115114
// become the new data directory
116115
// 9. The new data directory symlink is renamed to the data directory; rename is atomic
117-
// 10. Old paths are removed from the user-group-visible portion of the target directory
116+
// 10. Old paths are removed from the user-visible portion of the target directory
118117
// 11. The previous timestamped directory is removed, if it exists
119118
func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
120119
// (1)
@@ -144,7 +143,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
144143
// (3)
145144
pathsToRemove, err = w.pathsToRemove(cleanPayload, oldTsPath)
146145
if err != nil {
147-
klog.Errorf("%s: error determining user-group-visible files to remove: %v", w.logContext, err)
146+
klog.Errorf("%s: error determining user-visible files to remove: %v", w.logContext, err)
148147
return err
149148
}
150149

@@ -176,7 +175,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
176175
klog.V(4).Infof("%s: performed write of new data to ts data directory: %s", w.logContext, tsDir)
177176

178177
// (7)
179-
if err = w.createGroupVisibleFiles(cleanPayload); err != nil {
178+
if err = w.createUserVisibleFiles(cleanPayload); err != nil {
180179
klog.Errorf("%s: error creating visible symlinks in %s: %v", w.logContext, w.targetDir, err)
181180
return err
182181
}
@@ -205,7 +204,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
205204
}
206205

207206
// (10)
208-
if err = w.removeGroupVisiblePaths(pathsToRemove); err != nil {
207+
if err = w.removeUserVisiblePaths(pathsToRemove); err != nil {
209208
klog.Errorf("%s: error removing old visible symlinks: %v", w.logContext, err)
210209
return err
211210
}
@@ -277,8 +276,8 @@ func validatePath(targetPath string) error {
277276

278277
// shouldWritePayload returns whether the payload should be written to disk.
279278
func shouldWritePayload(payload map[string]FileProjection, oldTsDir string) (bool, error) {
280-
for userGroupVisiblePath, fileProjection := range payload {
281-
shouldWrite, err := shouldWriteFile(filepath.Join(oldTsDir, userGroupVisiblePath), fileProjection.Data)
279+
for userVisiblePath, fileProjection := range payload {
280+
shouldWrite, err := shouldWriteFile(filepath.Join(oldTsDir, userVisiblePath), fileProjection.Data)
282281
if err != nil {
283282
return false, err
284283
}
@@ -356,7 +355,7 @@ func (w *AtomicWriter) newTimestampDir() (string, error) {
356355
return "", err
357356
}
358357

359-
// 0755 permissions are needed to allow 'user' and 'other' to recurse the
358+
// 0755 permissions are needed to allow 'group' and 'other' to recurse the
360359
// directory tree. do a chmod here to ensure that permissions are set correctly
361360
// regardless of the process' umask.
362361
err = os.Chmod(tsDir, 0755)
@@ -371,10 +370,10 @@ func (w *AtomicWriter) newTimestampDir() (string, error) {
371370
// writePayloadToDir writes the given payload to the given directory. The
372371
// directory must exist.
373372
func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir string) error {
374-
for userGroupVisiblePath, fileProjection := range payload {
373+
for userVisiblePath, fileProjection := range payload {
375374
content := fileProjection.Data
376375
mode := os.FileMode(fileProjection.Mode)
377-
fullPath := filepath.Join(dir, userGroupVisiblePath)
376+
fullPath := filepath.Join(dir, userVisiblePath)
378377
baseDir, _ := filepath.Split(fullPath)
379378

380379
if err := os.MkdirAll(baseDir, os.ModePerm); err != nil {
@@ -395,31 +394,19 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
395394
return err
396395
}
397396

398-
// If neither FsUser or FsGroup defined, we don't need to Chown.
399-
if fileProjection.FsUser == nil && fileProjection.FsGroup == nil {
397+
if fileProjection.FsUser == nil {
400398
continue
401399
}
402-
403-
fsUser := -1
404-
if fileProjection.FsUser != nil {
405-
fsUser = int(*fileProjection.FsUser)
406-
}
407-
408-
fsGroup := -1
409-
if fileProjection.FsGroup != nil {
410-
fsGroup = int(*fileProjection.FsGroup)
411-
}
412-
413-
if err := os.Chown(fullPath, fsUser, fsGroup); err != nil {
414-
klog.Errorf("%s: unable to change file %s with group=%v user=%v owner: %v", w.logContext, fullPath, fsUser, fsGroup, err)
400+
if err := os.Chown(fullPath, int(*fileProjection.FsUser), -1); err != nil {
401+
klog.Errorf("%s: unable to change file %s with owner %v: %v", w.logContext, fullPath, int(*fileProjection.FsUser), err)
415402
return err
416403
}
417404
}
418405

419406
return nil
420407
}
421408

422-
// createGroupVisibleFiles creates the relative symlinks for all the
409+
// createUserVisibleFiles creates the relative symlinks for all the
423410
// files configured in the payload. If the directory in a file path does not
424411
// exist, it is created.
425412
//
@@ -429,13 +416,13 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
429416
// bar -> ..data/bar
430417
// foo -> ..data/foo
431418
// baz -> ..data/baz
432-
func (w *AtomicWriter) createGroupVisibleFiles(payload map[string]FileProjection) error {
433-
for userGroupVisiblePath := range payload {
434-
slashpos := strings.Index(userGroupVisiblePath, string(os.PathSeparator))
419+
func (w *AtomicWriter) createUserVisibleFiles(payload map[string]FileProjection) error {
420+
for userVisiblePath := range payload {
421+
slashpos := strings.Index(userVisiblePath, string(os.PathSeparator))
435422
if slashpos == -1 {
436-
slashpos = len(userGroupVisiblePath)
423+
slashpos = len(userVisiblePath)
437424
}
438-
linkname := userGroupVisiblePath[:slashpos]
425+
linkname := userVisiblePath[:slashpos]
439426
_, err := os.Readlink(filepath.Join(w.targetDir, linkname))
440427
if err != nil && os.IsNotExist(err) {
441428
// The link into the data directory for this path doesn't exist; create it
@@ -451,9 +438,9 @@ func (w *AtomicWriter) createGroupVisibleFiles(payload map[string]FileProjection
451438
return nil
452439
}
453440

454-
// removeGroupVisiblePaths removes the set of paths from the user-group-visible
441+
// removeUserVisiblePaths removes the set of paths from the user-visible
455442
// portion of the writer's target directory.
456-
func (w *AtomicWriter) removeGroupVisiblePaths(paths sets.String) error {
443+
func (w *AtomicWriter) removeUserVisiblePaths(paths sets.String) error {
457444
ps := string(os.PathSeparator)
458445
var lasterr error
459446
for p := range paths {
@@ -462,7 +449,7 @@ func (w *AtomicWriter) removeGroupVisiblePaths(paths sets.String) error {
462449
continue
463450
}
464451
if err := os.Remove(filepath.Join(w.targetDir, p)); err != nil {
465-
klog.Errorf("%s: error pruning old user-group-visible path %s: %v", w.logContext, p, err)
452+
klog.Errorf("%s: error pruning old user-visible path %s: %v", w.logContext, p, err)
466453
lasterr = err
467454
}
468455
}

third_party/util/atomic_writer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ func TestValidatePayload(t *testing.T) {
899899
}
900900
}
901901

902-
func TestCreateGroupVisibleFiles(t *testing.T) {
902+
func TestCreateUserVisibleFiles(t *testing.T) {
903903
cases := []struct {
904904
name string
905905
payload map[string]FileProjection
@@ -960,7 +960,7 @@ func TestCreateGroupVisibleFiles(t *testing.T) {
960960
if err != nil {
961961
t.Fatalf("%v: unexpected error validating payload: %v", tc.name, err)
962962
}
963-
err = writer.createGroupVisibleFiles(payload)
963+
err = writer.createUserVisibleFiles(payload)
964964
if err != nil {
965965
t.Fatalf("%v: unexpected error creating visible files: %v", tc.name, err)
966966
}

0 commit comments

Comments
 (0)