use of com.axway.ats.core.filesystem.model.IFileSystemOperations in project ats-framework by Axway.
the class MobileDeviceUtils method listFiles.
/**
*
* @param path a path on the mobile device. It can be a directory or file (if you want to check
* for specific file existence)
* <br/>
* <b>Note for iOS:</b> You have to specify a relative path to the application data folder<br/>
* For example: <i>"Documents/MyAppFiles"</i><br/>
* and we'll internally search for files in:
* <i>"/Users/<username>/Library/Developer/CoreSimulator/Devices/<device_id>/data/Containers/Data/Application/<app_id>/Documents/MyAppFiles/"</i><br/>
* which is the iOS Simulator application data folder path
*
* @return {@link FileInfo} array with all the files and folders from the target path
*/
public FileInfo[] listFiles(String path) {
List<FileInfo> files = new ArrayList<FileInfo>();
if (this.mobileDriver.isAndroidAgent()) {
String[] commandArguments = new String[] { "shell", "ls", "-lan", path };
try {
IProcessExecutor processExecutor = executeAdbCommand(commandArguments, true);
String result = processExecutor.getStandardOutput();
if (!StringUtils.isNullOrEmpty(result)) {
String[] lines = result.split("[\r\n]+");
for (String line : lines) {
Matcher m = LS_ENTRY_PATTERN.matcher(line);
if (m.matches()) {
/*
* group 1 -> file size
* group 2 -> modification date and time
* group 3 -> file name
*/
String fileName = m.group(3);
String absolutePath = new String(IoUtils.normalizeUnixDir(path) + fileName).replace("//", "/");
if (line.startsWith("l") && line.contains("->")) {
// a link
// after '->'
absolutePath = fileName.substring(fileName.lastIndexOf("->") + 2).trim();
// before '->'
fileName = fileName.substring(0, fileName.indexOf("->")).trim();
}
String fileSize = m.group(1);
FileInfo file = new FileInfo(fileName, absolutePath, line.startsWith("d") || fileSize.isEmpty());
if (!fileSize.isEmpty()) {
file.setSize(Long.parseLong(fileSize));
}
file.setModificationDate(LS_DATE_FORMAT.parse(m.group(2)));
files.add(file);
}
}
}
} catch (Exception e) {
throw new MobileOperationException("Unable to list files for path '" + path + "'", e);
}
} else {
// iOS case supposed
IFileSystemOperations fileSystemOperations = getFileSystemOperatoinsImpl();
String[] filePaths = fileSystemOperations.findFiles(getiOSApplicationDataPath() + path, ".*", true, true, false);
for (String filePath : filePaths) {
boolean isDirectory = filePath.endsWith("/");
if (isDirectory) {
filePath = filePath.substring(0, filePath.length() - 1);
}
String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
FileInfo file = new FileInfo(fileName, filePath, isDirectory);
file.setSize(fileSystemOperations.getFileSize(filePath));
files.add(file);
}
}
return files.toArray(new FileInfo[files.size()]);
}
Aggregations