1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
21 import java
.io
.IOException
;
23 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
24 //import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
26 import android
.accounts
.Account
;
28 import com
.owncloud
.android
.Log_OC
;
29 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
31 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
32 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavUtils
;
33 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
34 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
35 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
.ResultCode
;
36 import com
.owncloud
.android
.utils
.FileStorageUtils
;
40 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
42 * @author David A. Velasco
44 public class RenameFileOperation
extends RemoteOperation
{
46 private static final String TAG
= RenameFileOperation
.class.getSimpleName();
48 private static final int RENAME_READ_TIMEOUT
= 10000;
49 private static final int RENAME_CONNECTION_TIMEOUT
= 5000;
53 private Account mAccount
;
54 private String mNewName
;
55 private String mNewRemotePath
;
56 private FileDataStorageManager mStorageManager
;
62 * @param file OCFile instance describing the remote file or folder to rename
63 * @param account OwnCloud account containing the remote file
64 * @param newName New name to set as the name of file.
65 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
67 public RenameFileOperation(OCFile file
, Account account
, String newName
, FileDataStorageManager storageManager
) {
71 mNewRemotePath
= null
;
72 mStorageManager
= storageManager
;
75 public OCFile
getFile() {
81 * Performs the rename operation.
83 * @param client Client object to communicate with the remote ownCloud server.
86 protected RemoteOperationResult
run(WebdavClient client
) {
87 RemoteOperationResult result
= null
;
89 LocalMoveMethod move
= null
;
90 mNewRemotePath
= null
;
92 if (mNewName
.equals(mFile
.getFileName())) {
93 return new RemoteOperationResult(ResultCode
.OK
);
96 String parent
= (new File(mFile
.getRemotePath())).getParent();
97 parent
= (parent
.endsWith(OCFile
.PATH_SEPARATOR
)) ? parent
: parent
+ OCFile
.PATH_SEPARATOR
;
98 mNewRemotePath
= parent
+ mNewName
;
99 if (mFile
.isFolder()) {
100 mNewRemotePath
+= OCFile
.PATH_SEPARATOR
;
103 // check if the new name is valid in the local file system
104 if (!isValidNewName()) {
105 return new RemoteOperationResult(ResultCode
.INVALID_LOCAL_FILE_NAME
);
108 // check if a file with the new name already exists
109 if (client
.existsFile(mNewRemotePath
) || // remote check could fail by network failure. by indeterminate behavior of HEAD for folders ...
110 mStorageManager
.getFileByPath(mNewRemotePath
) != null
) { // ... so local check is convenient
111 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
113 move
= new LocalMoveMethod( client
.getBaseUri() + WebdavUtils
.encodePath(mFile
.getRemotePath()),
114 client
.getBaseUri() + WebdavUtils
.encodePath(mNewRemotePath
));
115 int status
= client
.executeMethod(move
, RENAME_READ_TIMEOUT
, RENAME_CONNECTION_TIMEOUT
);
116 if (move
.succeeded()) {
118 if (mFile
.isFolder()) {
119 saveLocalDirectory();
127 *} else if (mFile.isDirectory() && (status == 207 || status >= 500)) {
129 * // if server fails in the rename of a folder, some children files could have been moved to a folder with the new name while some others
130 * // stayed in the old folder;
132 * // easiest and heaviest solution is synchronizing the parent folder (or the full account);
134 * // a better solution is synchronizing the folders with the old and new names;
140 move
.getResponseBodyAsString(); // exhaust response, although not interesting
141 result
= new RemoteOperationResult(move
.succeeded(), status
, move
.getResponseHeaders());
142 Log_OC
.i(TAG
, "Rename " + mFile
.getRemotePath() + " to " + mNewRemotePath
+ ": " + result
.getLogMessage());
144 } catch (Exception e
) {
145 result
= new RemoteOperationResult(e
);
146 Log_OC
.e(TAG
, "Rename " + mFile
.getRemotePath() + " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " + result
.getLogMessage(), e
);
150 move
.releaseConnection();
156 private void saveLocalDirectory() {
157 mStorageManager
.moveFolder(mFile
, mNewRemotePath
);
158 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
159 File localDir
= new File(localPath
);
160 if (localDir
.exists()) {
161 localDir
.renameTo(new File(FileStorageUtils
.getSavePath(mAccount
.name
) + mNewRemotePath
));
162 // TODO - if renameTo fails, children files that are already down will result unlinked
166 private void saveLocalFile() {
167 mFile
.setFileName(mNewName
);
169 // try to rename the local copy of the file
170 if (mFile
.isDown()) {
171 File f
= new File(mFile
.getStoragePath());
172 String parentStoragePath
= f
.getParent();
173 if (!parentStoragePath
.endsWith(File
.separator
))
174 parentStoragePath
+= File
.separator
;
175 if (f
.renameTo(new File(parentStoragePath
+ mNewName
))) {
176 mFile
.setStoragePath(parentStoragePath
+ mNewName
);
178 // else - NOTHING: the link to the local file is kept although the local name can't be updated
179 // TODO - study conditions when this could be a problem
182 mStorageManager
.saveFile(mFile
);
186 * Checks if the new name to set is valid in the file system
188 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
189 * for downloads, out of any account, and then removed.
191 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
192 * could be formatted with a different file system.
194 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
196 * @return 'True' if a temporal file named with the name to set could be created in the file system where
197 * local files are stored.
198 * @throws IOException When the temporal folder can not be created.
200 private boolean isValidNewName() throws IOException
{
201 // check tricky names
202 if (mNewName
== null
|| mNewName
.length() <= 0 || mNewName
.contains(File
.separator
) || mNewName
.contains("%")) {
205 // create a test file
206 String tmpFolderName
= FileStorageUtils
.getTemporalPath("");
207 File testFile
= new File(tmpFolderName
+ mNewName
);
208 File tmpFolder
= testFile
.getParentFile();
210 if (!tmpFolder
.isDirectory()) {
211 throw new IOException("Unexpected error: temporal directory could not be created");
214 testFile
.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
215 } catch (IOException e
) {
216 Log_OC
.i(TAG
, "Test for validity of name " + mNewName
+ " in the file system failed");
219 boolean result
= (testFile
.exists() && testFile
.isFile());
221 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...
229 private class LocalMoveMethod
extends DavMethodBase
{
231 public LocalMoveMethod(String uri
, String dest
) {
233 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
237 public String
getName() {
242 protected boolean isSuccess(int status
) {
243 return status
== 201 || status
== 204;