Skip to content

Commit 956570f

Browse files
committed
HADOOP-9155. FsPermission should have different default value, 777 for directory and 666 for file. Contributed by Binglin Chang.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1431148 13f79535-47bb-0310-9956-ffa450edef68
1 parent cff4a1a commit 956570f

File tree

13 files changed

+88
-22
lines changed

13 files changed

+88
-22
lines changed

hadoop-common-project/hadoop-common/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ Release 2.0.3-alpha - Unreleased
525525
HADOOP-9181. Set daemon flag for HttpServer's QueuedThreadPool.
526526
(Liang Xie via suresh)
527527

528+
HADOOP-9155. FsPermission should have different default value, 777 for
529+
directory and 666 for file. (Binglin Chang via atm)
530+
528531
Release 2.0.2-alpha - 2012-09-07
529532

530533
INCOMPATIBLE CHANGES

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileContext.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,25 @@
172172
public final class FileContext {
173173

174174
public static final Log LOG = LogFactory.getLog(FileContext.class);
175+
/**
176+
* Default permission for directory and symlink
177+
* In previous versions, this default permission was also used to
178+
* create files, so files created end up with ugo+x permission.
179+
* See HADOOP-9155 for detail.
180+
* Two new constants are added to solve this, please use
181+
* {@link FileContext#DIR_DEFAULT_PERM} for directory, and use
182+
* {@link FileContext#FILE_DEFAULT_PERM} for file.
183+
* This constant is kept for compatibility.
184+
*/
175185
public static final FsPermission DEFAULT_PERM = FsPermission.getDefault();
186+
/**
187+
* Default permission for directory
188+
*/
189+
public static final FsPermission DIR_DEFAULT_PERM = FsPermission.getDirDefault();
190+
/**
191+
* Default permission for file
192+
*/
193+
public static final FsPermission FILE_DEFAULT_PERM = FsPermission.getFileDefault();
176194

177195
/**
178196
* Priority of the FileContext shutdown hook.
@@ -656,7 +674,7 @@ public FSDataOutputStream create(final Path f,
656674
CreateOpts.Perms permOpt =
657675
(CreateOpts.Perms) CreateOpts.getOpt(CreateOpts.Perms.class, opts);
658676
FsPermission permission = (permOpt != null) ? permOpt.getValue() :
659-
FsPermission.getDefault();
677+
FILE_DEFAULT_PERM;
660678
permission = permission.applyUMask(umask);
661679

662680
final CreateOpts[] updatedOpts =
@@ -704,7 +722,7 @@ public void mkdir(final Path dir, final FsPermission permission,
704722
IOException {
705723
final Path absDir = fixRelativePart(dir);
706724
final FsPermission absFerms = (permission == null ?
707-
FsPermission.getDefault() : permission).applyUMask(umask);
725+
FsPermission.getDirDefault() : permission).applyUMask(umask);
708726
new FSLinkResolver<Void>() {
709727
@Override
710728
public Void next(final AbstractFileSystem fs, final Path p)
@@ -2157,7 +2175,7 @@ public boolean copy(final Path src, final Path dst, boolean deleteSource,
21572175
FileStatus fs = FileContext.this.getFileStatus(qSrc);
21582176
if (fs.isDirectory()) {
21592177
checkDependencies(qSrc, qDst);
2160-
mkdir(qDst, FsPermission.getDefault(), true);
2178+
mkdir(qDst, FsPermission.getDirDefault(), true);
21612179
FileStatus[] contents = listStatus(qSrc);
21622180
for (FileStatus content : contents) {
21632181
copy(makeQualified(content.getPath()), makeQualified(new Path(qDst,

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileStatus.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,15 @@ public FileStatus(long length, boolean isdir,
7979
this.blocksize = blocksize;
8080
this.modification_time = modification_time;
8181
this.access_time = access_time;
82-
this.permission = (permission == null) ?
83-
FsPermission.getDefault() : permission;
82+
if (permission != null) {
83+
this.permission = permission;
84+
} else if (isdir) {
85+
this.permission = FsPermission.getDirDefault();
86+
} else if (symlink!=null) {
87+
this.permission = FsPermission.getDefault();
88+
} else {
89+
this.permission = FsPermission.getFileDefault();
90+
}
8491
this.owner = (owner == null) ? "" : owner;
8592
this.group = (group == null) ? "" : group;
8693
this.symlink = symlink;
@@ -217,7 +224,7 @@ public void setPath(final Path p) {
217224
*/
218225
protected void setPermission(FsPermission permission) {
219226
this.permission = (permission == null) ?
220-
FsPermission.getDefault() : permission;
227+
FsPermission.getFileDefault() : permission;
221228
}
222229

223230
/**

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ public FSDataOutputStream create(Path f,
850850
long blockSize,
851851
Progressable progress
852852
) throws IOException {
853-
return this.create(f, FsPermission.getDefault().applyUMask(
853+
return this.create(f, FsPermission.getFileDefault().applyUMask(
854854
FsPermission.getUMask(getConf())), overwrite, bufferSize,
855855
replication, blockSize, progress);
856856
}
@@ -1030,7 +1030,7 @@ public FSDataOutputStream createNonRecursive(Path f,
10301030
boolean overwrite,
10311031
int bufferSize, short replication, long blockSize,
10321032
Progressable progress) throws IOException {
1033-
return this.createNonRecursive(f, FsPermission.getDefault(),
1033+
return this.createNonRecursive(f, FsPermission.getFileDefault(),
10341034
overwrite, bufferSize, replication, blockSize, progress);
10351035
}
10361036

@@ -1866,7 +1866,7 @@ protected Path getInitialWorkingDirectory() {
18661866
* Call {@link #mkdirs(Path, FsPermission)} with default permission.
18671867
*/
18681868
public boolean mkdirs(Path f) throws IOException {
1869-
return mkdirs(f, FsPermission.getDefault());
1869+
return mkdirs(f, FsPermission.getDirDefault());
18701870
}
18711871

18721872
/**

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ftp/FTPFileSystem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public FSDataOutputStream create(Path file, FsPermission permission,
224224
}
225225

226226
Path parent = absolute.getParent();
227-
if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
227+
if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
228228
parent = (parent == null) ? new Path("/") : parent;
229229
disconnect(client);
230230
throw new IOException("create(): Mkdirs failed to create: " + parent);
@@ -484,7 +484,7 @@ private boolean mkdirs(FTPClient client, Path file, FsPermission permission)
484484
if (!exists(client, absolute)) {
485485
Path parent = absolute.getParent();
486486
created = (parent == null || mkdirs(client, parent, FsPermission
487-
.getDefault()));
487+
.getDirDefault()));
488488
if (created) {
489489
String parentDir = parent.toUri().getPath();
490490
client.changeWorkingDirectory(parentDir);

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/local/RawLocalFs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void createSymlink(Path target, Path link, boolean createParent)
8585
"system: "+target.toString());
8686
}
8787
if (createParent) {
88-
mkdir(link.getParent(), FsPermission.getDefault(), true);
88+
mkdir(link.getParent(), FsPermission.getDirDefault(), true);
8989
}
9090
// NB: Use createSymbolicLink in java.nio.file.Path once available
9191
try {

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/permission/FsPermission.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,34 @@ public static void setUMask(Configuration conf, FsPermission umask) {
275275
conf.setInt(DEPRECATED_UMASK_LABEL, umask.toShort());
276276
}
277277

278-
/** Get the default permission. */
278+
/**
279+
* Get the default permission for directory and symlink.
280+
* In previous versions, this default permission was also used to
281+
* create files, so files created end up with ugo+x permission.
282+
* See HADOOP-9155 for detail.
283+
* Two new methods are added to solve this, please use
284+
* {@link FsPermission#getDirDefault()} for directory, and use
285+
* {@link FsPermission#getFileDefault()} for file.
286+
* This method is kept for compatibility.
287+
*/
279288
public static FsPermission getDefault() {
280289
return new FsPermission((short)00777);
281290
}
282291

292+
/**
293+
* Get the default permission for directory.
294+
*/
295+
public static FsPermission getDirDefault() {
296+
return new FsPermission((short)00777);
297+
}
298+
299+
/**
300+
* Get the default permission for file.
301+
*/
302+
public static FsPermission getFileDefault() {
303+
return new FsPermission((short)00666);
304+
}
305+
283306
/**
284307
* Create a FsPermission from a Unix symbolic permission string
285308
* @param unixSymbolicPermission e.g. "-rw-rw-rw-"

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/FileContextPermissionBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testCreatePermission() throws IOException {
9595
String filename = "foo";
9696
Path f = getTestRootPath(fc, filename);
9797
createFile(fc, filename);
98-
doFilePermissionCheck(FileContext.DEFAULT_PERM.applyUMask(fc.getUMask()),
98+
doFilePermissionCheck(FileContext.FILE_DEFAULT_PERM.applyUMask(fc.getUMask()),
9999
fc.getFileStatus(f).getPermission());
100100
}
101101

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void constructorNoOwner() throws IOException {
121121
FileStatus fileStatus = new FileStatus(LENGTH, isdir,
122122
REPLICATION, BLKSIZE, MTIME, PATH);
123123
validateAccessors(fileStatus, LENGTH, isdir, REPLICATION, BLKSIZE, MTIME,
124-
0, FsPermission.getDefault(), "", "", null, PATH);
124+
0, FsPermission.getDirDefault(), "", "", null, PATH);
125125
}
126126

127127
/**
@@ -131,7 +131,7 @@ public void constructorNoOwner() throws IOException {
131131
public void constructorBlank() throws IOException {
132132
FileStatus fileStatus = new FileStatus();
133133
validateAccessors(fileStatus, 0, false, 0, 0, 0,
134-
0, FsPermission.getDefault(), "", "", null, null);
134+
0, FsPermission.getFileDefault(), "", "", null, null);
135135
}
136136

137137
/**

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalFSFileContextMainOperations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.junit.Assert;
2525
import org.junit.Before;
2626
import org.junit.Test;
27+
import org.apache.hadoop.fs.FileContextTestHelper;
28+
import org.apache.hadoop.fs.permission.FsPermission;
2729

2830
public class TestLocalFSFileContextMainOperations extends FileContextMainOperationsBaseTest {
2931

@@ -47,4 +49,14 @@ public void testFileContextNoCache() throws UnsupportedFileSystemException {
4749
FileContext fc1 = FileContext.getLocalFSFileContext();
4850
Assert.assertTrue(fc1 != fc);
4951
}
52+
53+
@Test
54+
public void testDefaultFilePermission() throws IOException {
55+
Path file = FileContextTestHelper.getTestRootPath(fc,
56+
"testDefaultFilePermission");
57+
FileContextTestHelper.createFile(fc, file);
58+
FsPermission expect = FileContext.FILE_DEFAULT_PERM.applyUMask(fc.getUMask());
59+
Assert.assertEquals(expect, fc.getFileStatus(file)
60+
.getPermission());
61+
}
5062
}

0 commit comments

Comments
 (0)