Skip to content

Commit 9d20c8a

Browse files
committed
Merge pull request #27 from gluster/permissions_merged_2
Permissions patch to fix BZ_951305
2 parents 763688a + c2a9c14 commit 9d20c8a

File tree

5 files changed

+124
-126
lines changed

5 files changed

+124
-126
lines changed

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

Lines changed: 0 additions & 73 deletions
This file was deleted.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
package org.apache.hadoop.fs.glusterfs;
2121

2222
import java.io.*;
23-
23+
/**
24+
* An OutputStream for writing to a FUSE mount intended for use with gluster.
25+
*/
2426
public class GlusterFUSEOutputStream extends OutputStream{
2527
File f;
2628
long pos;
2729
boolean closed;
2830
OutputStream fuseOutputStream;
2931
org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(GlusterFUSEOutputStream.class);
30-
32+
3133
public GlusterFUSEOutputStream(String file, boolean append) throws IOException{
3234
this(file,append,0);
3335
}
@@ -43,7 +45,6 @@ public GlusterFUSEOutputStream(String file, boolean append, int bufferSize) thro
4345
fuseOutputStream = new BufferedOutputStream(fuseOutputStream, bufferSize);
4446
this.closed=false;
4547
}
46-
4748
public long getPos() throws IOException{
4849
return pos;
4950
}

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

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,24 @@ public boolean exists(Path path) throws IOException{
203203
* org.apache.hadoop.fs.RawLocalFileSystem#mkdirs(org.apache.hadoop.fs.Path)
204204
* as incremental fix towards a re-write. of this class to remove duplicity.
205205
*/
206-
public boolean mkdirs(Path f,FsPermission permission) throws IOException{
207-
208-
if(f==null)
209-
return true;
210-
211-
Path parent=f.getParent();
212-
Path absolute=makeAbsolute(f);
213-
File p2f=new File(absolute.toUri().getPath());
214-
return (f==null||mkdirs(parent))&&(p2f.mkdir()||p2f.isDirectory());
206+
public boolean mkdirs(Path path,FsPermission permission) throws IOException{
207+
208+
String split[]=path.toString().split(Path.SEPARATOR);
209+
String current="";
210+
boolean success=true;
211+
for(int i=0;i<split.length&&success;i++){
212+
current+=split[i]+Path.SEPARATOR;
213+
Path absolute=makeAbsolute(new Path(current));
214+
File p2f=new File(absolute.toUri().getPath());
215+
if(!p2f.exists()){
216+
p2f.mkdirs();
217+
setPermission(new Path(current), permission);
218+
}
219+
success=p2f.exists();
220+
}
221+
222+
return success;
223+
215224
}
216225

217226
@Deprecated
@@ -298,14 +307,18 @@ public FUSEFileStatus(File f, boolean isdir, int block_replication, long blocksi
298307
*/
299308
@Override
300309
public String getOwner(){
301-
try{
302-
return FileInfoUtil.getLSinfo(theFile.getAbsolutePath()).get("owner");
303-
}catch (Exception e){
304-
throw new RuntimeException(e);
305-
}
310+
loadPermissionInfo();
311+
return super.getOwner();
306312
}
313+
314+
315+
@Override
316+
public String getGroup() {
317+
loadPermissionInfo();
318+
return super.getGroup();
319+
}
307320

308-
public FsPermission getPermission(){
321+
public FsPermission getPermission(){
309322
// should be amortized, see method.
310323
loadPermissionInfo();
311324
return super.getPermission();
@@ -322,7 +335,6 @@ private void loadPermissionInfo(){
322335
try{
323336
String output;
324337
StringTokenizer t=new StringTokenizer(output=execCommand(theFile, Shell.getGET_PERMISSION_COMMAND()));
325-
326338
// log.info("Output of PERMISSION command = " + output
327339
// + " for " + this.getPath());
328340
// expected format
@@ -371,6 +383,9 @@ public static String execCommand(File f,String...cmd) throws IOException{
371383
*/
372384
@Override
373385
public void setPermission(Path p,FsPermission permission){
386+
387+
if(permission==null) return;
388+
374389
try{
375390
Path absolute=makeAbsolute(p);
376391
final File f=new File(absolute.toUri().getPath());
@@ -409,7 +424,7 @@ public FSDataOutputStream create(Path path,FsPermission permission,boolean overw
409424
Path absolute=makeAbsolute(path);
410425
Path parent=null;
411426
File f=null;
412-
File fParent=null;
427+
413428
FSDataOutputStream glusterFileStream=null;
414429

415430
f=new File(absolute.toUri().getPath());
@@ -422,7 +437,10 @@ public FSDataOutputStream create(Path path,FsPermission permission,boolean overw
422437
}
423438

424439
parent=path.getParent();
425-
mkdirs(parent);
440+
mkdirs(parent, permission);
441+
442+
f.createNewFile();
443+
setPermission(path, permission);
426444

427445
glusterFileStream=new FSDataOutputStream(new GlusterFUSEOutputStream(f.getPath(), false, writeBufferSize));
428446

@@ -494,7 +512,7 @@ public boolean delete(Path path,boolean recursive) throws IOException{
494512
if(dirEntries!=null)
495513
for(int i=0;i<dirEntries.length;i++)
496514
delete(new Path(absolute, dirEntries[i].getPath()), recursive);
497-
515+
498516
return f.delete();
499517
}
500518

@@ -562,8 +580,31 @@ public BlockLocation[] getFileBlockLocations(FileStatus file,long start,long len
562580
return result;
563581
}
564582

583+
/**
584+
* Adopted from {@link org.apache.hadoop.RawLocalFileSystem}, so that group privileges are
585+
* set properly when hadoop fs chwon is called in {@link org.apache.hadoop.fs.FSShellPermissions}.
586+
*/
587+
@Override
588+
public void setOwner(Path p, String username, String groupname) throws IOException {
589+
Path absolute=makeAbsolute(p);
590+
File f=new File(absolute.toUri().getPath());
591+
592+
if (username == null && groupname == null) {
593+
throw new IOException("username == null && groupname == null");
594+
}
595+
596+
if (username == null) {
597+
execCommand(f, Shell.SET_GROUP_COMMAND, groupname);
598+
}
599+
else {
600+
//OWNER[:[GROUP]]
601+
String s = username + (groupname == null? "": ":" + groupname);
602+
execCommand(f, Shell.SET_OWNER_COMMAND, s);
603+
}
604+
}
605+
565606
public void copyFromLocalFile(boolean delSrc,Path src,Path dst) throws IOException{
566-
FileUtil.copy(glusterFs, src, this, dst, delSrc, getConf());
607+
FileUtil.copy(glusterFs, src, this, dst, delSrc, getConf());
567608
}
568609

569610
public void copyToLocalFile(boolean delSrc,Path src,Path dst) throws IOException{

src/test/java/org/gluster/test/TestFileInfo.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/test/java/org/gluster/test/TestGluster.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void testTolerantMkdirs() throws Exception{
154154

155155
/* delete the directories */
156156

157-
gfs.delete(longPath);
157+
gfs.delete(new Path("a"), true);
158158
assertFalse(gfs.exists(longPath));
159159

160160
}
@@ -168,8 +168,8 @@ public void testOwner() throws Exception{
168168
final String me=System.getProperties().getProperty("user.name");
169169
Path myFile=new Path("to_owned_by_me.txt");
170170
gfs.create(myFile);
171-
System.out.println("Asserting that "+myFile+" is owned by "+me);
172171
Assert.assertEquals(gfs.getFileStatus(myFile).getOwner(), me);
172+
gfs.delete(myFile);
173173
}
174174

175175
@org.junit.Test
@@ -196,6 +196,61 @@ public void testTextWriteAndRead() throws Exception{
196196
assertFalse(gfs.exists(new Path("test1")));
197197
}
198198

199+
@Test
200+
public void testGroupOwnership() throws Exception{
201+
Path myFile=new Path("filePerm.txt");
202+
//Create a file
203+
gfs.create(myFile);
204+
205+
//Set the initial owner
206+
gfs.setOwner(myFile, "daemon", "root");
207+
String oldOwner = gfs.getFileStatus(myFile).getOwner();
208+
String oldGroup = gfs.getFileStatus(myFile).getGroup();
209+
Assert.assertEquals("daemon",oldOwner);
210+
Assert.assertEquals("root",oldGroup);
211+
212+
//Now, change it to "root" "wheel"
213+
gfs.setOwner(myFile, "root", "wheel");
214+
String newOwner = gfs.getFileStatus(myFile).getOwner();
215+
String newGroup = gfs.getFileStatus(myFile).getGroup();
216+
Assert.assertEquals("root",newOwner);
217+
Assert.assertEquals("wheel",newGroup);
218+
219+
gfs.delete(myFile,true);
220+
}
221+
222+
@org.junit.Test
223+
public void testPermissions() throws Exception{
224+
225+
Path myFile=new Path("filePerm.txt");
226+
gfs.create(myFile);
227+
short perm=0777;
228+
gfs.setPermission(myFile, new FsPermission(perm));
229+
assertEquals(gfs.getFileStatus(myFile).getPermission().toShort(), perm);
230+
231+
perm=0700;
232+
gfs.setPermission(myFile, new FsPermission(perm));
233+
assertEquals(gfs.getFileStatus(myFile).getPermission().toShort(), perm);
234+
235+
gfs.delete(myFile);
236+
assertFalse(gfs.exists(myFile));
237+
238+
/* directory permissions */
239+
Path directory = new Path("aa/bb/cc");
240+
perm = 0700;
241+
gfs.mkdirs(directory, new FsPermission(perm));
242+
assertEquals(gfs.getFileStatus(directory).getPermission().toShort(), perm);
243+
gfs.delete(new Path("aa"),true);
244+
assertFalse(gfs.exists(directory));
245+
246+
247+
perm = 0777;
248+
gfs.mkdirs(directory, new FsPermission(perm));
249+
assertEquals(gfs.getFileStatus(directory).getPermission().toShort(), perm);
250+
gfs.delete(new Path("aa"),true);
251+
assertFalse(gfs.exists(directory));
252+
}
253+
199254
@org.junit.Test
200255
public void testZDirs() throws Exception{
201256
final Path subDir1=new Path("td_dir.1");
@@ -378,5 +433,7 @@ public void test0aPermissions() throws Exception{
378433
Assert.assertTrue(this.gfs.getFileStatus(theFile).getPermission().getGroupAction().equals(changeTo.getGroupAction()));
379434
Assert.assertTrue(this.gfs.getFileStatus(theFile).getPermission().getUserAction().equals(changeTo.getUserAction()));
380435
Assert.assertTrue(this.gfs.getFileStatus(theFile).getPermission().getOtherAction().equals(changeTo.getOtherAction()));
436+
gfs.delete(new Path("mnt"),true);
437+
381438
}
382439
}

0 commit comments

Comments
 (0)