Advertisement
Ponywka

C# parse partitions from ADB device

Oct 18th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SharpAdbClient;
  7.  
  8. namespace TestADBClient
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var device = AdbClient.Instance.GetDevices()[0];
  15.             var receiver = new ConsoleOutputReceiver();
  16.             AdbClient.Instance.ExecuteRemoteCommand("fdisk /dev/block/mmcblk0 -l", device, receiver);
  17.             string output = receiver.ToString();
  18.             string[] fdiskData = output.Split('\n');
  19.             int sectorSize = 0;
  20.             int[,] partitions = new Int32[256,2];
  21.             foreach (string line in fdiskData)
  22.             {
  23.                 if (line.Length > 21 && line.Substring(0, 21) == "Logical sector size: ")
  24.                 {
  25.                     sectorSize = Convert.ToInt32(line.Substring(21));
  26.                 }
  27.                 else
  28.                 {
  29.                     int[] data = new Int32[3];
  30.                     int curPos = 0;
  31.                     bool startParse = false;
  32.                     int lenght = line.Length;
  33.                     bool corrupt = false;
  34.                     if (lenght > 0)
  35.                     {
  36.                         for (int charI = 0; charI < lenght; charI++)
  37.                         {
  38.                             if (curPos < 3)
  39.                             {
  40.                                 if (line[charI] != ' ' && line[charI] >= '0' && line[charI] <= '9')
  41.                                 {
  42.                                     if (startParse)
  43.                                     {
  44.                                         data[curPos] = data[curPos] * 10 + Convert.ToInt32(line[charI].ToString());
  45.                                     }
  46.                                     else
  47.                                     {
  48.                                         startParse = true;
  49.                                         data[curPos] = Convert.ToInt32(line[charI].ToString());
  50.                                     }
  51.                                 }
  52.                                 else
  53.                                 {
  54.                                     if (line[charI] == ' ')
  55.                                     {
  56.                                         if (startParse)
  57.                                         {
  58.                                             curPos++;
  59.                                             startParse = false;
  60.                                         }
  61.                                     }
  62.                                     else
  63.                                     {
  64.                                         charI = lenght;
  65.                                         corrupt = true;
  66.                                     }
  67.                                 }
  68.                             }
  69.                         }
  70.                         if (!corrupt)
  71.                         {
  72.                             partitions[data[0] - 1,0] = data[1];
  73.                             partitions[data[0] - 1,1] = data[2];
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.             Console.ReadKey();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement