github.com/google/osv-scalibr@v0.4.1/binary/platform/platform_windows.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build windows
    16  
    17  // Package platform provides platform-specific functionality.
    18  package platform
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"os"
    24  	"path/filepath"
    25  
    26  	"github.com/google/osv-scalibr/plugin"
    27  	"golang.org/x/sys/windows"
    28  )
    29  
    30  var (
    31  	errSystemDriveNotSet = errors.New("SystemDrive environment variable not set")
    32  )
    33  
    34  // SystemRoot returns the root directory of the system.
    35  func SystemRoot() (string, error) {
    36  	if os.Getenv("SystemDrive") == "" {
    37  		return "", errSystemDriveNotSet
    38  	}
    39  
    40  	return os.Getenv("SystemDrive") + string(os.PathSeparator), nil
    41  }
    42  
    43  func retrieveAllDrives() ([]string, error) {
    44  	// first determine the required size of the buffer
    45  	size, err := windows.GetLogicalDriveStrings(0, nil)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// perform the actual syscall
    51  	buf := make([]uint16, size)
    52  	n, err := windows.GetLogicalDriveStrings(size, &buf[0])
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	var drives []string
    58  	var drive string
    59  	var i uint32
    60  
    61  	// parse the output (null separated strings)
    62  	for ; i < n; i++ {
    63  		if buf[i] == 0 {
    64  			drives = append(drives, drive)
    65  			drive = ""
    66  			continue
    67  		}
    68  
    69  		drive += fmt.Sprintf("%c", buf[i])
    70  	}
    71  
    72  	return drives, nil
    73  }
    74  
    75  // DefaultScanRoots returns the default list of directories to be scanned for Windows.
    76  func DefaultScanRoots(allDrives bool) ([]string, error) {
    77  	systemDrive, err := SystemRoot()
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	scanRoots := []string{systemDrive}
    83  
    84  	if allDrives {
    85  		drives, err := retrieveAllDrives()
    86  		if err != nil {
    87  			return nil, err
    88  		}
    89  
    90  		// add all drives to the scan roots, but we remove the system drive as it's already in the list
    91  		for _, drive := range drives {
    92  			if drive != systemDrive {
    93  				scanRoots = append(scanRoots, drive)
    94  			}
    95  		}
    96  	}
    97  
    98  	return scanRoots, nil
    99  }
   100  
   101  // DefaultIgnoredDirectories returns the default list of directories to be ignored for Windows.
   102  func DefaultIgnoredDirectories() ([]string, error) {
   103  	systemDrive, err := SystemRoot()
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	windir := filepath.Join(systemDrive, "Windows")
   109  	return []string{windir}, nil
   110  }
   111  
   112  // OS returns the OS the SCALIBR binary was build on.
   113  func OS() plugin.OS {
   114  	return plugin.OSWindows
   115  }