github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/paths/transientfile/delete_windows.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  //go:build windows
     5  
     6  package transientfile
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  // ensureDeleteAfterReboot arranges for the specified file to be removed once
    14  // the machine reboots. It exploits the MoveFileEx API call which allows us to
    15  // defer the deletion of a file by specifying a nil value as the destination
    16  // target in conjunction with the MOVEFILE_DELAY_UNTIL_REBOOT flag.
    17  //
    18  // The file to be deleted is appended to the windows registry and automatically
    19  // cleaned up by windows when the system reboots.
    20  //
    21  // For more info see: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexa
    22  func ensureDeleteAfterReboot(file string) error {
    23  	from, err := windows.UTF16PtrFromString(file)
    24  	if err != nil {
    25  		return errors.Trace(err)
    26  	}
    27  	return windows.MoveFileEx(from, nil, windows.MOVEFILE_DELAY_UNTIL_REBOOT)
    28  }