Skip to content

Commit 2c5a455

Browse files
committed
Merge pull request #14 from gluster/BZ916371
fix to make mkdirs(..) tolerant of existing directories during a distrib...
2 parents 36f4029 + 2bc10f6 commit 2c5a455

File tree

1 file changed

+27
-40
lines changed

1 file changed

+27
-40
lines changed

src/main/java/org/apache/hadoop/fs/glusterfs/GlusterFileSystem.java

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
* This package provides interface for hadoop jobs (incl. Map/Reduce)
4747
* to access files in GlusterFS backed file system via FUSE mount
4848
*/
49+
50+
51+
/*
52+
*
53+
* TODO: Evaluate LocalFileSystem and RawLocalFileSystem as possible delegate file systems to remove & refactor this code.
54+
*
55+
*/
4956
public class GlusterFileSystem extends FileSystem {
5057

5158
private FileSystem glusterFs = null;
@@ -180,19 +187,21 @@ public boolean exists(Path path) throws IOException {
180187
return f.exists();
181188
}
182189

183-
public boolean mkdirs(Path path, FsPermission permission)
184-
throws IOException {
185-
boolean created = false;
186-
Path absolute = makeAbsolute(path);
187-
File f = new File(absolute.toUri().getPath());
188-
189-
if (f.exists()) {
190-
System.out.println("Directory " + f.getPath() + " already exist");
191-
return true;
192-
}
193-
194-
return f.mkdirs();
195-
}
190+
/*
191+
* Code copied from:
192+
* @see org.apache.hadoop.fs.RawLocalFileSystem#mkdirs(org.apache.hadoop.fs.Path)
193+
* as incremental fix towards a re-write. of this class to remove duplicity.
194+
*
195+
*/
196+
public boolean mkdirs(Path f, FsPermission permission) throws IOException {
197+
198+
if(f==null) return true;
199+
200+
Path parent = f.getParent();
201+
Path absolute = makeAbsolute(f);
202+
File p2f = new File(absolute.toUri().getPath());
203+
return (f == null || mkdirs(parent)) && (p2f.mkdir() || p2f.isDirectory());
204+
}
196205

197206
@Deprecated
198207
public boolean isDirectory(Path path) throws IOException {
@@ -293,9 +302,10 @@ public String getOwner() {
293302
* is an instance of OutputStream class.
294303
*/
295304
public FSDataOutputStream create(Path path, FsPermission permission,
296-
boolean overwrite, int bufferSize, short replication,
305+
boolean overwrite, int bufferSize, short replication,
297306
long blockSize, Progressable progress) throws IOException {
298-
Path absolute = makeAbsolute(path);
307+
308+
Path absolute = makeAbsolute(path);
299309
Path parent = null;
300310
File f = null;
301311
File fParent = null;
@@ -311,32 +321,9 @@ public FSDataOutputStream create(Path path, FsPermission permission,
311321
}
312322

313323
parent = path.getParent();
314-
fParent = new File((makeAbsolute(parent)).toUri().getPath());
315-
if ((parent != null) && (fParent != null) && (!fParent.exists())) {
316-
if (!fParent.mkdirs()) {
317-
//
318-
// File.mkdirs() is not multi-process safe. It is possible for
319-
// a peer who is running mkdirs() to cause us to fail. In such
320-
// a case, a rudimentary test is to try our exists() test for a
321-
// second time. The isDirectory() protects us from exists()
322-
// passing when a file is put in place of the directory we were
323-
// trying to create. We can be fooled by a directory, or set of
324-
// directories in the path, being owned by another user or with
325-
// incompatible permissions.
326-
//
327-
// This could be slightly improved to retry the mkdirs(), which
328-
// would cover races deep within the fParent's path. Each
329-
// iteration will address one race.
330-
//
331-
if (!fParent.exists() || !fParent.isDirectory()) {
332-
throw new IOException("cannot create parent directory: "
333-
+ fParent.getPath());
334-
}
335-
}
336-
}
324+
mkdirs(parent);
337325

338-
glusterFileStream = new FSDataOutputStream(new GlusterFUSEOutputStream(
339-
f.getPath(), false));
326+
glusterFileStream = new FSDataOutputStream(new GlusterFUSEOutputStream(f.getPath(), false));
340327

341328
return glusterFileStream;
342329
}

0 commit comments

Comments
 (0)