Skip to content

Commit 819c5e8

Browse files
committed
Fix a bug where an error change any permissions would cause the operation to fail and other files to not be changed
1 parent 66eb890 commit 819c5e8

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/com/limelight/binding/input/evdev/EvdevReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ public class EvdevReader {
1212
}
1313

1414
// Requires root to chmod /dev/input/eventX
15-
public static boolean setPermissions(int octalPermissions) {
16-
String chmodString = String.format("chmod %o /dev/input/*\n", octalPermissions);
17-
15+
public static boolean setPermissions(String[] files, int octalPermissions) {
1816
ProcessBuilder builder = new ProcessBuilder("su");
1917

2018
try {
2119
Process p = builder.start();
2220

2321
OutputStream stdin = p.getOutputStream();
24-
stdin.write(chmodString.getBytes("UTF-8"));
22+
for (String file : files) {
23+
stdin.write(String.format("chmod %o %s\n", octalPermissions, file).getBytes("UTF-8"));
24+
}
2525
stdin.write("exit\n".getBytes("UTF-8"));
2626
stdin.flush();
2727

src/com/limelight/binding/input/evdev/EvdevWatcher.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void onEvent(int event, String fileName) {
3838

3939
if (!init) {
4040
// If this a real new device, update permissions again so we can read it
41-
EvdevReader.setPermissions(0666);
41+
EvdevReader.setPermissions(new String[]{PATH + "/" + fileName}, 0666);
4242
}
4343

4444
EvdevHandler handler = new EvdevHandler(PATH + "/" + fileName, listener);
@@ -63,17 +63,29 @@ public EvdevWatcher(EvdevListener listener) {
6363
this.listener = listener;
6464
}
6565

66+
private File[] rundownWithPermissionsChange(int newPermissions) {
67+
// Rundown existing files
68+
File devInputDir = new File(PATH);
69+
File[] files = devInputDir.listFiles();
70+
71+
// Set desired permissions
72+
String[] filePaths = new String[files.length];
73+
for (int i = 0; i < files.length; i++) {
74+
filePaths[i] = files[i].getAbsolutePath();
75+
}
76+
EvdevReader.setPermissions(filePaths, newPermissions);
77+
78+
return files;
79+
}
80+
6681
public void start() {
6782
startThread = new Thread() {
6883
@Override
6984
public void run() {
70-
// Get permissions to read the eventX files
71-
EvdevReader.setPermissions(0666);
72-
init = true;
85+
// List all files and allow us access
86+
File[] files = rundownWithPermissionsChange(0666);
7387

74-
// Rundown existing files and generate synthetic events
75-
File devInputDir = new File(PATH);
76-
File[] files = devInputDir.listFiles();
88+
init = true;
7789
for (File f : files) {
7890
observer.onEvent(FileObserver.CREATE, f.getName());
7991
}
@@ -92,7 +104,7 @@ public void run() {
92104
}
93105

94106
// Giveup eventX permissions
95-
EvdevReader.setPermissions(0066);
107+
rundownWithPermissionsChange(066);
96108
}
97109
};
98110
startThread.start();

0 commit comments

Comments
 (0)