code: func writeFile(fs utilfs.Filesystem, path string, data []byte) (retErr error) {
  	// Create a temporary file in the base directory of `path` with a prefix.
  	tmpFile, err := fs.TempFile(filepath.Dir(path), tmpPrefix)
  	if err != nil {
  		return err
  	}
  
  	tmpPath := tmpFile.Name()
  	shouldClose := true
  
  	defer func() {
  		// Close the file.
  		if shouldClose {
  			if err := tmpFile.Close(); err != nil {
  				if retErr == nil {
  					retErr = fmt.Errorf("close error: %v", err)
  				} else {
  					retErr = fmt.Errorf("failed to close temp file after error %v; close error: %v", retErr, err)
  				}
  			}
  		}
  
  		// Clean up the temp file on error.
  		if retErr != nil && tmpPath != "" {
  			if err := removePath(fs, tmpPath); err != nil {
  				retErr = fmt.Errorf("failed to remove the temporary file (%q) after error %v; remove error: %v", tmpPath, retErr, err)
  			}
  		}
  	}()
  
  	// Write data.
  	if _, err := tmpFile.Write(data); err != nil {
  		return err
  	}
  
  	// Sync file.
  	if err := tmpFile.Sync(); err != nil {
  		return err
  	}
  
  	// Closing the file before renaming.
  	err = tmpFile.Close()
  	shouldClose = false
  	if err != nil {
  		return err
  	}
  
  	return fs.Rename(tmpPath, path)
  }
docs: // writeFile writes data to path in a single transaction.