Skip to content

Commit d7735b9

Browse files
authored
set permissions for temporary directory (#4882)
1 parent 6db7e45 commit d7735b9

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/SSCMRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.io.IOException;
3131
import java.io.OutputStream;
3232
import java.io.Reader;
33-
import java.nio.file.Files;
3433
import java.util.ArrayList;
3534
import java.util.List;
3635
import java.util.Properties;
@@ -44,6 +43,7 @@
4443
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4544
import org.opengrok.indexer.logger.LoggerFactory;
4645
import org.opengrok.indexer.util.Executor;
46+
import org.opengrok.indexer.util.IOUtils;
4747

4848
/**
4949
* Access to Surround SCM repository.
@@ -178,9 +178,8 @@ History getHistory(File file, String sinceRevision)
178178
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
179179

180180
File directory = new File(parent);
181-
182181
try {
183-
final File tmp = Files.createTempDirectory("opengrokSSCMtmp").toFile();
182+
final File tmp = IOUtils.createTemporaryDirectory("opengrokSSCMtmp");
184183
String tmpName = tmp.getCanonicalPath();
185184

186185
List<String> argv = new ArrayList<>();
@@ -236,6 +235,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
236235
LOGGER.log(Level.SEVERE, "Failed to get file", exception);
237236
}
238237

238+
239239
return false;
240240
}
241241

opengrok-indexer/src/main/java/org/opengrok/indexer/util/IOUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@
3838
import java.nio.file.Path;
3939
import java.nio.file.SimpleFileVisitor;
4040
import java.nio.file.attribute.BasicFileAttributes;
41+
import java.nio.file.attribute.FileAttribute;
42+
import java.nio.file.attribute.PosixFilePermission;
43+
import java.nio.file.attribute.PosixFilePermissions;
4144
import java.util.ArrayList;
4245
import java.util.Arrays;
4346
import java.util.List;
4447
import java.util.Map;
48+
import java.util.Set;
4549
import java.util.logging.Level;
4650
import java.util.logging.Logger;
4751

52+
import org.apache.commons.lang3.SystemUtils;
4853
import org.jetbrains.annotations.NotNull;
4954
import org.opengrok.indexer.logger.LoggerFactory;
5055

@@ -303,4 +308,31 @@ public static String getFileContent(File file) {
303308
}
304309
return "";
305310
}
311+
312+
/**
313+
* Create temporary directory with permissions restricted to the owner.
314+
* @param prefix prefix for the temporary directory name
315+
* @return File object
316+
* @throws IOException on I/O error or failure to set the permissions
317+
*/
318+
public static File createTemporaryDirectory(String prefix) throws IOException {
319+
File tmp;
320+
if (SystemUtils.IS_OS_UNIX) {
321+
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.
322+
asFileAttribute(PosixFilePermissions.fromString("rwx------"));
323+
tmp = Files.createTempDirectory(prefix, attr).toFile();
324+
} else {
325+
tmp = Files.createTempDirectory(prefix).toFile();
326+
if (!tmp.setReadable(true, true)) {
327+
throw new IOException("unable to set read permissions for '" + tmp.getAbsolutePath() + "'");
328+
}
329+
if (!tmp.setWritable(true, true)) {
330+
throw new IOException("unable to set write permissions for '" + tmp.getAbsolutePath() + "'");
331+
}
332+
if (!tmp.setExecutable(true, true)) {
333+
throw new IOException("unable to set executable permissions for '" + tmp.getAbsolutePath() + "'");
334+
}
335+
}
336+
return tmp;
337+
}
306338
}

0 commit comments

Comments
 (0)