001/*
002 * (C) Copyright 2006-2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 *     Mathieu Guillaume
019 *     jcarsique
020 */
021
022package org.nuxeo.ecm.core.blob.binary;
023
024import java.io.File;
025import java.io.FileInputStream;
026import java.io.FileOutputStream;
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.OutputStream;
030import java.io.RandomAccessFile;
031import java.util.Map;
032import java.util.regex.Pattern;
033
034import org.apache.commons.io.FileUtils;
035import org.apache.commons.io.IOUtils;
036import org.apache.commons.lang3.StringUtils;
037import org.apache.commons.logging.Log;
038import org.apache.commons.logging.LogFactory;
039import org.nuxeo.common.Environment;
040import org.nuxeo.ecm.core.api.NuxeoException;
041import org.nuxeo.runtime.api.Framework;
042import org.nuxeo.runtime.trackers.files.FileEventTracker;
043
044/**
045 * A simple filesystem-based binary manager. It stores the binaries according to their digest (hash), which means that
046 * no transactional behavior needs to be implemented.
047 * <p>
048 * A garbage collection is needed to purge unused binaries.
049 * <p>
050 * The format of the <em>binaries</em> directory is:
051 * <ul>
052 * <li><em>data/</em> hierarchy with the actual binaries in subdirectories,</li>
053 * <li><em>tmp/</em> temporary storage during creation,</li>
054 * <li><em>config.xml</em> a file containing the configuration used.</li>
055 * </ul>
056 *
057 * @author Florent Guillaume
058 * @since 5.6
059 */
060public class LocalBinaryManager extends AbstractBinaryManager {
061
062    private static final Log log = LogFactory.getLog(LocalBinaryManager.class);
063
064    public static final Pattern WINDOWS_ABSOLUTE_PATH = Pattern.compile("[a-zA-Z]:[/\\\\].*");
065
066    public static final String DEFAULT_PATH = "binaries";
067
068    public static final String DATA = "data";
069
070    public static final String TMP = "tmp";
071
072    public static final String CONFIG_FILE = "config.xml";
073
074    protected File storageDir;
075
076    protected File tmpDir;
077
078    @Override
079    public void initialize(String blobProviderId, Map<String, String> properties) throws IOException {
080        super.initialize(blobProviderId, properties);
081        String path = properties.get(BinaryManager.PROP_PATH);
082        if (StringUtils.isBlank(path)) {
083            path = DEFAULT_PATH;
084        }
085        path = Framework.expandVars(path);
086        path = path.trim();
087        File base;
088        if (path.startsWith("/") || path.startsWith("\\") || path.contains("://") || path.contains(":\\")
089                || WINDOWS_ABSOLUTE_PATH.matcher(path).matches()) {
090            // absolute
091            base = new File(path);
092        } else {
093            // relative
094            File home = Environment.getDefault().getData();
095            base = new File(home, path);
096
097            // Backward compliance with versions before 5.4 (NXP-5370)
098            File oldBase = new File(Framework.getRuntime().getHome().getPath(), path);
099            if (oldBase.exists()) {
100                log.warn("Old binaries path used (NXP-5370). Please move " + oldBase + " to " + base);
101                base = oldBase;
102            }
103        }
104
105        log.info("Registering binary manager '" + blobProviderId + "' using "
106                + (this.getClass().equals(LocalBinaryManager.class) ? "" : (this.getClass().getSimpleName() + " and "))
107                + "binary store: " + base);
108        storageDir = new File(base, DATA);
109        tmpDir = new File(base, TMP);
110        storageDir.mkdirs();
111        tmpDir.mkdirs();
112        descriptor = getDescriptor(new File(base, CONFIG_FILE));
113        createGarbageCollector();
114
115        // be sure FileTracker won't steal our files !
116        FileEventTracker.registerProtectedPath(storageDir.getAbsolutePath());
117    }
118
119    @Override
120    public void close() {
121        if (tmpDir != null) {
122            try {
123                FileUtils.cleanDirectory(tmpDir);
124            } catch (IOException e) {
125                throw new NuxeoException(e);
126            }
127        }
128    }
129
130    public File getStorageDir() {
131        return storageDir;
132    }
133
134    @Override
135    protected Binary getBinary(InputStream in) throws IOException {
136        String digest = storeAndDigest(in);
137        File file = getFileForDigest(digest, false);
138        /*
139         * Now we can build the Binary.
140         */
141        return new Binary(file, digest, blobProviderId);
142    }
143
144    @Override
145    public Binary getBinary(String digest) {
146        File file = getFileForDigest(digest, false);
147        if (file == null) {
148            // invalid digest
149            return null;
150        }
151        if (!file.exists()) {
152            log.warn("cannot fetch content at " + file.getPath() + " (file does not exist), check your configuration");
153            return null;
154        }
155        return new Binary(file, digest, blobProviderId);
156    }
157
158    /**
159     * Gets a file representing the storage for a given digest.
160     *
161     * @param digest the digest
162     * @param createDir {@code true} if the directory containing the file itself must be created
163     * @return the file for this digest
164     */
165    public File getFileForDigest(String digest, boolean createDir) {
166        int depth = descriptor.depth;
167        if (digest.length() < 2 * depth) {
168            return null;
169        }
170        StringBuilder buf = new StringBuilder(3 * depth - 1);
171        for (int i = 0; i < depth; i++) {
172            if (i != 0) {
173                buf.append(File.separatorChar);
174            }
175            buf.append(digest.substring(2 * i, 2 * i + 2));
176        }
177        File dir = new File(storageDir, buf.toString());
178        if (createDir) {
179            dir.mkdirs();
180        }
181        return new File(dir, digest);
182    }
183
184    protected String storeAndDigest(InputStream in) throws IOException {
185        File tmp = File.createTempFile("create_", ".tmp", tmpDir);
186        /*
187         * First, write the input stream to a temporary file, while computing a digest.
188         */
189        try {
190            String digest;
191            try (OutputStream out = new FileOutputStream(tmp)) {
192                digest = storeAndDigest(in, out);
193            } finally {
194                in.close();
195            }
196            /*
197             * Move the tmp file to its destination.
198             */
199            File file = getFileForDigest(digest, true);
200            atomicMove(tmp, file);
201            return digest;
202        } finally {
203            tmp.delete();
204        }
205
206    }
207
208    /**
209     * Does an atomic move of the tmp (or source) file to the final file.
210     * <p>
211     * Tries to work well with NFS mounts and different filesystems.
212     */
213    protected void atomicMove(File source, File dest) throws IOException {
214        if (dest.exists()) {
215            // The file with the proper digest is already there so don't do
216            // anything. This is to avoid "Stale NFS File Handle" problems
217            // which would occur if we tried to overwrite it anyway.
218            // Note that this doesn't try to protect from the case where
219            // two identical files are uploaded at the same time.
220            // Update date for the GC.
221            dest.setLastModified(source.lastModified());
222            return;
223        }
224        if (!source.renameTo(dest)) {
225            // Failed to rename, probably a different filesystem.
226            // Do *NOT* use Apache Commons IO's FileUtils.moveFile()
227            // because it rewrites the destination file so is not atomic.
228            // Do a copy through a tmp file on the same filesystem then
229            // atomic rename.
230            File tmp = File.createTempFile(dest.getName(), ".tmp", dest.getParentFile());
231            try {
232                try (InputStream in = new FileInputStream(source); //
233                        OutputStream out = new FileOutputStream(tmp)) {
234                    IOUtils.copy(in, out);
235                }
236                // then do the atomic rename
237                tmp.renameTo(dest);
238            } finally {
239                tmp.delete();
240            }
241            // finally remove the original source
242            source.delete();
243        }
244        if (!dest.exists()) {
245            throw new IOException("Could not create file: " + dest);
246        }
247    }
248
249    protected void createGarbageCollector() {
250        garbageCollector = new DefaultBinaryGarbageCollector(this);
251    }
252
253    public static class DefaultBinaryGarbageCollector implements BinaryGarbageCollector {
254
255        /**
256         * Windows FAT filesystems have a time resolution of 2s. Other common filesystems have 1s.
257         */
258        public static final int TIME_RESOLUTION = 2000;
259
260        protected final LocalBinaryManager binaryManager;
261
262        protected volatile long startTime;
263
264        protected BinaryManagerStatus status;
265
266        public DefaultBinaryGarbageCollector(LocalBinaryManager binaryManager) {
267            this.binaryManager = binaryManager;
268        }
269
270        @Override
271        public String getId() {
272            return binaryManager.getStorageDir().toURI().toString();
273        }
274
275        @Override
276        public BinaryManagerStatus getStatus() {
277            return status;
278        }
279
280        @Override
281        public boolean isInProgress() {
282            // volatile as this is designed to be called from another thread
283            return startTime != 0;
284        }
285
286        @Override
287        public void start() {
288            if (startTime != 0) {
289                throw new RuntimeException("Alread started");
290            }
291            startTime = System.currentTimeMillis();
292            status = new BinaryManagerStatus();
293        }
294
295        @Override
296        public void mark(String digest) {
297            File file = binaryManager.getFileForDigest(digest, false);
298            if (!file.exists()) {
299                log.error("Unknown file digest: " + digest);
300                return;
301            }
302            touch(file);
303        }
304
305        @Override
306        public void stop(boolean delete) {
307            if (startTime == 0) {
308                throw new RuntimeException("Not started");
309            }
310            deleteOld(binaryManager.getStorageDir(), startTime - TIME_RESOLUTION, 0, delete);
311            status.gcDuration = System.currentTimeMillis() - startTime;
312            startTime = 0;
313        }
314
315        protected void deleteOld(File file, long minTime, int depth, boolean delete) {
316            if (file.isDirectory()) {
317                for (File f : file.listFiles()) {
318                    deleteOld(f, minTime, depth + 1, delete);
319                }
320                if (depth > 0 && file.list().length == 0) {
321                    // empty directory
322                    file.delete();
323                }
324            } else if (file.isFile() && file.canWrite()) {
325                long lastModified = file.lastModified();
326                long length = file.length();
327                if (lastModified == 0) {
328                    log.error("Cannot read last modified for file: " + file);
329                } else if (lastModified < minTime) {
330                    status.sizeBinariesGC += length;
331                    status.numBinariesGC++;
332                    if (delete && !file.delete()) {
333                        log.warn("Cannot gc file: " + file);
334                    }
335                } else {
336                    status.sizeBinaries += length;
337                    status.numBinaries++;
338                }
339            }
340        }
341    }
342
343    /**
344     * Sets the last modification date to now on a file
345     *
346     * @param file the file
347     */
348    public static void touch(File file) {
349        long time = System.currentTimeMillis();
350        if (file.setLastModified(time)) {
351            // ok
352            return;
353        }
354        if (!file.canWrite()) {
355            // cannot write -> stop won't be able to delete anyway
356            return;
357        }
358        try {
359            // Windows: the file may be open for reading
360            // workaround found by Thomas Mueller, see JCR-2872
361            try (RandomAccessFile r = new RandomAccessFile(file, "rw")) {
362                r.setLength(r.length());
363            }
364        } catch (IOException e) {
365            log.error("Cannot set last modified for file: " + file, e);
366        }
367    }
368
369}