@@ -79,6 +79,28 @@ public static boolean isWindows()
79
79
80
80
private interface UnixCLibrary extends Library {
81
81
public int chmod (String path , int mode );
82
+ public int getgid ();
83
+ public int getegid ();
84
+ }
85
+
86
+
87
+ public static int getgid ()
88
+ {
89
+ if (isWindows ()) {
90
+ throw new UnsupportedOperationException ("The method JnaUtils.getgid is not supported under Windows." );
91
+ }
92
+
93
+ return getUnixCLibrary ().getgid ();
94
+ }
95
+
96
+
97
+ public static int getegid ()
98
+ {
99
+ if (isWindows ()) {
100
+ throw new UnsupportedOperationException ("The method JnaUtils.getegid is not supported under Windows." );
101
+ }
102
+
103
+ return getUnixCLibrary ().getegid ();
82
104
}
83
105
84
106
@@ -157,21 +179,77 @@ public static int getFilemode(String path)
157
179
throw new UnsupportedOperationException ("The method JnaUtils.getFilemode is not supported under Windows." );
158
180
}
159
181
182
+ Filestat stat = getFilestat (path );
183
+ if ( stat == null ) return -1 ;
184
+ return stat .mode ;
185
+ }
186
+
187
+
188
+ /**
189
+ * Status information of a file.
190
+ */
191
+ public static class Filestat
192
+ {
193
+ public int mode ; // file mode, permissions, type
194
+ public int uid ; // user Id of owner
195
+ public int gid ; // group Id of owner
196
+
197
+ Filestat (int mode , int uid , int gid ) {
198
+ this .mode = mode ; this .uid = uid ; this .gid = gid ;
199
+ }
200
+ }
201
+
202
+
203
+ /**
204
+ * Get Unix file status information for a file.
205
+ *
206
+ * This method is only implemented for OSes of the Unix family. It returns file status
207
+ * information for a file. Currently this is the file mode, the user id and group id of the owner.
208
+ *
209
+ * @param path
210
+ * File/directory to get the file status from.
211
+ * @return Upon successful completion, a Filestat object containing the file information is returned.
212
+ * Otherwise, null is returned.
213
+ */
214
+ public static Filestat getFilestat (File path )
215
+ {
216
+ return getFilestat (path .getAbsolutePath ());
217
+ }
218
+
219
+
220
+ /**
221
+ * Get Unix file status information for a file.
222
+ *
223
+ * This method is only implemented for OSes of the Unix family. It returns file status
224
+ * information for a file. Currently this is the file mode, the user id and group id of the owner.
225
+ *
226
+ * @param path
227
+ * Path to a file/directory to get the file status from.
228
+ * @return Upon successful completion, a Filestat object containing the file information is returned.
229
+ * Otherwise, null is returned.
230
+ */
231
+ public static Filestat getFilestat (String path )
232
+ {
233
+ if (isWindows ()) {
234
+ throw new UnsupportedOperationException ("The method JnaUtils.getFilestat is not supported under Windows." );
235
+ }
236
+
160
237
161
238
int mode = 0 ;
162
239
163
240
// Use a Runtime, because implementing stat() via JNA is just too much trouble.
241
+ // This could be done with the 'stat' command, too. But that may have a shell specific implementation, so we use 'ls' instead.
164
242
String lsLine = runProcessLs (path );
165
243
if (lsLine == null ) {
166
244
LOGGER .debug ("Could not get file information for path " + path );
167
- return - 1 ;
245
+ return null ;
168
246
}
169
247
170
- Pattern p = Pattern .compile ("^(([-bcdlsp ])([-r][-w][-xSs])([-r][-w][-xSs])([-r][-w][-xTt])) " );
248
+ Pattern p = Pattern .compile ("^(([-bcdlspCDMnP? ])([-r][-w][-xSs])([-r][-w][-xSs])([-r][-w][-xTt]))[@+.]? +[0-9]+ +([0-9]+) +([0-9]+ ) " );
171
249
Matcher m = p .matcher (lsLine );
172
250
if ( !m .lookingAt () ) {
173
251
LOGGER .debug ("Could not parse valid file mode information for path " + path );
174
- return - 1 ;
252
+ return null ;
175
253
}
176
254
177
255
// Parse mode string to mode bits
@@ -227,20 +305,20 @@ public static int getFilemode(String path)
227
305
}
228
306
}
229
307
230
- return mode ;
308
+ return new Filestat ( mode , Integer . parseInt ( m . group ( 6 )), Integer . parseInt ( m . group ( 7 ))) ;
231
309
}
232
310
233
311
234
312
/**
235
- * Run the unix command 'ls -ldO ' on a single file and return the resulting output line.
313
+ * Run the unix command 'ls -ldn ' on a single file and return the resulting output line.
236
314
*
237
315
* @param path
238
316
* Path to a single file or directory.
239
317
* @return The first line of output from the 'ls' command. Null, if an error occurred and no line could be read.
240
318
*/
241
319
private static String runProcessLs (String path )
242
320
{
243
- String cmd = "ls -ld " + path ;
321
+ String cmd = "ls -ldn " + path ;
244
322
Runtime rt = Runtime .getRuntime ();
245
323
Process pr = null ;
246
324
InputStreamReader ir = null ;
0 commit comments