@Override
    public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
        try {
            final EnumSet<OpenMode> flags;
            if(status.isAppend()) {
                if(status.isExists()) {
                    // No append flag. Otherwise the offset field of SSH_FXP_WRITE requests is ignored.
                    flags = EnumSet.of(OpenMode.WRITE);
                }
                else {
                    // Allocate offset
                    flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE);
                }
            }
            else {
                // A new file is created; if the file already exists, it is opened and truncated to preserve ownership of file.
                if(status.isExists()) {
                    if(file.isSymbolicLink()) {
                        // Workaround for #7327
                        session.sftp().remove(file.getAbsolute());
                        flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE);
                    }
                    else {
                        flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE);
                    }
                }
                else {
                    flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE);
                }
            }
            final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags);
            final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status);
            if(log.isInfoEnabled()) {
                log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites));
            }
            if(log.isInfoEnabled()) {
                log.info(String.format("Skipping %d bytes", status.getOffset()));
            }
            // Open stream at offset
            return new VoidStatusOutputStream(new ChunkedOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) {
                private final AtomicBoolean close = new AtomicBoolean();

                @Override
                public void close() throws IOException {
                    if(close.get()) {
                        log.warn(String.format("Skip double close of stream %s", this));
                        return;
                    }
                    try {
                        super.close();
                    }
                    finally {
                        handle.close();
                        close.set(true);
                    }
                }
            }, preferences.getInteger("sftp.write.chunksize")));
        }
        catch(IOException e) {
            throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file);
        }
    }
@Test
    public void testWriteSymlink() throws Exception {
        final Path workdir = new SFTPHomeDirectoryService(session).find();
        final Path target = new Path(workdir, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
        new SFTPTouchFeature(session).touch(target, new TransferStatus());
        assertTrue(new SFTPFindFeature(session).find(target));
        final String name = new AlphanumericRandomStringService().random();
        final Path symlink = new Path(workdir, name, EnumSet.of(Path.Type.file, AbstractPath.Type.symboliclink));
        new SFTPSymlinkFeature(session).symlink(symlink, target.getName());
        assertTrue(new SFTPFindFeature(session).find(symlink));
        final TransferStatus status = new TransferStatus();
        final int length = 1048576;
        final byte[] content = RandomUtils.nextBytes(length);
        status.setLength(content.length);
        status.setExists(true);
        final OutputStream out = new SFTPWriteFeature(session).write(symlink, status, new DisabledConnectionCallback());
        new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
        out.close();
        {
            final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
            final InputStream in = new SFTPReadFeature(session).read(symlink, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
            new StreamCopier(status, status).transfer(in, buffer);
            assertArrayEquals(content, buffer.toByteArray());
        }
        {
            final byte[] buffer = new byte[0];
            final InputStream in = new SFTPReadFeature(session).read(target, new TransferStatus(), new DisabledConnectionCallback());
            IOUtils.readFully(in, buffer);
            in.close();
            assertArrayEquals(new byte[0], buffer);
        }
        final AttributedList<Path> list = new SFTPListService(session).list(workdir, new DisabledListProgressListener());
        assertTrue(list.contains(new Path(workdir, name, EnumSet.of(Path.Type.file))));
        assertFalse(list.contains(symlink));
        new SFTPDeleteFeature(session).delete(Arrays.asList(target, symlink), new DisabledLoginCallback(), new Delete.DisabledCallback());
    }