@@ -78,84 +78,34 @@ public static boolean exists(File... files) {
78
78
}
79
79
80
80
/**
81
- * Deletes the given files recursively. if <tt>deleteRoots</tt> is set to <code>true</code>
82
- * the given root files will be deleted as well. Otherwise only their content is deleted.
81
+ * Returns an array of {@link Path} build from the correspondent element
82
+ * in the input array using {@link java.io.File#toPath()}
83
+ * @param files the files to get paths for
83
84
*/
84
- public static boolean deleteRecursively (File [] roots , boolean deleteRoots ) {
85
-
86
- boolean deleted = true ;
87
- for (File root : roots ) {
88
- deleted &= deleteRecursively (root , deleteRoots );
85
+ public static Path [] toPaths (File ... files ) {
86
+ Path [] paths = new Path [files .length ];
87
+ for (int i = 0 ; i < files .length ; i ++) {
88
+ paths [i ] = files [i ].toPath ();
89
89
}
90
- return deleted ;
90
+ return paths ;
91
91
}
92
92
93
93
/**
94
- * Deletes all subdirectories of the given roots recursively.
94
+ * Deletes all subdirectories in the given path recursively
95
+ * @throws java.lang.IllegalArgumentException if the given path is not a directory
95
96
*/
96
- public static boolean deleteSubDirectories (File [] roots ) {
97
-
98
- boolean deleted = true ;
99
- for (File root : roots ) {
100
- if (root .isDirectory ()) {
101
- File [] files = root .listFiles (new FileFilter () {
102
- @ Override
103
- public boolean accept (File pathname ) {
104
- return pathname .isDirectory ();
105
- }
106
- });
107
- deleted &= deleteRecursively (files , true );
108
- }
109
-
110
- }
111
- return deleted ;
112
- }
113
-
114
- /**
115
- * Deletes the given files recursively including the given roots.
116
- */
117
- public static boolean deleteRecursively (File ... roots ) {
118
- return deleteRecursively (roots , true );
119
- }
120
-
121
- /**
122
- * Delete the supplied {@link java.io.File} - for directories,
123
- * recursively delete any nested directories or files as well.
124
- *
125
- * @param root the root <code>File</code> to delete
126
- * @param deleteRoot whether or not to delete the root itself or just the content of the root.
127
- * @return <code>true</code> if the <code>File</code> was deleted,
128
- * otherwise <code>false</code>
129
- */
130
- public static boolean deleteRecursively (File root , boolean deleteRoot ) {
131
- if (root != null && root .exists ()) {
132
- if (root .isDirectory ()) {
133
- File [] children = root .listFiles ();
134
- if (children != null ) {
135
- for (File aChildren : children ) {
136
- deleteRecursively (aChildren , true );
97
+ public static void deleteSubDirectories (Path ... paths ) throws IOException {
98
+ for (Path path : paths ) {
99
+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (path )) {
100
+ for (Path subPath : stream ) {
101
+ if (Files .isDirectory (subPath )) {
102
+ IOUtils .rm (subPath );
137
103
}
138
104
}
139
105
}
140
-
141
- if (deleteRoot ) {
142
- return root .delete ();
143
- } else {
144
- return true ;
145
- }
146
106
}
147
- return false ;
148
107
}
149
108
150
- /**
151
- * Ensure that any writes to the given file is written to the storage device that contains it.
152
- * @param fileToSync the file to fsync
153
- * @param isDir if true, the given file is a directory (we open for read and ignore IOExceptions,
154
- * because not all file systems and operating systems allow to fsync on a directory)
155
- */
156
- public static void syncFile (File fileToSync , boolean isDir ) throws IOException {
157
- IOUtils .fsync (fileToSync .toPath (), isDir );
158
- }
159
109
160
110
/**
161
111
* Check that a directory exists, is a directory and is readable
@@ -181,11 +131,19 @@ public static boolean isAccessibleDirectory(File directory, ESLogger logger) {
181
131
182
132
private FileSystemUtils () {}
183
133
184
- public static void tryDeleteFile (File file ) {
185
- try {
186
- file .delete ();
187
- } catch (SecurityException e1 ) {
188
- // ignore
134
+ /**
135
+ * Temporary solution until LUCENE-6051 is fixed
136
+ * @see org.apache.lucene.util.IOUtils#deleteFilesIgnoringExceptions(java.nio.file.Path...)
137
+ */
138
+ public static void deleteFilesIgnoringExceptions (Path ... files ) {
139
+ for (Path name : files ) {
140
+ if (name != null ) {
141
+ try {
142
+ Files .delete (name );
143
+ } catch (Throwable ignored ) {
144
+ // ignore
145
+ }
146
+ }
189
147
}
190
148
}
191
149
0 commit comments