use of java.nio.file.Files in project intellij-community by JetBrains.
the class LocalFileSystemTest method doTestInterruptedRefresh.
public static void doTestInterruptedRefresh(@NotNull File top) throws Exception {
for (int i = 1; i <= 3; i++) {
File sub = IoTestUtil.createTestDir(top, "sub_" + i);
for (int j = 1; j <= 3; j++) {
IoTestUtil.createTestDir(sub, "sub_" + j);
}
}
Files.walkFileTree(top.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
for (int k = 1; k <= 3; k++) {
IoTestUtil.createTestFile(dir.toFile(), "file_" + k, ".");
}
return FileVisitResult.CONTINUE;
}
});
VirtualFile topDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(top);
assertNotNull(topDir);
Set<VirtualFile> files = ContainerUtil.newHashSet();
VfsUtilCore.processFilesRecursively(topDir, file -> {
if (!file.isDirectory())
files.add(file);
return true;
});
// 13 dirs of 3 files
assertEquals(39, files.size());
topDir.refresh(false, true);
Set<VirtualFile> processed = ContainerUtil.newHashSet();
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect();
connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {
@Override
public void after(@NotNull List<? extends VFileEvent> events) {
events.forEach(e -> processed.add(e.getFile()));
}
});
try {
files.forEach(f -> IoTestUtil.updateFile(new File(f.getPath()), "+++"));
((NewVirtualFile) topDir).markDirtyRecursively();
RefreshWorker.setCancellingCondition(file -> file.getPath().endsWith(top.getName() + "/sub_2/file_2"));
topDir.refresh(false, true);
assertThat(processed.size()).isGreaterThan(0).isLessThan(files.size());
RefreshWorker.setCancellingCondition(null);
topDir.refresh(false, true);
assertThat(processed).isEqualTo(files);
} finally {
connection.disconnect();
RefreshWorker.setCancellingCondition(null);
}
}
use of java.nio.file.Files in project proxyee-down by monkeyWie.
the class HttpDownController method getChildDirList.
@RequestMapping("/getChildDirList")
public ResultInfo getChildDirList(@RequestBody(required = false) DirForm body) {
ResultInfo resultInfo = new ResultInfo();
List<DirInfo> data = new LinkedList<>();
resultInfo.setData(data);
File[] files;
if (body == null || StringUtils.isEmpty(body.getPath())) {
if (OsUtil.isMac()) {
files = new File("/Users").listFiles(file -> file.isDirectory() && file.getName().indexOf(".") != 0);
} else {
files = File.listRoots();
}
} else {
File file = new File(body.getPath());
if (file.exists() && file.isDirectory()) {
files = file.listFiles();
} else {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("路径不存在");
return resultInfo;
}
}
if (files != null && files.length > 0) {
boolean isFileList = "file".equals(body.getModel());
for (File tempFile : files) {
if (tempFile.isFile()) {
if (isFileList) {
data.add(new DirInfo(StringUtils.isEmpty(tempFile.getName()) ? tempFile.getAbsolutePath() : tempFile.getName(), tempFile.getAbsolutePath(), true));
}
} else if (tempFile.isDirectory() && (tempFile.getParent() == null || !tempFile.isHidden()) && (OsUtil.isWindows() || tempFile.getName().indexOf(".") != 0)) {
DirInfo dirInfo = new DirInfo(StringUtils.isEmpty(tempFile.getName()) ? tempFile.getAbsolutePath() : tempFile.getName(), tempFile.getAbsolutePath(), tempFile.listFiles() == null ? true : Arrays.stream(tempFile.listFiles()).noneMatch(file -> file != null && (file.isDirectory() || isFileList) && !file.isHidden()));
data.add(dirInfo);
}
}
}
return resultInfo;
}
use of java.nio.file.Files in project felix by apache.
the class Posix method less.
protected void less(CommandSession session, Process process, String[] argv) throws Exception {
String[] usage = { "less - file pager", "Usage: less [OPTIONS] [FILES]", " -? --help Show help", " -e --quit-at-eof Exit on second EOF", " -E --QUIT-AT-EOF Exit on EOF", " -F --quit-if-one-screen Exit if entire file fits on first screen", " -q --quiet --silent Silent mode", " -Q --QUIET --SILENT Completely silent", " -S --chop-long-lines Do not fold long lines", " -i --ignore-case Search ignores lowercase case", " -I --IGNORE-CASE Search ignores all case", " -x --tabs Set tab stops", " -N --LINE-NUMBERS Display line number for each line", " --no-init Disable terminal initialization", " --no-keypad Disable keypad handling" };
boolean hasExtendedOptions = false;
try {
Less.class.getField("quitIfOneScreen");
hasExtendedOptions = true;
} catch (NoSuchFieldException e) {
List<String> ustrs = new ArrayList<>(Arrays.asList(usage));
ustrs.removeIf(s -> s.contains("--quit-if-one-screen") || s.contains("--no-init") || s.contains("--no-keypad"));
usage = ustrs.toArray(new String[ustrs.size()]);
}
Options opt = parseOptions(session, usage, argv);
List<Source> sources = new ArrayList<>();
if (opt.args().isEmpty()) {
opt.args().add("-");
}
for (String arg : opt.args()) {
if ("-".equals(arg)) {
sources.add(new StdInSource(process));
} else {
sources.add(new PathSource(session.currentDir().resolve(arg), arg));
}
}
if (!process.isTty(1)) {
for (Source source : sources) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(source.read()))) {
cat(process, reader, opt.isSet("LINE-NUMBERS"));
}
}
return;
}
Less less = new Less(Shell.getTerminal(session));
less.quitAtFirstEof = opt.isSet("QUIT-AT-EOF");
less.quitAtSecondEof = opt.isSet("quit-at-eof");
less.quiet = opt.isSet("quiet");
less.veryQuiet = opt.isSet("QUIET");
less.chopLongLines = opt.isSet("chop-long-lines");
less.ignoreCaseAlways = opt.isSet("IGNORE-CASE");
less.ignoreCaseCond = opt.isSet("ignore-case");
if (opt.isSet("tabs")) {
less.tabs = opt.getNumber("tabs");
}
less.printLineNumbers = opt.isSet("LINE-NUMBERS");
if (hasExtendedOptions) {
Less.class.getField("quitIfOneScreen").set(less, opt.isSet("quit-if-one-screen"));
Less.class.getField("noInit").set(less, opt.isSet("no-init"));
Less.class.getField("noKeypad").set(less, opt.isSet("no-keypad"));
}
less.run(sources);
}
use of java.nio.file.Files in project tezcRaft by tezc.
the class Cluster method openStores.
/**
* Open stores
*
* Called prior to open a cluster. This method reads cluster log from
* file storage if exists.
*
* @throws IOException on any IO error
*/
private void openStores() throws IOException {
List<Path> paths = Files.walk(path).filter(Files::isRegularFile).filter(p -> p.toString().endsWith(".store")).collect(Collectors.toList());
List<MappedStore> tmp = new ArrayList<>();
for (Path path : paths) {
try {
MappedStore store = new MappedStore(worker, path, storeSize);
/*
* This could happen on an race condition, store was created
* just before used first time. So, this store does not contain
* any data, we simply delete it.
*/
if (store.getBaseIndex() == -1) {
store.delete();
worker.logWarn("Inconsistent " + "log file is deleted", store.getPath());
continue;
}
tmp.add(store);
} catch (Exception e) {
worker.logError(e);
}
}
/*
* Store files should be sequential, if we detect files out of other,
* sequential ones will be kept, rest will be deleted. As we will start
* as follower, leader will send us the missing log anyway.
*/
tmp.sort(Comparator.comparingLong(MappedStore::getBaseIndex));
for (int i = 1; i < tmp.size(); i++) {
long base = tmp.get(i).getBaseIndex();
long prevEnd = tmp.get(i - 1).getEndIndex();
if (base != prevEnd + 1) {
List<MappedStore> sub = tmp.subList(i, tmp.size());
sub.forEach(store -> {
try {
store.delete();
worker.logWarn("Inconsistent " + "log file is deleted", store.getPath());
} catch (Exception e) {
worker.logError(e);
}
});
sub.clear();
break;
}
}
/*
* First log store must match with the snapshot index, if not, store
* files are inconsistent(in case of a race condition, manual log file
* deletion etc..)
*
* As we will start
* as follower, leader will send us the missing log anyway.
*/
if (tmp.size() > 0 && tmp.get(0).getBaseIndex() != snapshotIndex + 1) {
tmp.forEach(store -> {
try {
store.delete();
worker.logWarn("Inconsistent " + "log file is deleted", store.getPath());
} catch (Exception e) {
worker.logError(e);
}
});
tmp.clear();
}
stores.addAll(tmp);
}
use of java.nio.file.Files in project nifi by apache.
the class TestListFile method testFilterAge.
@Test
public void testFilterAge() throws Exception {
final File file1 = new File(TESTDIR + "/age1.txt");
assertTrue(file1.createNewFile());
final File file2 = new File(TESTDIR + "/age2.txt");
assertTrue(file2.createNewFile());
final File file3 = new File(TESTDIR + "/age3.txt");
assertTrue(file3.createNewFile());
final Function<Boolean, Object> runNext = resetAges -> {
if (resetAges) {
resetAges();
assertTrue(file1.setLastModified(time0millis));
assertTrue(file2.setLastModified(time2millis));
assertTrue(file3.setLastModified(time4millis));
}
try {
runNext();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return null;
};
// check all files
runner.setProperty(ListFile.DIRECTORY, testDir.getAbsolutePath());
runNext.apply(true);
runner.assertTransferCount(ListFile.REL_SUCCESS, 3);
// processor updates internal state, it shouldn't pick the same ones.
runNext.apply(false);
runner.assertTransferCount(ListFile.REL_SUCCESS, 0);
// exclude oldest
runner.setProperty(ListFile.MIN_AGE, age0);
runner.setProperty(ListFile.MAX_AGE, age3);
runNext.apply(true);
runner.assertAllFlowFilesTransferred(ListFile.REL_SUCCESS);
final List<MockFlowFile> successFiles2 = runner.getFlowFilesForRelationship(ListFile.REL_SUCCESS);
assertEquals(2, successFiles2.size());
assertEquals(file2.getName(), successFiles2.get(0).getAttribute("filename"));
assertEquals(file1.getName(), successFiles2.get(1).getAttribute("filename"));
// exclude newest
runner.setProperty(ListFile.MIN_AGE, age1);
runner.setProperty(ListFile.MAX_AGE, age5);
runNext.apply(true);
runner.assertAllFlowFilesTransferred(ListFile.REL_SUCCESS);
final List<MockFlowFile> successFiles3 = runner.getFlowFilesForRelationship(ListFile.REL_SUCCESS);
assertEquals(2, successFiles3.size());
assertEquals(file3.getName(), successFiles3.get(0).getAttribute("filename"));
assertEquals(file2.getName(), successFiles3.get(1).getAttribute("filename"));
// exclude oldest and newest
runner.setProperty(ListFile.MIN_AGE, age1);
runner.setProperty(ListFile.MAX_AGE, age3);
runNext.apply(true);
runner.assertAllFlowFilesTransferred(ListFile.REL_SUCCESS);
final List<MockFlowFile> successFiles4 = runner.getFlowFilesForRelationship(ListFile.REL_SUCCESS);
assertEquals(1, successFiles4.size());
assertEquals(file2.getName(), successFiles4.get(0).getAttribute("filename"));
}
Aggregations