github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/imageunpacker/unpacker/writeState.go (about)

     1  package unpacker
     2  
     3  import (
     4  	"bytes"
     5  	"path"
     6  	"syscall"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/fsutil"
     9  	"github.com/Cloud-Foundations/Dominator/lib/json"
    10  )
    11  
    12  const filePerms = syscall.S_IRUSR | syscall.S_IWUSR | syscall.S_IRGRP |
    13  	syscall.S_IROTH
    14  
    15  func (u *Unpacker) writeState() error {
    16  	buffer := new(bytes.Buffer)
    17  	u.rwMutex.RLock()
    18  	err := json.WriteWithIndent(buffer, "    ", u.pState)
    19  	u.rwMutex.RUnlock()
    20  	if err != nil {
    21  		return err
    22  	}
    23  	return fsutil.CopyToFile(path.Join(u.baseDir, stateFile), filePerms, buffer,
    24  		uint64(buffer.Len()))
    25  }
    26  
    27  func (u *Unpacker) writeStateWithLock() error {
    28  	buffer := new(bytes.Buffer)
    29  	if err := json.WriteWithIndent(buffer, "    ", u.pState); err != nil {
    30  		return err
    31  	}
    32  	return fsutil.CopyToFile(path.Join(u.baseDir, stateFile), filePerms, buffer,
    33  		uint64(buffer.Len()))
    34  }