OSDN Git Service

0a7e09a0718809e77f43748a69a65d3a6a4a31d0
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / commands / secure / MoveCommand.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.cyanogenmod.filemanager.commands.secure;
18
19 import android.util.Log;
20
21 import com.cyanogenmod.filemanager.commands.MoveExecutable;
22 import com.cyanogenmod.filemanager.console.ExecutionException;
23 import com.cyanogenmod.filemanager.console.NoSuchFileOrDirectory;
24 import com.cyanogenmod.filemanager.console.secure.SecureConsole;
25 import com.cyanogenmod.filemanager.model.MountPoint;
26 import com.cyanogenmod.filemanager.util.FileHelper;
27
28 import java.io.IOException;
29
30 import de.schlichtherle.truezip.file.TFile;
31
32
33
34 /**
35  * A class for move a file or directory.
36  */
37 public class MoveCommand extends Program implements MoveExecutable {
38
39     private static final String TAG = "MoveCommand"; //$NON-NLS-1$
40
41     private final String mSrc;
42     private final String mDst;
43
44     /**
45      * Constructor of <code>MoveCommand</code>.
46      *
47      * @param console The current console
48      * @param src The name of the file or directory to be moved
49      * @param dst The name of the file or directory in which move the source file or directory
50      */
51     public MoveCommand(SecureConsole console, String src, String dst) {
52         super(console);
53         this.mSrc = src;
54         this.mDst = dst;
55     }
56
57     /**
58      * {@inheritDoc}
59      */
60     @Override
61     public boolean requiresSync() {
62         return true;
63     }
64
65     /**
66      * {@inheritDoc}
67      */
68     @Override
69     public Boolean getResult() {
70         return Boolean.TRUE;
71     }
72
73     /**
74      * {@inheritDoc}
75      */
76     @Override
77     public void execute() throws NoSuchFileOrDirectory, ExecutionException {
78         if (isTrace()) {
79             Log.v(TAG,
80                     String.format("Creating from %s to %s", this.mSrc, this.mDst)); //$NON-NLS-1$
81         }
82
83         TFile s = getConsole().buildRealFile(this.mSrc);
84         TFile d = getConsole().buildRealFile(this.mDst);
85         if (!s.exists()) {
86             if (isTrace()) {
87                 Log.v(TAG, "Result: FAIL. NoSuchFileOrDirectory"); //$NON-NLS-1$
88             }
89             throw new NoSuchFileOrDirectory(this.mSrc);
90         }
91
92         //Move or copy recursively
93         if (d.exists()) {
94             try {
95                 TFile.cp_r(s, d, SecureConsole.DETECTOR, SecureConsole.DETECTOR);
96             } catch (IOException ex) {
97                 throw new ExecutionException("Failed to move file or directory", ex);
98             }
99             if (!FileHelper.deleteFolder(s)) {
100                 if (isTrace()) {
101                     Log.v(TAG, "Result: OK. WARNING. Source not deleted."); //$NON-NLS-1$
102                 }
103             }
104         } else {
105             // Use rename. We are not cross filesystem with this console, so this operation
106             // should be safe
107             try {
108                 TFile.mv(s, d, SecureConsole.DETECTOR);
109             } catch (IOException ex) {
110                 // Make sure truecrypt was right
111                 // There's a strange bug in it where it thinks that a file did not
112                 // move over. However, it actually did.
113                 if (!d.exists()) {
114                     throw new ExecutionException("Failed to rename file or directory", ex);
115                 }
116             }
117         }
118
119         if (isTrace()) {
120             Log.v(TAG, "Result: OK"); //$NON-NLS-1$
121         }
122     }
123
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public MountPoint getSrcWritableMountPoint() {
129         return null;
130     }
131
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public MountPoint getDstWritableMountPoint() {
137         return null;
138     }
139
140 }