@Override public void close() throws IOException { if (mClosed) { return; } mTemporaryOutputStream.close(); if (!mUfs.renameFile(mTemporaryPath, mPermanentPath)) { if (!mUfs.deleteFile(mTemporaryPath)) { LOG.error("Failed to delete temporary file {}", mTemporaryPath); } throw new IOException( ExceptionMessage.FAILED_UFS_RENAME.getMessage(mTemporaryPath, mPermanentPath)); } // Preserve owner and group in case delegation was used to create the path if (mOptions.getOwner() != null || mOptions.getGroup() != null) { try { mUfs.setOwner(mPermanentPath, mOptions.getOwner(), mOptions.getGroup()); } catch (Exception e) { LOG.warn("Failed to update the ufs ownership, default values will be used. " + e); } } // TODO(chaomin): consider setMode of the ufs file. mClosed = true; } }
@Override protected void completeRequest(UfsFileWriteRequestContext context) throws Exception { if (context == null) { return; } if (context.getOutputStream() == null) { createUfsFile(context); } Preconditions.checkState(context.getOutputStream() != null); context.getOutputStream().close(); CreateOptions createOptions = context.getCreateOptions(); if (createOptions != null) { try { // Set the owner/group of the file to the correct owner. context.getUfsResource().get() .setOwner(context.getRequest().getUfsPath(), createOptions.getOwner(), createOptions.getGroup()); } catch (IOException e) { LOG.warn("Failed to update ownership for ufs path: {} owner: {} group: {} error: {}", context.getRequest().getUfsPath(), createOptions.getOwner(), createOptions.getGroup(), e.toString()); } } context.setOutputStream(null); context.setCreateOptions(null); context.getUfsResource().close(); }
/** * 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 void close() throws IOException { if (mClosed) { return; } mTemporaryOutputStream.close(); if (!mUfs.renameFile(mTemporaryPath, mPermanentPath)) { if (!mUfs.deleteFile(mTemporaryPath)) { LOG.error("Failed to delete temporary file {}", mTemporaryPath); } throw new IOException( ExceptionMessage.FAILED_UFS_RENAME.getMessage(mTemporaryPath, mPermanentPath)); } // Preserve owner and group in case delegation was used to create the path if (mOptions.getOwner() != null || mOptions.getGroup() != null) { try { mUfs.setOwner(mPermanentPath, mOptions.getOwner(), mOptions.getGroup()); } catch (Exception e) { LOG.warn("Failed to update the ufs ownership, default values will be used. " + e); } } // TODO(chaomin): consider setMode of the ufs file. mClosed = true; } }