github.com/iDigitalFlame/xmt@v0.5.4/device/regedit/key.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package regedit
    21  
    22  import "github.com/iDigitalFlame/xmt/device/winapi/registry"
    23  
    24  // MakeKey will attempt to create an empty key for the supplied path.
    25  //
    26  // The key path can either be a "reg" style path (ex: HKLM\System or
    27  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    28  //
    29  // Any errors creating the key will be returned.
    30  //
    31  // Returns device.ErrNoWindows on non-Windows devices.
    32  func MakeKey(key string) error {
    33  	k, err := read(key, true)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	k.Close()
    38  	return nil
    39  }
    40  
    41  // Delete will attempt to delete the specified key or value name specified.
    42  //
    43  // The value will be probed for a type and if it is a key, it will delete the
    44  // key ONLY if it is empty. (Use 'DeleteEx' for recursive deletion).
    45  //
    46  // The key path can either be a "reg" style path (ex: HKLM\System or
    47  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    48  //
    49  // Any errors deleting the key or value will be returned.
    50  //
    51  // Returns device.ErrNoWindows on non-Windows devices.
    52  func Delete(key, value string) error {
    53  	return DeleteEx(key, value, false)
    54  }
    55  
    56  // DeleteKey will attempt to delete the specified subkey name specified.
    57  //
    58  // If force is specified, this will recursively delete a subkey and will delete
    59  // non-empty subkeys. Otherwise, if force is false, non-empty subkeys will NOT
    60  // be deleted.
    61  //
    62  // The key path can either be a "reg" style path (ex: HKLM\System or
    63  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    64  //
    65  // Any errors creating the key will be returned.
    66  //
    67  // Returns device.ErrNoWindows on non-Windows devices.
    68  func DeleteKey(key string, force bool) error {
    69  	h, d, err := translateRootKey(key)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	if d >= len(key) || h == 0 {
    74  		return registry.ErrNotExist
    75  	}
    76  	if force {
    77  		return registry.DeleteTree(h, key[d:])
    78  	}
    79  	return registry.DeleteEx(h, key[d:], 0)
    80  }
    81  
    82  // DeleteEx will attempt to delete the specified key or value name specified.
    83  //
    84  // The value will be probed for a type and if it is a key, it will delete the
    85  // key ONLY if it is empty or force is true (which will recursively delete the
    86  // key)
    87  //
    88  // The key path can either be a "reg" style path (ex: HKLM\System or
    89  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    90  //
    91  // Any errors deleting the key or value will be returned.
    92  //
    93  // Returns device.ErrNoWindows on non-Windows devices.
    94  func DeleteEx(key, value string, force bool) error {
    95  	k, err := read(key, true)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	_, t, err := k.Value(value, nil)
   100  	if err == registry.ErrNotExist {
   101  		// 0x2001F - KEY_READ | KEY_WRITE
   102  		s, err1 := k.Open(value, 0x2001F)
   103  		if err1 != nil {
   104  			k.Close()
   105  			return err
   106  		}
   107  		s.Close()
   108  		if force {
   109  			err = registry.DeleteTree(k, value)
   110  		} else {
   111  			err = k.DeleteKey(value)
   112  		}
   113  		k.Close()
   114  		return err
   115  	}
   116  	if err == nil && t > 0 {
   117  		err = k.DeleteValue(value)
   118  	}
   119  	k.Close()
   120  	return err
   121  }