github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/simplefs/platform_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package simplefs
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  // Quarantine is for adding the mark of the web.
    14  func Quarantine(ctx context.Context, path string) error {
    15  	return os.WriteFile(path+":Zone.Identifier", []byte("[ZoneTransfer]\r\nZoneId=3"), 0644)
    16  }
    17  
    18  // limitFilenameLengthForWindowsDownloads truncates the filename so that its
    19  // length is smaller than or equal to filenameLengthLimit. The reason for this
    20  // is Windows has a limit on path length of 255, but somehow some calls making
    21  // a file that causes the path length go over that is possible. This limits the
    22  // download filename to 200 bytes, which should be enough to add any suffix or
    23  // the X:\Downloads prefix to.
    24  func limitFilenameLengthForWindowsDownloads(filename string) string {
    25  	const filenameLengthLimit = 200
    26  
    27  	if len(filename) <= filenameLengthLimit {
    28  		return filename
    29  	}
    30  
    31  	ext := filepath.Ext(filename)
    32  	base := filename[:len(filename)-len(ext)]
    33  	basenameLengthLimit := filenameLengthLimit - len(ext)
    34  
    35  	// Find the last index of UTF-8 rune that doesn't cause us to go over
    36  	// filenameLengthLimit/basenameLengthLimit, and use that as the boundary.
    37  	end := 0
    38  	for i := range base {
    39  		if i > basenameLengthLimit {
    40  			break
    41  		}
    42  		end = i
    43  	}
    44  	return base[:end] + ext
    45  }