View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.context.task;
21  
22  import org.waarp.common.command.exception.Reply550Exception;
23  import org.waarp.common.file.FileUtils;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.openr66.context.R66Session;
27  import org.waarp.openr66.protocol.exception.OpenR66ProtocolSystemException;
28  
29  import java.io.File;
30  import java.util.regex.Pattern;
31  
32  /**
33   * Create a link of the current file and make the file pointing to it.
34   * <p>
35   * The link first tries to be a hard link, then a soft link, and if really not
36   * possible (not supported by the
37   * filesystem), it does a copy and rename task.
38   */
39  public class LinkRenameTask extends AbstractTask {
40    /**
41     * Internal Logger
42     */
43    private static final WaarpLogger logger =
44        WaarpLoggerFactory.getLogger(LinkRenameTask.class);
45    private static final Pattern COMPILE = Pattern.compile(" ");
46  
47    /**
48     * @param argRule
49     * @param delay
50     * @param argTransfer
51     * @param session
52     */
53    public LinkRenameTask(final String argRule, final int delay,
54                          final String argTransfer, final R66Session session) {
55      super(TaskType.LINKRENAME, delay, argRule, argTransfer, session);
56    }
57  
58    @Override
59    public final void run() {
60      String finalname = argRule;
61      finalname = getReplacedValue(finalname, COMPILE.split(argTransfer));
62      logger.info("Move and Rename to {} with {}:{} and {}", finalname, argRule,
63                  argTransfer, session);
64      // First try hard link
65      // FIXME wait for NIO.2 in JDK7 to have such functions, in the meantime only move...
66      final File from = session.getFile().getTrueFile();
67      final File to = new File(finalname);
68      try {
69        FileUtils.copy(from, to, false, false);
70      } catch (final Reply550Exception e1) {
71        logger.error(
72            "Copy and Rename to " + finalname + " with " + argRule + ':' +
73            argTransfer + " and " + session + ": {}", e1.getMessage());
74        futureCompletion.setFailure(new OpenR66ProtocolSystemException(e1));
75        return;
76      }
77      session.getRunner().setFileMoved(finalname, true);
78      futureCompletion.setSuccess();
79    }
80  
81  }