Fix a bug where an error change any permissions would cause the operation to fail and other files to not be changed

This commit is contained in:
Cameron Gutman 2014-09-03 23:33:25 -07:00
parent bb869a51fd
commit 6bae056e3a
2 changed files with 24 additions and 12 deletions

View File

@ -12,16 +12,16 @@ public class EvdevReader {
}
// Requires root to chmod /dev/input/eventX
public static boolean setPermissions(int octalPermissions) {
String chmodString = String.format("chmod %o /dev/input/*\n", octalPermissions);
public static boolean setPermissions(String[] files, int octalPermissions) {
ProcessBuilder builder = new ProcessBuilder("su");
try {
Process p = builder.start();
OutputStream stdin = p.getOutputStream();
stdin.write(chmodString.getBytes("UTF-8"));
for (String file : files) {
stdin.write(String.format("chmod %o %s\n", octalPermissions, file).getBytes("UTF-8"));
}
stdin.write("exit\n".getBytes("UTF-8"));
stdin.flush();

View File

@ -38,7 +38,7 @@ public class EvdevWatcher {
if (!init) {
// If this a real new device, update permissions again so we can read it
EvdevReader.setPermissions(0666);
EvdevReader.setPermissions(new String[]{PATH + "/" + fileName}, 0666);
}
EvdevHandler handler = new EvdevHandler(PATH + "/" + fileName, listener);
@ -63,17 +63,29 @@ public class EvdevWatcher {
this.listener = listener;
}
private File[] rundownWithPermissionsChange(int newPermissions) {
// Rundown existing files
File devInputDir = new File(PATH);
File[] files = devInputDir.listFiles();
// Set desired permissions
String[] filePaths = new String[files.length];
for (int i = 0; i < files.length; i++) {
filePaths[i] = files[i].getAbsolutePath();
}
EvdevReader.setPermissions(filePaths, newPermissions);
return files;
}
public void start() {
startThread = new Thread() {
@Override
public void run() {
// Get permissions to read the eventX files
EvdevReader.setPermissions(0666);
init = true;
// List all files and allow us access
File[] files = rundownWithPermissionsChange(0666);
// Rundown existing files and generate synthetic events
File devInputDir = new File(PATH);
File[] files = devInputDir.listFiles();
init = true;
for (File f : files) {
observer.onEvent(FileObserver.CREATE, f.getName());
}
@ -92,7 +104,7 @@ public class EvdevWatcher {
}
// Giveup eventX permissions
EvdevReader.setPermissions(0066);
rundownWithPermissionsChange(066);
}
};
startThread.start();