List of usage examples for org.apache.hadoop.fs FileSystem getFileStatus
public abstract FileStatus getFileStatus(Path f) throws IOException;
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.CREATEHandler.java
License:Apache License
@Override protected CREATEResponse doHandle(NFS4Handler server, Session session, CREATERequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }//from w w w . jav a 2s . c o m if (request.getType() != NFS4_DIR) { throw new UnsupportedOperationException( "Create files of type " + request.getType() + " is not supported."); } if ("".equals(request.getName())) { throw new NFS4Exception(NFS4ERR_INVAL); } Path parent = server.getPath(session.getCurrentFileHandle()); Path path = new Path(parent, request.getName()); FileSystem fs = session.getFileSystem(); if (!fs.exists(parent)) { throw new NFS4Exception(NFS4ERR_STALE, "Parent " + parent + " does not exist"); } if (fs.exists(path)) { throw new NFS4Exception(NFS4ERR_EXIST, "Path " + path + " already exists."); } long parentModTimeBefore = fs.getFileStatus(parent).getModificationTime(); if (!fs.mkdirs(path)) { throw new NFS4Exception(NFS4ERR_IO); } long parentModTimeAfter = fs.getFileStatus(parent).getModificationTime(); FileStatus fileStatus = fs.getFileStatus(path); ImmutableMap<Integer, Attribute> requestAttrs = request.getAttrValues(); // TODO Handlers should have annotations so that setAttrs can throw an // error if they require the stateID to be set. Bitmap responseAttrs = Attribute.setAttrs(server, session, request.getAttrs(), requestAttrs, fs, fileStatus, null); session.setCurrentFileHandle(server.createFileHandle(path)); CREATEResponse response = createResponse(); response.setChangeInfo(ChangeInfo.newChangeInfo(true, parentModTimeBefore, parentModTimeAfter)); response.setStatus(NFS4_OK); response.setAttrs(responseAttrs); return response; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.GETATTRHandler.java
License:Apache License
@Override protected GETATTRResponse doHandle(NFS4Handler server, Session session, GETATTRRequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }/*from w w w. ja v a2 s .co m*/ Path path = server.getPath(session.getCurrentFileHandle()); try { FileSystem fs = session.getFileSystem(); FileStatus fileStatus = fs.getFileStatus(path); Pair<Bitmap, ImmutableList<Attribute>> attrs = Attribute.getAttrs(server, session, request.getAttrs(), fs, fileStatus); GETATTRResponse response = createResponse(); response.setStatus(NFS4_OK); response.setAttrs(attrs.getFirst()); response.setAttrValues(attrs.getSecond()); return response; } catch (FileNotFoundException e) { throw new NFS4Exception(NFS4ERR_NOENT); } }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.OPENHandler.java
License:Apache License
protected OPENResponse read(NFS4Handler server, Session session, OPENRequest request) throws NFS4Exception, IOException { if (request.getDeny() != 0) { throw new UnsupportedOperationException("Read access does not support deny " + request.getDeny()); }//from w w w . j a v a2 s . co m // generate stateid StateID stateID = StateID.newStateID(request.getSeqID()); FileSystem fs = session.getFileSystem(); Path parentPath = server.getPath(session.getCurrentFileHandle()); Path path = new Path(parentPath, request.getName()); session.setCurrentFileHandle(server.createFileHandle(path)); @SuppressWarnings("unused") FSDataInputStream in = server.forRead(stateID, fs, session.getCurrentFileHandle()); OPENResponse response = createResponse(); response.setStateID(stateID); FileStatus fileStatus = fs.getFileStatus(path); // TODO this is wrong but files in HDFS are currently immutable once closed ChangeID changeID = new ChangeID(); changeID.setChangeID(fileStatus.getModificationTime()); ChangeInfo changeInfo = new ChangeInfo(); changeInfo.setChangeIDBefore(changeID); changeInfo.setChangeIDAfter(changeID); changeInfo.setAtomic(true); response.setChangeID(changeInfo); response.setResultFlags(NFS4_OPEN4_RESULT_CONFIRM); // TODO do we really need confirm step? // if(request.getAttrs() != null) { // Pair<Bitmap, ImmutableList<Attribute>> pair = Attribute.getAttrs(server, session, request.getAttrs(), fs, fileStatus); // response.setAttrs(pair.getFirst()); // response.setAttrValues(pair.getSecond()); // } response.setDelgationType(NFS4_CLAIM_NULL); response.setStatus(NFS4_OK); return response; }
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); }/*www. j ava2 s . co 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.handlers.READHandler.java
License:Apache License
@Override protected READResponse doHandle(NFS4Handler server, Session session, READRequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }//from w w w . j a v a2 s .c o m int size = Math.min(request.getCount(), NFS4_MAX_RWSIZE); if (size < 0) { throw new NFS4Exception(NFS4ERR_INVAL); } FileHandle fileHandle = session.getCurrentFileHandle(); Path path = server.getPath(fileHandle); FileSystem fs = session.getFileSystem(); FSDataInputStream inputStream = server.forRead(request.getStateID(), fs, fileHandle); synchronized (inputStream) { if (inputStream.getPos() != request.getOffset()) { try { inputStream.seek(request.getOffset()); } catch (IOException e) { throw new IOException(e.getMessage() + ": " + inputStream.getPos() + ", " + request.getOffset(), e); } server.incrementMetric("NFS_RANDOM_READS", 1); } READResponse response = createResponse(); byte[] data = new byte[size]; int count = inputStream.read(data); long fileLength = -1; if (count > 0 && count != data.length && (request.getOffset() + count) < (fileLength = fs.getFileStatus(path).getLen())) { LOGGER.info("Short read " + path + " at pos = " + request.getOffset() + ", wanted " + data.length + " and read " + count + ", fileLength = " + fileLength); server.incrementMetric("NFS_SHORT_READS", 1); } boolean eof = count < 0; if (eof) { data = new byte[0]; count = 0; } server.incrementMetric("HDFS_BYTES_READ", count); response.setData(data, 0, count); response.setEOF(eof); response.setStatus(NFS4_OK); return response; } }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.REMOVEHandler.java
License:Apache License
@Override protected REMOVEResponse doHandle(NFS4Handler server, Session session, REMOVERequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }// www . j a va 2s .co m if ("".equals(request.getName())) { throw new NFS4Exception(NFS4ERR_INVAL); } Path parentPath = server.getPath(session.getCurrentFileHandle()); Path path = new Path(parentPath, request.getName()); FileSystem fs = session.getFileSystem(); if (!fs.exists(path)) { throw new NFS4Exception(NFS4ERR_NOENT); } REMOVEResponse response = createResponse(); ChangeInfo changeInfo = new ChangeInfo(); FileStatus parentStatus = fs.getFileStatus(parentPath); ChangeID changeIDBefore = new ChangeID(); changeIDBefore.setChangeID(parentStatus.getModificationTime()); changeInfo.setChangeIDBefore(changeIDBefore); fs.delete(path, false); parentStatus = fs.getFileStatus(parentPath); ChangeID changeIDAfter = new ChangeID(); changeIDAfter.setChangeID(parentStatus.getModificationTime()); changeInfo.setChangeIDAfter(changeIDAfter); changeInfo.setAtomic(true); response.setChangeInfo(changeInfo); response.setStatus(NFS4_OK); return response; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.RENAMEHandler.java
License:Apache License
@Override protected RENAMEResponse doHandle(NFS4Handler server, Session session, RENAMERequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null || session.getSavedFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }/*from w ww. j a v a 2 s . co m*/ if ("".equals(request.getOldName()) || "".equals(request.getNewName())) { throw new NFS4Exception(NFS4ERR_INVAL); } FileSystem fs = session.getFileSystem(); Path oldParentPath = server.getPath(session.getSavedFileHandle()); Path oldPath = new Path(oldParentPath, request.getOldName()); Path newParentPath = server.getPath(session.getCurrentFileHandle()); Path newPath = new Path(newParentPath, request.getNewName()); if (!(fs.getFileStatus(oldParentPath).isDir() && fs.getFileStatus(newParentPath).isDir())) { throw new NFS4Exception(NFS4ERR_NOTDIR); } if (!server.fileExists(fs, oldPath)) { throw new NFS4Exception(NFS4ERR_NOENT, "Path " + oldPath + " does not exist."); } if (server.fileExists(fs, newPath)) { // TODO according to the RFC we are supposed to check to see if // the entry which exists is compatible (overwrite file and // empty directory if the "old" item is a file or dir respectively. throw new NFS4Exception(NFS4ERR_EXIST, "Path " + newPath + " exists."); } // we won't support renaming files which are open // but it happens fairly often that due to tcp/ip // the rename request is received before the close. // Below we delay the rename if the file is open for (int i = 0; i < 5; i++) { if (!server.isFileOpen(oldPath)) { break; } try { Thread.sleep(100L); } catch (InterruptedException e) { throw new IOException("Interrupted while waiting for file to close", e); } } if (server.isFileOpen(oldPath)) { throw new NFS4Exception(NFS4ERR_FILE_OPEN); } LOGGER.info(session.getSessionID() + " Renaming " + oldPath + " to " + newPath); long beforeSource = fs.getFileStatus(oldParentPath).getModificationTime(); long beforeDest = fs.getFileStatus(newParentPath).getModificationTime(); if (!fs.rename(oldPath, newPath)) { throw new NFS4Exception(NFS4ERR_IO); } long afterSource = fs.getFileStatus(oldParentPath).getModificationTime(); long afterDest = fs.getFileStatus(newParentPath).getModificationTime(); RENAMEResponse response = createResponse(); response.setChangeInfoSource(ChangeInfo.newChangeInfo(true, beforeSource, afterSource)); response.setChangeInfoDest(ChangeInfo.newChangeInfo(true, beforeDest, afterDest)); response.setStatus(NFS4_OK); return response; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.handlers.SETATTRHandler.java
License:Apache License
@Override protected SETATTRResponse doHandle(NFS4Handler server, Session session, SETATTRRequest request) throws NFS4Exception, IOException { if (session.getCurrentFileHandle() == null) { throw new NFS4Exception(NFS4ERR_NOFILEHANDLE); }// w ww.j av a 2s .co m Path path = server.getPath(session.getCurrentFileHandle()); FileSystem fs = session.getFileSystem(); FileStatus fileStatus = fs.getFileStatus(path); ImmutableMap<Integer, Attribute> requestAttrs = request.getAttrValues(); Bitmap responseAttrs = Attribute.setAttrs(server, session, request.getAttrs(), requestAttrs, fs, fileStatus, request.getStateID()); SETATTRResponse response = createResponse(); response.setStatus(NFS4_OK); response.setAttrs(responseAttrs); return response; }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.NFS4Handler.java
License:Apache License
/** * Open if not open or obtain the input stream opened by the StateID. * * @param stateID/*from w w w . j a v a 2 s .c o m*/ * @param fs * @param fileHandle * @return FSDataInputStream for reading * @throws NFS4Exception if the file is already open for write, the open is * not confirmed or the file handle is stale. * @throws IOException if the file open throws an IOException */ public synchronized FSDataInputStream forRead(StateID stateID, FileSystem fs, FileHandle fileHandle) throws NFS4Exception, IOException { FileHolder fileHolder = mFileHandleMap.get(fileHandle); if (fileHolder != null) { if (fileHolder.isOpenForWrite()) { throw new NFS4Exception(NFS4ERR_FILE_OPEN); // TODO lock unavailable // should be _LOCK? } Path path = new Path(fileHolder.getPath()); OpenFile<FSDataInputStream> file = fileHolder.getFSDataInputStream(stateID); if (file != null) { if (!file.isConfirmed()) { throw new NFS4Exception(NFS4ERR_DENIED); } return file.get(); } FileStatus status = fs.getFileStatus(path); if (status.isDir()) { throw new NFS4Exception(NFS4ERR_ISDIR); } FSDataInputStream in = fs.open(path); this.incrementMetric("FILES_OPENED_READ", 1); fileHolder.putFSDataInputStream(stateID, in); return in; } throw new NFS4Exception(NFS4ERR_STALE); }
From source file:com.cloudera.hadoop.hdfs.nfs.nfs4.NFS4Handler.java
License:Apache License
/** * * @param stateID//from www . j a va2 s .c o m * @param fs * @param fileHandle * @param overwrite * @return * @throws NFS4Exception * @throws IOException */ public synchronized FSDataOutputStream forWrite(StateID stateID, FileSystem fs, FileHandle fileHandle, boolean overwrite) throws NFS4Exception, IOException { FileHolder fileHolder = mFileHandleMap.get(fileHandle); if (fileHolder != null) { OpenFile<FSDataOutputStream> file = fileHolder.getFSDataOutputStream(); if (file != null) { if (file.isOwnedBy(stateID)) { return file.get(); } throw new NFS4Exception(NFS4ERR_FILE_OPEN); } Path path = new Path(fileHolder.getPath()); boolean exists = fs.exists(path); // If overwrite = false, fs.create throws IOException which // is useless. In case of IOE do we always return EXIST? // doesn't seem to make sense. As such, I am mitigating the issue // even if there is a known race between the exists and create if (!overwrite && exists) { // append to a file // We used to be NFS4ERR_EXIST here but the linux client behaved rather // oddly. // It would open the fily with overwrite=true but then send the data // which // was to be appended at offset 0 throw new NFS4Exception(NFS4ERR_PERM, "File Exists and overwrite = false", true); } if (path.getParent() != null) { // TODO bad perms will fail with IOException, perhaps we should check // that file can be created before trying to so we can return the // correct error perm denied } if (exists && fs.getFileStatus(path).isDir()) { throw new NFS4Exception(NFS4ERR_ISDIR); } FSDataOutputStream out = fs.create(path, overwrite); this.incrementMetric("FILES_OPENED_WRITE", 1); fileHolder.setFSDataOutputStream(stateID, out); return out; } throw new NFS4Exception(NFS4ERR_STALE); }