Skip to content

Commit 37e863d

Browse files
committed
修复忘记创建文件夹导致的文件移动失败问题
1 parent e5477d9 commit 37e863d

5 files changed

Lines changed: 80 additions & 17 deletions

File tree

src/main/java/com/github/balloonupdate/mcpatch/client/Main.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static boolean AppMain(boolean graphicsMode, StartMethod startMethod, boolean en
8989
InitConsoleLogging(graphicsMode, enableLogFile);
9090

9191
// 准备各种目录
92-
Path workDir = getWorkDirectory();
93-
Path progDir = getProgramDirectory(workDir);
92+
Path progDir = getProgramDirectory();
93+
Path workDir = getWorkDirectory(progDir);
9494
AppConfig config = new AppConfig(readConfig(progDir.resolve("mcpatch.yml")));
9595
Path baseDir = getUpdateDirectory(workDir, config);
9696

@@ -210,25 +210,48 @@ static boolean AppMain(boolean graphicsMode, StartMethod startMethod, boolean en
210210
/**
211211
* 获取Jar文件所在的目录
212212
*/
213-
static Path getProgramDirectory(Path workDir)
213+
static Path getProgramDirectory()
214214
{
215-
if (Env.isDevelopment())
216-
return workDir.resolve("test");
215+
if (Env.isDevelopment()) {
216+
String devWorkDir = System.getenv("MCPATCH_DEV_WORK_DIR");
217+
String devProgDir = System.getenv("MCPATCH_DEV_PROG_DIR");
218+
219+
// 优先用环境变量里的
220+
if (devWorkDir != null && devProgDir != null) {
221+
return Paths.get(devProgDir);
222+
}
223+
224+
// 然后用test文件夹
225+
Path userDir = Paths.get(System.getProperty("user.dir"));
226+
return userDir.resolve("test");
227+
}
217228

218229
return Env.getJarPath().getParent();
219230
}
220231

221232
/**
222233
* 获取进程的工作目录
223234
*/
224-
static Path getWorkDirectory() {
235+
static Path getWorkDirectory(Path progDir) {
225236
Path userDir = Paths.get(System.getProperty("user.dir"));
226237

227238
if (Env.isDevelopment()) {
228-
Path testDir = userDir.resolve("test");
239+
String devWorkDir = System.getenv("MCPATCH_DEV_WORK_DIR");
240+
String devProgDir = System.getenv("MCPATCH_DEV_PROG_DIR");
241+
242+
Path workDir;
243+
244+
// 优先用环境变量里的
245+
if (devWorkDir != null && devProgDir != null) {
246+
workDir = Paths.get(devWorkDir);
247+
} else {
248+
// 同程序文件夹
249+
workDir = progDir;
250+
}
229251

230252
try {
231-
Files.createDirectories(testDir);
253+
Files.createDirectories(workDir);
254+
return workDir;
232255
} catch (IOException e) {
233256
throw new RuntimeException(e);
234257
}
@@ -247,7 +270,7 @@ static Path getWorkDirectory() {
247270
static Path getUpdateDirectory(Path workDir, AppConfig config) throws McpatchBusinessException {
248271
// 开发环境下直接返回工作目录
249272
if (Env.isDevelopment())
250-
return workDir.resolve("test");
273+
return workDir;
251274

252275
// 如果填写了base-path,就使用
253276
if (!config.basePath.equals("")) {

src/main/java/com/github/balloonupdate/mcpatch/client/Work.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
272272
// 不能和别人的to冲突了
273273
RuntimeAssert.isTrue(!moveFiles.stream().anyMatch(e -> e.to.equals(op.to)));
274274

275-
// 更新下载路径
275+
// 修改下载目的地为新的路径
276276
find.get().path = op.to;
277-
277+
find.get().tempPath = tempDir.resolve(find.get().path + ".temp");
278278
} else {
279279
// 不能和别人的from或者to冲突了
280280
RuntimeAssert.isTrue(!moveFiles.stream().anyMatch(e -> e.from.equals(op.from) || e.to.equals(op.to)));
@@ -324,7 +324,6 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
324324
break;
325325

326326
TempUpdateFile f = updateFiles.get(i);
327-
328327
Path targetPath = baseDir.resolve(f.path);
329328

330329
// 检查一下看能不能跳过下载
@@ -343,11 +342,12 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
343342
}
344343

345344
// 2.判断文件时间
346-
long timeDiff = (mtime.toMillis() / 1000 - f.modified);
345+
long timeDiff = Math.abs(mtime.toMillis() / 1000 - f.modified);
347346

348347
// Log.debug(f.path + " : " + timeDiff);
349348

350-
if (timeDiff < 5) {
349+
// 超过5秒视作是不同文件
350+
if (timeDiff > 5) {
351351
continue;
352352
}
353353

@@ -427,7 +427,8 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
427427
for (TempUpdateFile f : updateFiles) {
428428
String filename = PathUtility.getFilename(f.path);
429429

430-
Log.debug(" a.开始下载 " + f.tempPath);
430+
Log.debug(" a.开始下载 " + f.path);
431+
Log.debug(" Download " + f.tempPath);
431432

432433
Path tempDirectory = f.tempPath.getParent();
433434
Files.createDirectories(tempDirectory);
@@ -504,6 +505,8 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
504505

505506
Path path = baseDir.resolve(f);
506507

508+
Log.debug(" MKDIR " + path);
509+
507510
Files.createDirectories(path);
508511
}
509512

@@ -520,6 +523,9 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
520523
Path from = baseDir.resolve(move.from);
521524
Path to = baseDir.resolve(move.to);
522525

526+
Log.debug(" From " + from);
527+
Log.debug(" To " + to);
528+
523529
if (Files.exists(from)) {
524530
Files.move(from, to);
525531
}
@@ -551,9 +557,28 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
551557
Path from = f.tempPath;
552558
Path to = baseDir.resolve(f.path);
553559

554-
Log.debug(String.format(" e.移动临时文件 %s.temp => %s", f.path, f.path));
560+
Log.debug(String.format(" e.移动临时文件 %s", f.path));
561+
Log.debug(String.format(" From %s", from));
562+
Log.debug(String.format(" To %s", to));
563+
564+
if (Files.exists(to)) {
565+
Log.debug(" 目标文件存在,先进行删除");
566+
PathUtility.delete(to);
567+
}
568+
569+
if (!Files.exists(from.getParent())) {
570+
throw new McpatchBusinessException("移动临时文件时,源文件上级目录不存在: " + from.getParent());
571+
}
572+
573+
Files.createDirectories(to.getParent());
555574

556-
PathUtility.delete(to);
575+
if (!Files.exists(to.getParent())) {
576+
throw new McpatchBusinessException("移动临时文件时,目标文件上级目录不存在: " + to.getParent());
577+
}
578+
579+
if (!Files.exists(from)) {
580+
throw new McpatchBusinessException("移动临时文件时,要被移动的源文件不存在: " + from);
581+
}
557582

558583
Files.move(from, to);
559584
}

src/main/java/com/github/balloonupdate/mcpatch/client/data/TempMoveFile.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public TempMoveFile(String from, String to) {
1414
this.from = from;
1515
this.to = to;
1616
}
17+
18+
@Override
19+
public String toString() {
20+
return from + " => " + to;
21+
}
1722
}

src/main/java/com/github/balloonupdate/mcpatch/client/data/TempUpdateFile.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public TempUpdateFile(String containerName, String label, FileChange.UpdateFile
5858

5959
this.tempPath = tempPath;
6060
}
61+
62+
@Override
63+
public String toString() {
64+
return path + " (" + label + ")";
65+
}
6166
}

src/main/java/com/github/balloonupdate/mcpatch/client/data/TempVersionMeta.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public TempVersionMeta(String filename, VersionMeta metadata) {
1818
this.filename = filename;
1919
this.metadata = metadata;
2020
}
21+
22+
@Override
23+
public String toString() {
24+
return metadata.label + "(" + metadata.changes.size() + " changes)";
25+
}
2126
}

0 commit comments

Comments
 (0)