@Override public OutputStream createDirect(String path, CreateOptions options) throws IOException { path = stripPath(path); if (options.getCreateParent()) { File parent = new File(path).getParentFile(); if (parent != null && !parent.mkdirs() && !parent.isDirectory()) { throw new IOException(ExceptionMessage.PARENT_CREATION_FAILED.getMessage(path)); } } OutputStream stream = new FileOutputStream(path); try { setMode(path, options.getMode().toShort()); } catch (IOException e) { stream.close(); throw e; } return stream; }
@Override public OutputStream createDirect(String path, CreateOptions options) throws IOException { IOException te = null; FileSystem hdfs = getFs(); RetryPolicy retryPolicy = new CountingRetry(MAX_TRY); while (retryPolicy.attempt()) { try { // TODO(chaomin): support creating HDFS files with specified block size and replication. OutputStream outputStream = new HdfsUnderFileOutputStream( FileSystem.create(hdfs, new Path(path), new FsPermission(options.getMode().toShort()))); if (options.getAcl() != null) { setAclEntries(path, options.getAcl().getEntries()); } return outputStream; } catch (IOException e) { LOG.warn("Attempt count {} : {} ", retryPolicy.getAttemptCount(), e.getMessage()); te = e; } } throw te; }
/** * Tests getting and setting fields. */ @Test public void fields() { Random random = new Random(); boolean createParent = random.nextBoolean(); boolean ensureAtomic = random.nextBoolean(); String owner = CommonUtils.randomAlphaNumString(10); String group = CommonUtils.randomAlphaNumString(10); Mode mode = new Mode((short) random.nextInt()); CreateOptions options = CreateOptions.defaults(mConfiguration); options.setCreateParent(createParent); options.setEnsureAtomic(ensureAtomic); options.setOwner(owner); options.setGroup(group); options.setMode(mode); assertEquals(createParent, options.getCreateParent()); assertEquals(ensureAtomic, options.isEnsureAtomic()); assertEquals(owner, options.getOwner()); assertEquals(group, options.getGroup()); assertEquals(mode, options.getMode()); }
/** * Tests for default {@link CreateOptions}. */ @Test public void defaults() throws IOException { CreateOptions options = CreateOptions.defaults(mConfiguration); assertFalse(options.getCreateParent()); assertFalse(options.isEnsureAtomic()); assertNull(options.getOwner()); assertNull(options.getGroup()); String umask = mConfiguration.get(PropertyKey.SECURITY_AUTHORIZATION_PERMISSION_UMASK); assertEquals(ModeUtils.applyFileUMask(Mode.defaults(), umask), options.getMode()); }
/** * Tests for building an {@link CreateOptions} with a security enabled * configuration. */ @Test public void securityEnabled() throws IOException { mConfiguration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.SIMPLE.getAuthName()); mConfiguration.set(PropertyKey.SECURITY_LOGIN_USERNAME, "foo"); // Use IdentityUserGroupMapping to map user "foo" to group "foo". mConfiguration.set(PropertyKey.SECURITY_GROUP_MAPPING_CLASS, IdentityUserGroupsMapping.class.getName()); CreateOptions options = CreateOptions.defaults(mConfiguration); assertFalse(options.getCreateParent()); assertFalse(options.isEnsureAtomic()); assertNull(options.getOwner()); assertNull(options.getGroup()); String umask = mConfiguration.get(PropertyKey.SECURITY_AUTHORIZATION_PERMISSION_UMASK); assertEquals(ModeUtils.applyFileUMask(Mode.defaults(), umask), options.getMode()); }
@Override public OutputStream createDirect(String path, CreateOptions options) throws IOException { IOException te = null; RetryPolicy retryPolicy = new CountingRetry(MAX_TRY); while (retryPolicy.attemptRetry()) { try { // TODO(chaomin): support creating HDFS files with specified block size and replication. return FileSystem.create(mFileSystem, new Path(path), new FsPermission(options.getMode().toShort())); } catch (IOException e) { LOG.warn("Retry count {} : {} ", retryPolicy.getRetryCount(), e.getMessage()); te = e; } } throw te; }