Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

179 Zeilen
4.6 KiB

  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Fork, exec, wait, etc.
  5. package windows
  6. import (
  7. errorspkg "errors"
  8. "unsafe"
  9. )
  10. // EscapeArg rewrites command line argument s as prescribed
  11. // in http://msdn.microsoft.com/en-us/library/ms880421.
  12. // This function returns "" (2 double quotes) if s is empty.
  13. // Alternatively, these transformations are done:
  14. // - every back slash (\) is doubled, but only if immediately
  15. // followed by double quote (");
  16. // - every double quote (") is escaped by back slash (\);
  17. // - finally, s is wrapped with double quotes (arg -> "arg"),
  18. // but only if there is space or tab inside s.
  19. func EscapeArg(s string) string {
  20. if len(s) == 0 {
  21. return "\"\""
  22. }
  23. n := len(s)
  24. hasSpace := false
  25. for i := 0; i < len(s); i++ {
  26. switch s[i] {
  27. case '"', '\\':
  28. n++
  29. case ' ', '\t':
  30. hasSpace = true
  31. }
  32. }
  33. if hasSpace {
  34. n += 2
  35. }
  36. if n == len(s) {
  37. return s
  38. }
  39. qs := make([]byte, n)
  40. j := 0
  41. if hasSpace {
  42. qs[j] = '"'
  43. j++
  44. }
  45. slashes := 0
  46. for i := 0; i < len(s); i++ {
  47. switch s[i] {
  48. default:
  49. slashes = 0
  50. qs[j] = s[i]
  51. case '\\':
  52. slashes++
  53. qs[j] = s[i]
  54. case '"':
  55. for ; slashes > 0; slashes-- {
  56. qs[j] = '\\'
  57. j++
  58. }
  59. qs[j] = '\\'
  60. j++
  61. qs[j] = s[i]
  62. }
  63. j++
  64. }
  65. if hasSpace {
  66. for ; slashes > 0; slashes-- {
  67. qs[j] = '\\'
  68. j++
  69. }
  70. qs[j] = '"'
  71. j++
  72. }
  73. return string(qs[:j])
  74. }
  75. // ComposeCommandLine escapes and joins the given arguments suitable for use as a Windows command line,
  76. // in CreateProcess's CommandLine argument, CreateService/ChangeServiceConfig's BinaryPathName argument,
  77. // or any program that uses CommandLineToArgv.
  78. func ComposeCommandLine(args []string) string {
  79. var commandLine string
  80. for i := range args {
  81. if i > 0 {
  82. commandLine += " "
  83. }
  84. commandLine += EscapeArg(args[i])
  85. }
  86. return commandLine
  87. }
  88. // DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
  89. // as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
  90. // command lines are passed around.
  91. func DecomposeCommandLine(commandLine string) ([]string, error) {
  92. if len(commandLine) == 0 {
  93. return []string{}, nil
  94. }
  95. var argc int32
  96. argv, err := CommandLineToArgv(StringToUTF16Ptr(commandLine), &argc)
  97. if err != nil {
  98. return nil, err
  99. }
  100. defer LocalFree(Handle(unsafe.Pointer(argv)))
  101. var args []string
  102. for _, v := range (*argv)[:argc] {
  103. args = append(args, UTF16ToString((*v)[:]))
  104. }
  105. return args, nil
  106. }
  107. func CloseOnExec(fd Handle) {
  108. SetHandleInformation(Handle(fd), HANDLE_FLAG_INHERIT, 0)
  109. }
  110. // FullPath retrieves the full path of the specified file.
  111. func FullPath(name string) (path string, err error) {
  112. p, err := UTF16PtrFromString(name)
  113. if err != nil {
  114. return "", err
  115. }
  116. n := uint32(100)
  117. for {
  118. buf := make([]uint16, n)
  119. n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
  120. if err != nil {
  121. return "", err
  122. }
  123. if n <= uint32(len(buf)) {
  124. return UTF16ToString(buf[:n]), nil
  125. }
  126. }
  127. }
  128. // NewProcThreadAttributeList allocates a new ProcThreadAttributeListContainer, with the requested maximum number of attributes.
  129. func NewProcThreadAttributeList(maxAttrCount uint32) (*ProcThreadAttributeListContainer, error) {
  130. var size uintptr
  131. err := initializeProcThreadAttributeList(nil, maxAttrCount, 0, &size)
  132. if err != ERROR_INSUFFICIENT_BUFFER {
  133. if err == nil {
  134. return nil, errorspkg.New("unable to query buffer size from InitializeProcThreadAttributeList")
  135. }
  136. return nil, err
  137. }
  138. alloc, err := LocalAlloc(LMEM_FIXED, uint32(size))
  139. if err != nil {
  140. return nil, err
  141. }
  142. // size is guaranteed to be ≥1 by InitializeProcThreadAttributeList.
  143. al := &ProcThreadAttributeListContainer{data: (*ProcThreadAttributeList)(unsafe.Pointer(alloc))}
  144. err = initializeProcThreadAttributeList(al.data, maxAttrCount, 0, &size)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return al, err
  149. }
  150. // Update modifies the ProcThreadAttributeList using UpdateProcThreadAttribute.
  151. func (al *ProcThreadAttributeListContainer) Update(attribute uintptr, value unsafe.Pointer, size uintptr) error {
  152. al.pointers = append(al.pointers, value)
  153. return updateProcThreadAttribute(al.data, 0, attribute, value, size, nil, nil)
  154. }
  155. // Delete frees ProcThreadAttributeList's resources.
  156. func (al *ProcThreadAttributeListContainer) Delete() {
  157. deleteProcThreadAttributeList(al.data)
  158. LocalFree(Handle(unsafe.Pointer(al.data)))
  159. al.data = nil
  160. al.pointers = nil
  161. }
  162. // List returns the actual ProcThreadAttributeList to be passed to StartupInfoEx.
  163. func (al *ProcThreadAttributeListContainer) List() *ProcThreadAttributeList {
  164. return al.data
  165. }