List of usage examples for org.apache.hadoop.fs FileStatus getPath
public Path getPath()
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.FileIDHandler.java
License:Apache License
@Override public FileID get(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus) throws NFS4Exception { FileID fileID = new FileID(); fileID.setFileID(server.getFileID(fileStatus.getPath())); return fileID; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.ModeHandler.java
License:Apache License
@Override public boolean set(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus, StateID stateID, Mode attr) throws NFS4Exception, IOException { FsPermission perm = new FsPermission((short) attr.getMode()); fs.setPermission(fileStatus.getPath(), perm); return true;/* www . j a va 2 s .c o m*/ }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.OwnerGroupHandler.java
License:Apache License
@Override public boolean set(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus, StateID stateID, OwnerGroup attr) throws IOException { String group = OwnerHandler.removeDomain(attr.getOwnerGroup()); if (fileStatus.getGroup().equals(group)) { fs.setOwner(fileStatus.getPath(), null, group); return true; }//from ww w. j av a 2 s . c o m return false; }
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; }/* ww w . j av a 2s .c o m*/ return false; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.SetAccessTimeHandler.java
License:Apache License
@Override public boolean set(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus, StateID stateID, SetAccessTime attr) throws NFS4Exception, IOException { if (attr.getHow() == NFS4_SET_TO_CLIENT_TIME4) { fs.setTimes(fileStatus.getPath(), fileStatus.getModificationTime(), attr.getTime().toMilliseconds()); } else {// w w w . j ava 2 s .com fs.setTimes(fileStatus.getPath(), fileStatus.getModificationTime(), System.currentTimeMillis()); } return true; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.attrs.SetModifyTimeHandler.java
License:Apache License
@Override public boolean set(NFS4Handler server, Session session, FileSystem fs, FileStatus fileStatus, StateID stateID, SetModifyTime attr) throws NFS4Exception, IOException { if (attr.getHow() == NFS4_SET_TO_CLIENT_TIME4) { fs.setTimes(fileStatus.getPath(), attr.getTime().toMilliseconds(), fileStatus.getAccessTime()); } else {//from w w w . j a v a 2 s . c o m fs.setTimes(fileStatus.getPath(), System.currentTimeMillis(), fileStatus.getAccessTime()); } return true; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.READDIRHandler.java
License:Apache License
@Override protected READDIRResponse doHandle(NFS4Handler server, Session session, READDIRRequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }/*from www . ja v a 2s.c o m*/ Path path = server.getPath(session.getCurrentFileHandle()); FileSystem fs = session.getFileSystem(); FileStatus fileStatus = fs.getFileStatus(path); if (!fileStatus.isDir()) { throw new NFS4Exception(NFS4ERR_NOTDIR); } FileStatus[] fileStati = fs.listStatus(path); if (fileStati == null) { // we have already check the dir exists, this means it's empty fileStati = new FileStatus[0]; } Arrays.sort(fileStati); // low cookie numbers are "special" in the linux kernel // since we don't return . and .. they fake them with low #s long verifer = fileStati.length; long cookie = request.getCookie(); if (cookie == 0) { cookie += NFS4_COOKIE_OFFSET; } else { cookie++; } long requestVerifer = Bytes.toLong(request.getCookieVerifer().getData()); if ((requestVerifer > 0 && verifer != requestVerifer) || (cookie > 0 && cookie > (fileStati.length + NFS4_COOKIE_OFFSET))) { LOGGER.warn("BAD COOKIE verifier = " + verifer + ", request.getVerifier() = " + requestVerifer + ", cookie = " + cookie + ", dirLength = " + fileStati.length); throw new NFS4Exception(NFS4ERR_BAD_COOKIE); } // TODO improve this guess // we can only send maxCount bytes including xdr overhead // save 100 bytes for the readDir header and for RPC header // I saw about 100 bytes in wireshark for linux and pulled // the RPC number out of my arse. I guessed high. int messageSize = 100 + 150; int maxMessageSize = request.getMaxCount(); // TODO this check should be after we add the first entry to the response if (messageSize > maxMessageSize) { throw new NFS4Exception(NFS4ERR_TOOSMALL); } List<DirectoryEntry> entries = Lists.newArrayList(); for (; cookie < (fileStati.length + NFS4_COOKIE_OFFSET); cookie++) { fileStatus = fileStati[(int) (cookie - NFS4_COOKIE_OFFSET)]; // we have to force creation of a file handle because that creates // a fileid which is required later in the getAttrs. server.createFileHandle(fileStatus.getPath()); DirectoryEntry entry = readAttrs(server, session, request.getAttrs(), fs, fileStatus); entry.setName(fileStatus.getPath().getName()); entry.setCookie(cookie); // If this entry is more than we can send // break out and the rest will be handled // in a future call // below is ugly as hell but this code is not hot RPCBuffer buffer = new RPCBuffer(); entry.write(buffer); buffer.flip(); int entryLength = buffer.length(); if (messageSize + entryLength >= maxMessageSize) { break; } messageSize += entryLength; entries.add(entry); server.incrementMetric("NFS_READDIR_ENTRIES", 1); } DirectoryList entryList = new DirectoryList(); entryList.setDirEntries(entries); entryList.setEOF(cookie == (fileStati.length + NFS4_COOKIE_OFFSET)); READDIRResponse response = createResponse(); response.setStatus(NFS4_OK); response.setCookieVerifer(new OpaqueData8(verifer)); response.setDirectoryList(entryList); return response; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.NFS4Handler.java
License:Apache License
/** * Files open for write will have an unreliable length according to the name * node. As such, this call intercepts calls for open files and returns the * length of the as reported by the output stream. * * @param status/*from w w w .j a v a2 s. com*/ * @return the current file length including data written to the output * stream * @throws NFS4Exception if the getPos() call of the output stream throws * IOException */ public long getFileSize(FileStatus status) throws NFS4Exception { FileHolder fileHolder = mPathMap.get(realPath(status.getPath())); if (fileHolder != null) { OpenFile<FSDataOutputStream> file = fileHolder.getFSDataOutputStream(); if (file != null) { try { FSDataOutputStream out = file.get(); return out.getPos(); } catch (IOException e) { throw new NFS4Exception(NFS4ERR_SERVERFAULT, e); } } } return status.getLen(); }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.state.HDFSState.java
License:Apache License
/** * Files open for write will have an unreliable length according to the name * node. As such, this call intercepts calls for open files and returns the * length of the as reported by the output stream. * * @param status/*from w w w . ja va 2s. c om*/ * @return the current file length including data written to the output * stream * @throws NFS4Exception if the getPos() call of the output stream throws * IOException */ public long getFileSize(FileStatus status) throws NFS4Exception { FileHandle fileHandle = mFileHandleINodeMap.getFileHandleByPath(realPath(status.getPath())); if (fileHandle != null) { HDFSFile hdfsFile = mOpenFilesMap.get(fileHandle); if (hdfsFile != null) { OpenResource<HDFSOutputStream> file = hdfsFile.getHDFSOutputStreamForWrite(); if (file != null) { HDFSOutputStream out = file.get(); return out.getPos(); } } } return status.getLen(); }
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);//from w w w . ja va 2 s . c om 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()); }