diskspace.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package diskspace
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os/exec"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. )
  11. /*
  12. Disk Space Services
  13. Return the disk information of the system.
  14. The most basic task of all
  15. */
  16. type LogicalDiskSpaceInfo struct {
  17. Device string
  18. Volume int64
  19. Used int64
  20. Available int64
  21. UsedPercentage string
  22. MountPoint string
  23. }
  24. func HandleDiskSpaceList(w http.ResponseWriter, r *http.Request) {
  25. allDisksVolume := GetAllLogicDiskInfo()
  26. jsonString, _ := json.Marshal(allDisksVolume)
  27. w.Header().Set("Content-Type", "application/json")
  28. w.Write(jsonString)
  29. }
  30. func GetAllLogicDiskInfo() []LogicalDiskSpaceInfo {
  31. if runtime.GOOS == "windows" {
  32. //Check window disk info, wip
  33. cmd := exec.Command("wmic", "logicaldisk", "get", "caption,size,freespace")
  34. out, err := cmd.CombinedOutput()
  35. if err != nil {
  36. log.Println("wmic not supported.")
  37. return []LogicalDiskSpaceInfo{}
  38. }
  39. lines := strings.Split(string(out), "\n")
  40. var results []LogicalDiskSpaceInfo
  41. for _, line := range lines {
  42. if strings.Contains(line, ":") {
  43. //This is a valid drive
  44. line = strings.TrimSpace(line)
  45. //Tidy the line
  46. for strings.Contains(line, " ") {
  47. line = strings.Replace(line, " ", " ", -1)
  48. }
  49. //Split by space
  50. infoChunk := strings.Split(line, " ")
  51. if len(infoChunk) == 1 {
  52. //Drive reserved and not mounted, like SD card adapters
  53. results = append(results, LogicalDiskSpaceInfo{
  54. Device: infoChunk[0],
  55. Volume: 0,
  56. Used: 0,
  57. Available: 0,
  58. UsedPercentage: "Not Mounted",
  59. MountPoint: infoChunk[0],
  60. })
  61. } else if len(infoChunk) > 2 {
  62. size, err := stringToInt64(infoChunk[2])
  63. if err != nil {
  64. size = 0
  65. }
  66. freespace, err := stringToInt64(infoChunk[1])
  67. if err != nil {
  68. size = 0
  69. }
  70. usedSpace := size - freespace
  71. percentage := int64(float64(usedSpace) / float64(size) * 100)
  72. results = append(results, LogicalDiskSpaceInfo{
  73. Device: infoChunk[0],
  74. Volume: size,
  75. Used: usedSpace,
  76. Available: freespace,
  77. UsedPercentage: strconv.Itoa(int(percentage)) + "%",
  78. MountPoint: infoChunk[0],
  79. })
  80. }
  81. }
  82. }
  83. return results
  84. } else {
  85. //Get drive status using df command
  86. cmdin := `df -k | sed -e /Filesystem/d`
  87. cmd := exec.Command("bash", "-c", cmdin)
  88. dev, err := cmd.Output()
  89. if err != nil {
  90. dev = []byte{}
  91. }
  92. drives := strings.Split(string(dev), "\n")
  93. if len(drives) == 0 {
  94. return []LogicalDiskSpaceInfo{}
  95. }
  96. var arr []LogicalDiskSpaceInfo
  97. for _, driveInfo := range drives {
  98. if driveInfo == "" {
  99. continue
  100. }
  101. for strings.Contains(driveInfo, " ") {
  102. driveInfo = strings.Replace(driveInfo, " ", " ", -1)
  103. }
  104. driveInfoChunk := strings.Split(driveInfo, " ")
  105. volume, _ := stringToInt64(driveInfoChunk[1])
  106. usedSpace, _ := stringToInt64(driveInfoChunk[2])
  107. freespaceInByte, _ := stringToInt64(driveInfoChunk[3])
  108. LogicalDisk := LogicalDiskSpaceInfo{
  109. Device: driveInfoChunk[0],
  110. Volume: volume * 1024,
  111. Used: usedSpace * 1024,
  112. Available: freespaceInByte * 1024,
  113. UsedPercentage: driveInfoChunk[4],
  114. MountPoint: driveInfoChunk[5],
  115. }
  116. //Mountpoint fixes for macOS
  117. //tested on Darwin 11.1
  118. if runtime.GOOS == "darwin" {
  119. if LogicalDisk.Device == "map" {
  120. LogicalDisk.Device = driveInfoChunk[1]
  121. LogicalDisk.MountPoint = driveInfoChunk[9]
  122. } else {
  123. LogicalDisk.MountPoint = driveInfoChunk[8]
  124. }
  125. }
  126. arr = append(arr, LogicalDisk)
  127. }
  128. return arr
  129. }
  130. }
  131. func stringToInt64(value string) (int64, error) {
  132. n, err := strconv.Atoi(value)
  133. return int64(n), err
  134. }