List of usage examples for org.apache.hadoop.fs FileStatus getOwner
public String getOwner()
From source file:com.amintor.hdfs.client.kerberizedhdfsclient.KerberizedHDFSClient.java
/** * @param args the command line arguments *///ww w. java 2 s . c o m public static void main(String[] args) { try { Configuration conf = new Configuration(); conf.addResource(new FileInputStream(HDFS_SITE_LOCATION)); conf.addResource(new FileInputStream(CORE_SITE_LOCATION)); String authType = conf.get("hadoop.security.authentication"); System.out.println("Authentication Type:" + authType); if (authType.trim().equalsIgnoreCase("kerberos")) { // Login through UGI keytab UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab("vijay", "/Users/vsingh/Software/vijay.keytab"); FileSystem hdFS = FileSystem.get(conf); FileStatus[] listStatus = hdFS.listStatus(new Path(args[0])); for (FileStatus statusFile : listStatus) { System.out.print("Replication:" + statusFile.getReplication() + "\t"); System.out.print("Owner:" + statusFile.getOwner() + "\t"); System.out.print("Group:" + statusFile.getGroup() + "\t"); System.out.println("Path:" + statusFile.getPath() + "\t"); } } } catch (IOException ex) { Logger.getLogger(KerberizedHDFSClient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.OwnerHandler.java
License:Apache License
@Override public Owner get(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus) { Owner owner = new Owner(); String domain = NFSUtils.getDomain(session.getConfiguration(), session.getClientAddress()); owner.setOwner(fileStatus.getOwner() + "@" + domain); return owner; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.OwnerHandler.java
License:Apache License
@Override public boolean set(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus, StateID stateID, Owner attr) throws IOException { String user = removeDomain(attr.getOwner()); if (fileStatus.getOwner().equals(user)) { fs.setOwner(fileStatus.getPath(), user, null); return true; }/*from ww w .ja v a 2 s. c om*/ return false; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.ACCESSHandler.java
License:Apache License
@Override protected ACCESSResponse doHandle(NFS4Handler server, Session session, ACCESSRequest request) throws NFS4Exception { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }// w w w .j av a2 s .co m CompoundRequest compoundRequest = session.getCompoundRequest(); AuthenticatedCredentials creds = compoundRequest.getCredentials(); Path path = server.getPath(session.getCurrentFileHandle()); try { UserIDMapper mapper = UserIDMapper.get(session.getConfiguration()); String user = mapper.getUserForUID(creds.getUID(), null); if (user == null) { throw new Exception("Could not map " + creds.getUID() + " to user"); } String group = mapper.getGroupForGID(creds.getGID(), null); if (group == null) { throw new Exception("Could not map " + creds.getGID() + " to group"); } FileSystem fs = session.getFileSystem(); FileStatus fileStatus = fs.getFileStatus(path); FsPermission perms = fileStatus.getPermission(); //FsAction action = perms.getUserAction(); // always comes back ALL?? int permissions = perms.toShort(); int saved = permissions; int rtn = setPerms(permissions, false); permissions = permissions >> 3; if (group.equals(fileStatus.getGroup())) { rtn = setPerms(permissions, true); } permissions = permissions >> 3; if (user.equals(fileStatus.getOwner())) { rtn = setPerms(permissions, true); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking access for '" + user + "' and path " + path + " owned by '" + fileStatus.getOwner() + "' permissions " + Integer.toOctalString(saved) + ", Returning " + Integer.toHexString(rtn)); } int access = rtn & request.getAccess(); ACCESSResponse response = createResponse(); response.setStatus(NFS4_OK); response.setAccess(access); response.setSupported(access); return response; } catch (Exception e) { throw new NFS4Exception(NFS4ERR_SERVERFAULT, e); } }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.state.HDFSState.java
License:Apache License
protected boolean check(String user, List<String> groups, FileStatus status, FsAction access) { FsPermission mode = status.getPermission(); if (user.equals(status.getOwner())) { // user class if (mode.getUserAction().implies(access)) { return true; }/*w w w . j a va 2 s .c om*/ } else if (groups.contains(status.getGroup())) { // group class if (mode.getGroupAction().implies(access)) { return true; } } else { // other class if (mode.getOtherAction().implies(access)) { return true; } } return false; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.TestWithClient.java
License:Apache License
protected void doCompareFileStatusFile(FileStatus fileStatus) throws IOException { File file = new File(fileStatus.path.toString()); String domain = NFSUtils.getDomain(new Configuration(), LOCALHOST); assertEquals(getOwner(file) + "@" + domain, fileStatus.getOwner()); assertEquals(getOwnerGroup(file) + "@" + domain, fileStatus.getOwnerGroup()); assertEquals(file.length(), fileStatus.getSize()); assertEquals(file.lastModified(), fileStatus.getMTime()); assertEquals(file.isDirectory(), fileStatus.isDir()); }
From source file:com.cloudera.hoop.client.fs.TestHoopFileSystem.java
License:Open Source License
private void testListStatus() throws Exception { FileSystem fs = FileSystem.get(getHadoopConf()); Path path = new Path(getHadoopTestDir(), "foo.txt"); OutputStream os = fs.create(path); os.write(1);/* w w w .jav a2s . co m*/ os.close(); FileStatus status1 = fs.getFileStatus(path); fs.close(); Configuration conf = new Configuration(); conf.set("fs.http.impl", HoopFileSystem.class.getName()); fs = FileSystem.get(getJettyURL().toURI(), conf); FileStatus status2 = fs.getFileStatus(new Path(path.toUri().getPath())); fs.close(); Assert.assertEquals(status2.getPermission(), status1.getPermission()); Assert.assertEquals(status2.getPath().toUri().getPath(), status1.getPath().toUri().getPath()); Assert.assertEquals(status2.getReplication(), status1.getReplication()); Assert.assertEquals(status2.getBlockSize(), status1.getBlockSize()); Assert.assertEquals(status2.getAccessTime(), status1.getAccessTime()); Assert.assertEquals(status2.getModificationTime(), status1.getModificationTime()); Assert.assertEquals(status2.getOwner(), status1.getOwner()); Assert.assertEquals(status2.getGroup(), status1.getGroup()); Assert.assertEquals(status2.getLen(), status1.getLen()); FileStatus[] stati = fs.listStatus(path.getParent()); Assert.assertEquals(stati.length, 1); Assert.assertEquals(stati[0].getPath().getName(), path.getName()); }
From source file:com.cloudera.hoop.client.fs.TestHoopFileSystem.java
License:Open Source License
private void testSetOwner() throws Exception { FileSystem fs = FileSystem.get(getHadoopConf()); Path path = new Path(getHadoopTestDir(), "foo.txt"); OutputStream os = fs.create(path); os.write(1);/*from ww w .j ava2s .co m*/ os.close(); fs.close(); Configuration conf = new Configuration(); conf.set("fs.http.impl", HoopFileSystem.class.getName()); fs = FileSystem.get(getJettyURL().toURI(), conf); String user = getHadoopUsers()[1]; String group = getHadoopUserGroups(user)[0]; fs.setOwner(path, user, group); fs.close(); fs = FileSystem.get(getHadoopConf()); FileStatus status1 = fs.getFileStatus(path); fs.close(); Assert.assertEquals(status1.getOwner(), user); Assert.assertEquals(status1.getGroup(), group); }
From source file:com.cloudera.hoop.fs.FSUtils.java
License:Open Source License
/** * Converts a Hadoop <code>FileStatus</code> object into a JSON array * object. It replaces the <code>SCHEME://HOST:PORT</code> of the path * with the specified URL./*from w w w.j av a 2s . c o m*/ * <p/> * @param status Hadoop file status. * @param hoopBaseUrl base URL to replace the * <code>SCHEME://HOST:PORT</code> in the file status. * @return The JSON representation of the file status. */ @SuppressWarnings("unchecked") public static Map fileStatusToJSON(FileStatus status, String hoopBaseUrl) { Map json = new LinkedHashMap(); json.put("path", convertPathToHoop(status.getPath(), hoopBaseUrl).toString()); json.put("isDir", status.isDir()); json.put("len", status.getLen()); json.put("owner", status.getOwner()); json.put("group", status.getGroup()); json.put("permission", permissionToString(status.getPermission())); json.put("accessTime", status.getAccessTime()); json.put("modificationTime", status.getModificationTime()); json.put("blockSize", status.getBlockSize()); json.put("replication", status.getReplication()); return json; }
From source file:com.cloudera.recordbreaker.analyzer.FSAnalyzer.java
License:Open Source License
/** * <code>addFileMetadata</code> stores the pathname, size, owner, etc. *//*from w w w .java2 s . c o m*/ void addFileMetadata(final FileStatus fstatus, final long crawlId) { // Compute strings to represent file metadata Path insertFile = fstatus.getPath(); final boolean isDir = fstatus.isDir(); FsPermission fsp = fstatus.getPermission(); final String permissions = (isDir ? "d" : "-") + fsp.getUserAction().SYMBOL + fsp.getGroupAction().SYMBOL + fsp.getOtherAction().SYMBOL; // Compute formal pathname representation String fnameString = null; String parentPathString = null; if (isDir && insertFile.getParent() == null) { parentPathString = ""; fnameString = insertFile.toString(); } else { fnameString = insertFile.getName(); parentPathString = insertFile.getParent().toString(); // REMIND --- mjc --- If we want to modify the Files table s.t. it does // not contain the filesystem prefix, then this would be the place to do it. if (!parentPathString.endsWith("/")) { parentPathString = parentPathString + "/"; } } final String parentPath = parentPathString; final String fName = fnameString; final long fileId = dbQueue.execute(new SQLiteJob<Long>() { protected Long job(SQLiteConnection db) throws SQLiteException { SQLiteStatement stmt = db.prepare("INSERT into Files VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); try { stmt.bind(1, isDir ? "True" : "False").bind(2, crawlId).bind(3, fName) .bind(4, fstatus.getOwner()).bind(5, fstatus.getGroup()).bind(6, permissions) .bind(7, fstatus.getLen()) .bind(8, fileDateFormat.format(new Date(fstatus.getModificationTime()))) .bind(9, parentPath); stmt.step(); return db.getLastInsertId(); } finally { stmt.dispose(); } } }).complete(); }