@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 create(String path, CreateOptions options) throws IOException { if (options.getCreateParent() && !mkdirs(getParentPath(path))) { throw new IOException(ExceptionMessage.PARENT_CREATION_FAILED.getMessage(path)); } return createObject(stripPrefixIfPresent(path)); }
/** * 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 create(String path, CreateOptions options) throws IOException { if (options.getCreateParent() && !mkdirs(getParentPath(path))) { throw new IOException(ExceptionMessage.PARENT_CREATION_FAILED.getMessage(path)); } return createObject(stripPrefixIfPresent(path)); }