1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender.rolling.helper;
18
19 import org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.status.StatusLogger;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.nio.channels.FileChannel;
27
28
29
30
31
32 public final class FileRenameAction extends AbstractAction {
33
34 private static final Logger LOGGER = StatusLogger.getLogger();
35
36
37
38
39 private final File source;
40
41
42
43
44 private final File destination;
45
46
47
48
49 private final boolean renameEmptyFiles;
50
51
52
53
54
55
56
57
58 public FileRenameAction(final File src, final File dst, boolean renameEmptyFiles) {
59 source = src;
60 destination = dst;
61 this.renameEmptyFiles = renameEmptyFiles;
62 }
63
64
65
66
67
68
69 @Override
70 public boolean execute() {
71 return execute(source, destination, renameEmptyFiles);
72 }
73
74
75
76
77
78
79
80
81
82 public static boolean execute(final File source, final File destination, boolean renameEmptyFiles) {
83 if (renameEmptyFiles || (source.length() > 0)) {
84 File parent = destination.getParentFile();
85 if (!parent.exists()) {
86 if (!parent.mkdirs()) {
87 LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
88 return false;
89 }
90 }
91 try {
92 if (!source.renameTo(destination)) {
93 try {
94 copyFile(source, destination);
95 return source.delete();
96 } catch (IOException iex) {
97 LOGGER.error("Unable to rename file {} to {} - {}", source.getAbsolutePath(),
98 destination.getAbsolutePath(), iex.getMessage());
99 }
100 }
101 return true;
102 } catch (Exception ex) {
103 try {
104 copyFile(source, destination);
105 return source.delete();
106 } catch (IOException iex) {
107 LOGGER.error("Unable to rename file {} to {} - {}", source.getAbsolutePath(),
108 destination.getAbsolutePath(), iex.getMessage());
109 }
110 }
111 }
112
113 return false;
114 }
115
116 private static void copyFile(final File source, final File destination) throws IOException {
117 if (!destination.exists()) {
118 destination.createNewFile();
119 }
120
121 FileChannel srcChannel = null;
122 FileChannel destChannel = null;
123 FileInputStream srcStream = null;
124 FileOutputStream destStream = null;
125 try {
126 srcStream = new FileInputStream(source);
127 destStream = new FileOutputStream(destination);
128 srcChannel = srcStream.getChannel();
129 destChannel = destStream.getChannel();
130 destChannel.transferFrom(srcChannel, 0, srcChannel.size());
131 } finally {
132 if (srcChannel != null) {
133 srcChannel.close();
134 }
135 if (srcStream != null) {
136 srcStream.close();
137 }
138 if (destChannel != null) {
139 destChannel.close();
140 }
141 if (destStream != null) {
142 destStream.close();
143 }
144 }
145 }
146 }