34
34
* @author Florian Zschocke
35
35
*/
36
36
public class JnaUtils {
37
- public static final int S_IFMT = 0170000 ;
38
- public static final int S_IFIFO = 0010000 ;
39
- public static final int S_IFCHR = 0020000 ;
40
- public static final int S_IFDIR = 0040000 ;
41
- public static final int S_IFBLK = 0060000 ;
42
- public static final int S_IFREG = 0100000 ;
43
- public static final int S_IFLNK = 0120000 ;
44
- public static final int S_IFSOCK = 0140000 ;
45
-
46
- public static final int S_ISUID = 0004000 ;
47
- public static final int S_ISGID = 0002000 ;
48
- public static final int S_ISVTX = 0001000 ;
49
-
50
- public static final int S_IRWXU = 0000700 ;
51
- public static final int S_IRUSR = 0000400 ;
52
- public static final int S_IWUSR = 0000200 ;
53
- public static final int S_IXUSR = 0000100 ;
54
- public static final int S_IRWXG = 0000070 ;
55
- public static final int S_IRGRP = 0000040 ;
56
- public static final int S_IWGRP = 0000020 ;
57
- public static final int S_IXGRP = 0000010 ;
58
- public static final int S_IRWXO = 0000007 ;
59
- public static final int S_IROTH = 0000004 ;
60
- public static final int S_IWOTH = 0000002 ;
61
- public static final int S_IXOTH = 0000001 ;
37
+ public static final int S_ISUID = 0004000 ; // set user id on execution
38
+ public static final int S_ISGID = 0002000 ; // set group id on execution
39
+ public static final int S_ISVTX = 0001000 ; // sticky bit, save swapped text even after use
40
+
41
+ public static final int S_IRWXU = 0000700 ; // RWX mask for owner
42
+ public static final int S_IRUSR = 0000400 ; // read permission for owner
43
+ public static final int S_IWUSR = 0000200 ; // write permission for owner
44
+ public static final int S_IXUSR = 0000100 ; // execute/search permission for owner
45
+ public static final int S_IRWXG = 0000070 ; // RWX mask for group
46
+ public static final int S_IRGRP = 0000040 ; // read permission for group
47
+ public static final int S_IWGRP = 0000020 ; // write permission for group
48
+ public static final int S_IXGRP = 0000010 ; // execute/search permission for group
49
+ public static final int S_IRWXO = 0000007 ; // RWX mask for other
50
+ public static final int S_IROTH = 0000004 ; // read permission for other
51
+ public static final int S_IWOTH = 0000002 ; // write permission for other
52
+ public static final int S_IXOTH = 0000001 ; // execute/search permission for other
53
+
54
+ public static final int S_IFMT = 0170000 ; // type of file mask
55
+ public static final int S_IFIFO = 0010000 ; // named pipe (fifo)
56
+ public static final int S_IFCHR = 0020000 ; // character special device
57
+ public static final int S_IFDIR = 0040000 ; // directory
58
+ public static final int S_IFBLK = 0060000 ; // block special device
59
+ public static final int S_IFREG = 0100000 ; // regular file
60
+ public static final int S_IFLNK = 0120000 ; // symbolic link
61
+ public static final int S_IFSOCK = 0140000 ; // socket
62
62
63
63
64
64
private static final Logger LOGGER = LoggerFactory .getLogger (JGitUtils .class );
65
65
66
66
private static UnixCLibrary unixlibc = null ;
67
67
68
68
69
+ /**
70
+ * Utility method to check if the JVM is running on a Windows OS.
71
+ *
72
+ * @return true, if the system property 'os.name' starts with 'Windows'.
73
+ */
69
74
public static boolean isWindows ()
70
75
{
71
76
return System .getProperty ("os.name" ).toLowerCase ().startsWith ("windows" );
@@ -77,11 +82,37 @@ private interface UnixCLibrary extends Library {
77
82
}
78
83
79
84
80
- public static int setFilemode (File path , int mode )
85
+ /**
86
+ * Set the permission bits of a file.
87
+ *
88
+ * The permission bits are set to the provided mode. This method is only
89
+ * implemented for OSes of the Unix family and makes use of the 'chmod'
90
+ * function of the native C library. See 'man 2 chmod' for more information.
91
+ *
92
+ * @param path
93
+ * File/directory to set the permission bits for.
94
+ * @param mode
95
+ * A mode created from or'd permission bit masks S_I*
96
+ * @return Upon successful completion, a value of 0 returned. Otherwise, a value of -1 is returned.
97
+ */
98
+ public static int setFilemode (File file , int mode )
81
99
{
82
- return setFilemode (path .getAbsolutePath (), mode );
100
+ return setFilemode (file .getAbsolutePath (), mode );
83
101
}
84
102
103
+ /**
104
+ * Set the permission bits of a file.
105
+ *
106
+ * The permission bits are set to the provided mode. This method is only
107
+ * implemented for OSes of the Unix family and makes use of the 'chmod'
108
+ * function of the native C library. See 'man 2 chmod' for more information.
109
+ *
110
+ * @param path
111
+ * Path to a file/directory to set the permission bits for.
112
+ * @param mode
113
+ * A mode created from or'd permission bit masks S_I*
114
+ * @return Upon successful completion, a value of 0 returned. Otherwise, a value of -1 is returned.
115
+ */
85
116
public static int setFilemode (String path , int mode )
86
117
{
87
118
if (isWindows ()) {
@@ -93,11 +124,33 @@ public static int setFilemode(String path, int mode)
93
124
94
125
95
126
127
+ /**
128
+ * Get the file mode bits of a file.
129
+ *
130
+ * This method is only implemented for OSes of the Unix family. It returns the file mode
131
+ * information as available in the st_mode member of the resulting struct stat when calling
132
+ * 'lstat' on a file.
133
+ *
134
+ * @param path
135
+ * File/directory to get the file mode from.
136
+ * @return Upon successful completion, the file mode bits are returned. Otherwise, a value of -1 is returned.
137
+ */
96
138
public static int getFilemode (File path )
97
139
{
98
140
return getFilemode (path .getAbsolutePath ());
99
141
}
100
142
143
+ /**
144
+ * Get the file mode bits of a file.
145
+ *
146
+ * This method is only implemented for OSes of the Unix family. It returns the file mode
147
+ * information as available in the st_mode member of the resulting struct stat when calling
148
+ * 'lstat' on a file.
149
+ *
150
+ * @param path
151
+ * Path to a file/directory to get the file mode from.
152
+ * @return Upon successful completion, the file mode bits are returned. Otherwise, a value of -1 is returned.
153
+ */
101
154
public static int getFilemode (String path )
102
155
{
103
156
if (isWindows ()) {
@@ -178,6 +231,13 @@ public static int getFilemode(String path)
178
231
}
179
232
180
233
234
+ /**
235
+ * Run the unix command 'ls -ldO' on a single file and return the resulting output line.
236
+ *
237
+ * @param path
238
+ * Path to a single file or directory.
239
+ * @return The first line of output from the 'ls' command. Null, if an error occurred and no line could be read.
240
+ */
181
241
private static String runProcessLs (String path )
182
242
{
183
243
String cmd = "ls -ldO " + path ;
0 commit comments