github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/path.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
     6   */
     7  
     8  package winc
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"syscall"
    15  
    16  	"github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    17  )
    18  
    19  func knownFolderPath(id w32.CSIDL) (string, error) {
    20  	var buf [w32.MAX_PATH]uint16
    21  
    22  	if !w32.SHGetSpecialFolderPath(0, &buf[0], id, false) {
    23  		return "", fmt.Errorf("SHGetSpecialFolderPath failed")
    24  	}
    25  
    26  	return syscall.UTF16ToString(buf[0:]), nil
    27  }
    28  
    29  func AppDataPath() (string, error) {
    30  	return knownFolderPath(w32.CSIDL_APPDATA)
    31  }
    32  
    33  func CommonAppDataPath() (string, error) {
    34  	return knownFolderPath(w32.CSIDL_COMMON_APPDATA)
    35  }
    36  
    37  func LocalAppDataPath() (string, error) {
    38  	return knownFolderPath(w32.CSIDL_LOCAL_APPDATA)
    39  }
    40  
    41  // EnsureAppDataPath uses AppDataPath to ensure storage for local settings and databases.
    42  func EnsureAppDataPath(company, product string) (string, error) {
    43  	path, err := AppDataPath()
    44  	if err != nil {
    45  		return path, err
    46  	}
    47  	p := filepath.Join(path, company, product)
    48  
    49  	if _, err := os.Stat(p); os.IsNotExist(err) {
    50  		// path/to/whatever does not exist
    51  		if err := os.MkdirAll(p, os.ModePerm); err != nil {
    52  			return p, err
    53  		}
    54  	}
    55  	return p, nil
    56  }
    57  
    58  func DriveNames() ([]string, error) {
    59  	bufLen := w32.GetLogicalDriveStrings(0, nil)
    60  	if bufLen == 0 {
    61  		return nil, fmt.Errorf("GetLogicalDriveStrings failed")
    62  	}
    63  	buf := make([]uint16, bufLen+1)
    64  
    65  	bufLen = w32.GetLogicalDriveStrings(bufLen+1, &buf[0])
    66  	if bufLen == 0 {
    67  		return nil, fmt.Errorf("GetLogicalDriveStrings failed")
    68  	}
    69  
    70  	var names []string
    71  	for i := 0; i < len(buf)-2; {
    72  		name := syscall.UTF16ToString(buf[i:])
    73  		names = append(names, name)
    74  		i += len(name) + 1
    75  	}
    76  	return names, nil
    77  }