Skip to content

Commit

Permalink
support list parameter when print apps and add get disk space info cli (
Browse files Browse the repository at this point in the history
#176)

* support list parameter when print apps

* support get disk space info
  • Loading branch information
wangchaoHZ authored Oct 4, 2022
1 parent 06383c6 commit 5bedc19
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ios/afc/afc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
Afc_operation_remove_path uint64 = 0x00000008
Afc_operation_make_dir uint64 = 0x00000009
Afc_operation_file_info uint64 = 0x0000000A
Afc_operation_device_info uint64 = 0x0000000B
Afc_operation_file_open uint64 = 0x0000000D
Afc_operation_file_close uint64 = 0x00000014
Afc_operation_file_write uint64 = 0x00000010
Expand Down Expand Up @@ -63,6 +64,13 @@ const (
Afc_Err_DirNotEmpty = 33
)

type AFCDeviceInfo struct {
Model string
TotalBytes uint64
FreeBytes uint64
BlockSize uint64
}

func getError(errorCode uint64) error {
switch errorCode {
case Afc_Err_UnknownError:
Expand Down
46 changes: 46 additions & 0 deletions ios/afc/fsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,52 @@ func (conn *Connection) listDir(path string) ([]string, error) {
return fileList, nil
}

func (conn *Connection) GetSpaceInfo() (*AFCDeviceInfo, error) {
thisLength := Afc_header_size
header := AfcPacketHeader{Magic: Afc_magic, Packet_num: conn.packageNumber, Operation: Afc_operation_device_info, This_length: thisLength, Entire_length: thisLength}
conn.packageNumber++
packet := AfcPacket{Header: header, HeaderPayload: nil, Payload: nil}
response, err := conn.sendAfcPacketAndAwaitResponse(packet)
if err != nil {
return nil, err
}
if err = conn.checkOperationStatus(response); err != nil {
return nil, fmt.Errorf("mkdir: unexpected afc status: %v", err)
}

bs := bytes.Split(response.Payload, []byte{0})
strs := make([]string, len(bs)-1)
for i := 0; i < len(strs); i++ {
strs[i] = string(bs[i])
}
m := make(map[string]string)
if strs != nil {
for i := 0; i < len(strs); i += 2 {
m[strs[i]] = strs[i+1]
}
}

totalBytes, err := strconv.ParseUint(m["FSTotalBytes"], 10, 64)
if err != nil {
return nil, err
}
freeBytes, err := strconv.ParseUint(m["FSFreeBytes"], 10, 64)
if err != nil {
return nil, err
}
blockSize, err := strconv.ParseUint(m["FSBlockSize"], 10, 64)
if err != nil {
return nil, err
}

return &AFCDeviceInfo{
Model: m["Model"],
TotalBytes: totalBytes,
FreeBytes: freeBytes,
BlockSize: blockSize,
}, nil
}

//ListFiles returns all files in the given directory, matching the pattern.
//Example: ListFiles(".", "*") returns all files and dirs in the current path the afc connection is in
func (conn *Connection) ListFiles(cwd string, matchPattern string) ([]string, error) {
Expand Down
13 changes: 13 additions & 0 deletions ios/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ func FixWindowsPaths(path string) string {
}
return path
}

func ByteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB", float64(b)/float64(div), "kMGTPE"[exp])
}
33 changes: 29 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Usage:
ios pcap [options] [--pid=<processID>] [--process=<processName>]
ios install --path=<ipaOrAppFolder> [options]
ios uninstall <bundleID> [options]
ios apps [--system] [--all] [options]
ios apps [--system] [--all] [--list] [options]
ios launch <bundleID> [options]
ios kill (<bundleID> | --pid=<processID> | --process=<processName>) [options]
ios runtest <bundleID> [options]
Expand All @@ -108,6 +108,7 @@ Usage:
ios setlocationgpx [options] [--gpxfilepath=<gpxfilepath>]
ios resetlocation [options]
ios assistivetouch (enable | disable | toggle | get) [--force] [options]
ios diskspace [options]
Options:
-v --verbose Enable Debug Logging.
Expand Down Expand Up @@ -175,7 +176,7 @@ The commands work as following:
ios readpair Dump detailed information about the pairrecord for a device.
ios install --path=<ipaOrAppFolder> [options] Specify a .app folder or an installable ipa file that will be installed.
ios pcap [options] [--pid=<processID>] [--process=<processName>] Starts a pcap dump of network traffic, use --pid or --process to filter specific processes.
ios apps [--system] [--all] Retrieves a list of installed applications. --system prints out preinstalled system apps. --all prints all apps, including system, user, and hidden apps.
ios apps [--system] [--all] [--list] Retrieves a list of installed applications. --system prints out preinstalled system apps. --all prints all apps, including system, user, and hidden apps. --list only prints bundle ID, bundle name and version number.
ios launch <bundleID> Launch app with the bundleID on the device. Get your bundle ID from the apps command.
ios kill (<bundleID> | --pid=<processID> | --process=<processName>) [options] Kill app with the specified bundleID, process id, or process name on the device.
ios runtest <bundleID> Run a XCUITest.
Expand All @@ -192,6 +193,7 @@ The commands work as following:
ios setlocationgpx [options] [--gpxfilepath=<gpxfilepath>] Updates the location of the device based on the data in a GPX file. Example: setlocationgpx --gpxfilepath=/home/username/location.gpx
ios resetlocation [options] Resets the location of the device to the actual one
ios assistivetouch (enable | disable | toggle | get) [--force] [options] Enables, disables, toggles, or returns the state of the "AssistiveTouch" software home-screen button. iOS 11+ only (Use --force to try on older versions).
ios diskspace [options] Prints disk space info.
`, version)
arguments, err := docopt.ParseDoc(usage)
Expand Down Expand Up @@ -410,9 +412,10 @@ The commands work as following:
b, _ = arguments.Bool("apps")

if b {
list, _ := arguments.Bool("--list")
system, _ := arguments.Bool("--system")
all, _ := arguments.Bool("--all")
printInstalledApps(device, system, all)
printInstalledApps(device, system, all, list)
return
}

Expand Down Expand Up @@ -682,6 +685,22 @@ The commands work as following:
afcService.Close()
return
}

b, _ = arguments.Bool("diskspace")
if b {
afcService, err := afc.New(device)
exitIfError("connect afc service failed", err)
info, err := afcService.GetSpaceInfo()
if err != nil {
exitIfError("get device info push failed", err)
}
fmt.Printf(" Model: %s\n", info.Model)
fmt.Printf(" BlockSize: %d\n", info.BlockSize/8)
fmt.Printf(" FreeSpace: %s\n", ios.ByteCountDecimal(int64(info.FreeBytes)))
fmt.Printf(" UsedSpace: %s\n", ios.ByteCountDecimal(int64(info.TotalBytes-info.FreeBytes)))
fmt.Printf(" TotalSpace: %s\n", ios.ByteCountDecimal(int64(info.TotalBytes)))
return
}
}

func mobileGestaltCommand(device ios.DeviceEntry, arguments docopt.Opts) bool {
Expand Down Expand Up @@ -1126,7 +1145,7 @@ func printDeviceDate(device ios.DeviceEntry) {
}

}
func printInstalledApps(device ios.DeviceEntry, system bool, all bool) {
func printInstalledApps(device ios.DeviceEntry, system bool, all bool, list bool) {
svc, _ := installationproxy.New(device)
var err error
var response []installationproxy.AppInfo
Expand All @@ -1143,6 +1162,12 @@ func printInstalledApps(device ios.DeviceEntry, system bool, all bool) {
}
exitIfError("browsing "+appType+" apps failed", err)

if list {
for _, v := range response {
fmt.Printf("%s %s %s\n", v.CFBundleIdentifier, v.CFBundleName, v.CFBundleShortVersionString)
}
return
}
if JSONdisabled {
log.Info(response)
} else {
Expand Down

0 comments on commit 5bedc19

Please sign in to comment.