Skip to content

Commit

Permalink
Merge branch 'master' of github.com:onepub-dev/dcli
Browse files Browse the repository at this point in the history
  • Loading branch information
bsutton committed Apr 29, 2024
2 parents a23870c + 117daca commit 1b4a906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
22 changes: 15 additions & 7 deletions dcli/lib/src/util/process_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,29 @@ class ProcessHelper {
continue;
}

// we have to deal with files that contain spaces in their name.
final exe = parts.sublist(0, parts.length - 3).join(' ');
final parentPid = int.tryParse(parts[parts.length - 2]) ?? -1;
final processPid = int.tryParse(parts[parts.length - 1]) ?? -1;
final r = parseWMICLine(process);

final parent = _WindowsParentProcess(
path: exe,
parentPid: parentPid,
processPid: processPid,
path: r.exe,
parentPid: r.parentPid,
processPid: r.processPid,
);
parents.add(parent);
}
return parents;
}

@visibleForTesting
static ({String exe, int parentPid, int processPid}) parseWMICLine(String process) {
final parts = process.split(' ');
// we have to deal with files that contain spaces in their name.
final exe = parts.sublist(0, parts.length - 2).join(' ');
final parentPid = int.tryParse(parts[parts.length - 2]) ?? -1;
final processPid = int.tryParse(parts[parts.length - 1]) ?? -1;

return (exe: exe, parentPid: parentPid, processPid: processPid);
}

bool _windowsIsrunning(int? lpid) {
for (final details in getWindowsProcesses()) {
if (details.pid == lpid) {
Expand Down
19 changes: 19 additions & 0 deletions dcli/test/src/util/process_helper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,23 @@ void main() {

// });
// });

test('line splitter', () {
const process = r'C:\windows\system32\svchost.exe 1104 2128';

var r = ProcessHelper.parseWMICLine(process);

expect(r.exe, equals(r'C:\windows\system32\svchost.exe'));
expect(r.parentPid, equals(1104));
expect(r.processPid, equals(2128));

/// process with space in its name.
const process2 = r'C:\windows\system32\svchost name.exe 1104 2128';

r = ProcessHelper.parseWMICLine(process2);

expect(r.exe, equals(r'C:\windows\system32\svchost name.exe'));
expect(r.parentPid, equals(1104));
expect(r.processPid, equals(2128));
});
}

0 comments on commit 1b4a906

Please sign in to comment.