Skip to content

Commit 8a67d9d

Browse files
committed
Add proper implementation of methods in JnaUtils.
Implement the methods getFilemode and setFilemode in JnaUtils. Not using the libc names as we don't necessarily use JNA and because it is not necessarily a one to one mapping.
1 parent 6900702 commit 8a67d9d

File tree

4 files changed

+440
-99
lines changed

4 files changed

+440
-99
lines changed

src/main/java/com/gitblit/utils/JGitUtils.java

+93-99
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@
8989
import com.gitblit.models.PathModel.PathChangeModel;
9090
import com.gitblit.models.RefModel;
9191
import com.gitblit.models.SubmoduleModel;
92-
import com.sun.jna.Library;
93-
import com.sun.jna.Native;
9492

9593
/**
9694
* Collection of static methods for retrieving information from a repository.
@@ -270,103 +268,99 @@ public static Repository createRepository(File repositoriesFolder, String name)
270268
}
271269
}
272270

273-
/**
274-
* Creates a bare, shared repository.
275-
*
276-
* @param repositoriesFolder
277-
* @param name
278-
* @param shared
279-
* the setting for the --shared option of "git init".
280-
* @return Repository
281-
*/
282-
public static Repository createRepository(File repositoriesFolder, String name, String shared) {
283-
try {
284-
Repository repo = createRepository(repositoriesFolder, name);
285-
286-
GitConfigSharedRepository sharedRepository = new GitConfigSharedRepository(shared);
287-
if (sharedRepository.isShared()) {
288-
StoredConfig config = repo.getConfig();
289-
config.setString("core", null, "sharedRepository", sharedRepository.getValue());
290-
config.setBoolean("receive", null, "denyNonFastforwards", true);
291-
config.save();
292-
293-
if (! System.getProperty("os.name").toLowerCase().startsWith("windows")) {
294-
final CLibrary libc = (CLibrary) Native.loadLibrary("c", CLibrary.class);
295-
296-
//libc.chmod("/path/to/file", 0755);
297-
}
298-
}
299-
300-
return repo;
301-
} catch (IOException e) {
302-
throw new RuntimeException(e);
303-
}
304-
}
305-
interface CLibrary extends Library {
306-
public int chmod(String path, int mode);
307-
}
308-
private enum GitConfigSharedRepositoryValue {
309-
UMASK("0", 0), FALSE("0", 0), OFF("0", 0), NO("0", 0),
310-
GROUP("1", 0660), TRUE("1", 0660), ON("1", 0660), YES("1", 0660),
311-
ALL("2", 0664), WORLD("2", 0664), EVERYBODY("2", 0664),
312-
Oxxx(null, -1);
313-
314-
private String configValue;
315-
private int permValue;
316-
private GitConfigSharedRepositoryValue(String config, int perm) { configValue = config; permValue = perm; };
317-
318-
public String getConfigValue() { return configValue; };
319-
public int getPerm() { return permValue; };
320-
321-
}
322-
private static class GitConfigSharedRepository
323-
{
324-
private int intValue;
325-
GitConfigSharedRepositoryValue enumValue;
326-
327-
GitConfigSharedRepository(String s)
328-
{
329-
if ( s == null || s.trim().isEmpty() ) {
330-
enumValue = GitConfigSharedRepositoryValue.GROUP;
331-
}
332-
else {
333-
try {
334-
// Try one of the string values
335-
enumValue = GitConfigSharedRepositoryValue.valueOf(s.trim().toUpperCase());
336-
} catch (IllegalArgumentException iae) {
337-
try {
338-
// Try if this is an octal number
339-
int i = Integer.parseInt(s, 8);
340-
if ( (i & 0600) != 0600 ) {
341-
String msg = String.format("Problem with core.sharedRepository filemode value (0%03o).\nThe owner of files must always have read and write permissions.", i);
342-
throw new IllegalArgumentException(msg);
343-
}
344-
intValue = i & 0666;
345-
enumValue = GitConfigSharedRepositoryValue.Oxxx;
346-
} catch (NumberFormatException nfe) {
347-
throw new IllegalArgumentException("Bad configuration value for 'shared': '" + s + "'");
348-
}
349-
}
350-
}
351-
}
352-
353-
String getValue()
354-
{
355-
if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return Integer.toOctalString(intValue);
356-
return enumValue.getConfigValue();
357-
}
358-
359-
int getPerm()
360-
{
361-
if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return intValue;
362-
return enumValue.getPerm();
363-
}
364-
365-
boolean isShared()
366-
{
367-
return (enumValue.getPerm() > 0) || enumValue == GitConfigSharedRepositoryValue.Oxxx;
368-
}
369-
}
271+
/**
272+
* Creates a bare, shared repository.
273+
*
274+
* @param repositoriesFolder
275+
* @param name
276+
* @param shared
277+
* the setting for the --shared option of "git init".
278+
* @return Repository
279+
*/
280+
public static Repository createRepository(File repositoriesFolder, String name, String shared) {
281+
try {
282+
Repository repo = createRepository(repositoriesFolder, name);
283+
284+
GitConfigSharedRepository sharedRepository = new GitConfigSharedRepository(shared);
285+
if (sharedRepository.isShared()) {
286+
StoredConfig config = repo.getConfig();
287+
config.setString("core", null, "sharedRepository", sharedRepository.getValue());
288+
config.setBoolean("receive", null, "denyNonFastforwards", true);
289+
config.save();
290+
291+
if (! JnaUtils.isWindows()) {
292+
293+
//libc.chmod("/path/to/file", 0755);
294+
}
295+
}
296+
297+
return repo;
298+
} catch (IOException e) {
299+
throw new RuntimeException(e);
300+
}
301+
}
302+
private enum GitConfigSharedRepositoryValue {
303+
UMASK("0", 0), FALSE("0", 0), OFF("0", 0), NO("0", 0),
304+
GROUP("1", 0660), TRUE("1", 0660), ON("1", 0660), YES("1", 0660),
305+
ALL("2", 0664), WORLD("2", 0664), EVERYBODY("2", 0664),
306+
Oxxx(null, -1);
307+
308+
private String configValue;
309+
private int permValue;
310+
private GitConfigSharedRepositoryValue(String config, int perm) { configValue = config; permValue = perm; };
311+
312+
public String getConfigValue() { return configValue; };
313+
public int getPerm() { return permValue; };
314+
315+
}
316+
private static class GitConfigSharedRepository
317+
{
318+
private int intValue;
319+
GitConfigSharedRepositoryValue enumValue;
320+
321+
GitConfigSharedRepository(String s)
322+
{
323+
if ( s == null || s.trim().isEmpty() ) {
324+
enumValue = GitConfigSharedRepositoryValue.GROUP;
325+
}
326+
else {
327+
try {
328+
// Try one of the string values
329+
enumValue = GitConfigSharedRepositoryValue.valueOf(s.trim().toUpperCase());
330+
} catch (IllegalArgumentException iae) {
331+
try {
332+
// Try if this is an octal number
333+
int i = Integer.parseInt(s, 8);
334+
if ( (i & 0600) != 0600 ) {
335+
String msg = String.format("Problem with core.sharedRepository filemode value (0%03o).\nThe owner of files must always have read and write permissions.", i);
336+
throw new IllegalArgumentException(msg);
337+
}
338+
intValue = i & 0666;
339+
enumValue = GitConfigSharedRepositoryValue.Oxxx;
340+
} catch (NumberFormatException nfe) {
341+
throw new IllegalArgumentException("Bad configuration value for 'shared': '" + s + "'");
342+
}
343+
}
344+
}
345+
}
346+
347+
String getValue()
348+
{
349+
if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return Integer.toOctalString(intValue);
350+
return enumValue.getConfigValue();
351+
}
352+
353+
int getPerm()
354+
{
355+
if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return intValue;
356+
return enumValue.getPerm();
357+
}
358+
359+
boolean isShared()
360+
{
361+
return (enumValue.getPerm() > 0) || enumValue == GitConfigSharedRepositoryValue.Oxxx;
362+
}
363+
}
370364

371365

372366
/**

0 commit comments

Comments
 (0)