1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 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 com
.owncloud
.android
.datamodel
.OCFile
;
24 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
25 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
26 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
27 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
28 import com
.owncloud
.android
.lib
.resources
.files
.RenameRemoteFileOperation
;
29 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
36 * @author David A. Velasco
39 public class RenameFileOperation
extends SyncOperation
{
41 private static final String TAG
= RenameFileOperation
.class.getSimpleName();
44 private String mRemotePath
;
45 private String mNewName
;
46 private String mNewRemotePath
;
53 * @param remotePath RemotePath of the OCFile instance describing the remote file or folder to rename
54 * @param newName New name to set as the name of file.
56 public RenameFileOperation(String remotePath
, String newName
) {
57 mRemotePath
= remotePath
;
59 mNewRemotePath
= null
;
62 public OCFile
getFile() {
68 * Performs the rename operation.
70 * @param client Client object to communicate with the remote ownCloud server.
73 protected RemoteOperationResult
run(OwnCloudClient client
) {
74 RemoteOperationResult result
= null
;
76 mFile
= getStorageManager().getFileByPath(mRemotePath
);
78 // check if the new name is valid in the local file system
80 if (!isValidNewName()) {
81 return new RemoteOperationResult(ResultCode
.INVALID_LOCAL_FILE_NAME
);
83 String parent
= (new File(mFile
.getRemotePath())).getParent();
84 parent
= (parent
.endsWith(OCFile
.PATH_SEPARATOR
)) ? parent
: parent
+ OCFile
.PATH_SEPARATOR
;
85 mNewRemotePath
= parent
+ mNewName
;
86 if (mFile
.isFolder()) {
87 mNewRemotePath
+= OCFile
.PATH_SEPARATOR
;
90 // check local overwrite
91 if (getStorageManager().getFileByPath(mNewRemotePath
) != null
) {
92 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
95 RenameRemoteFileOperation operation
= new RenameRemoteFileOperation(mFile
.getFileName(), mFile
.getRemotePath(),
96 mNewName
, mFile
.isFolder());
97 result
= operation
.execute(client
);
99 if (result
.isSuccess()) {
100 if (mFile
.isFolder()) {
101 getStorageManager().moveLocalFile(mFile
, mNewRemotePath
, parent
);
102 //saveLocalDirectory();
109 } catch (IOException e
) {
110 Log_OC
.e(TAG
, "Rename " + mFile
.getRemotePath() + " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " +
111 ((result
!= null
) ? result
.getLogMessage() : ""), e
);
117 private void saveLocalFile() {
118 mFile
.setFileName(mNewName
);
120 // try to rename the local copy of the file
121 if (mFile
.isDown()) {
122 String oldPath
= mFile
.getStoragePath();
123 File f
= new File(oldPath
);
124 String parentStoragePath
= f
.getParent();
125 if (!parentStoragePath
.endsWith(File
.separator
))
126 parentStoragePath
+= File
.separator
;
127 if (f
.renameTo(new File(parentStoragePath
+ mNewName
))) {
128 String newPath
= parentStoragePath
+ mNewName
;
129 mFile
.setStoragePath(newPath
);
131 // notify MediaScanner about removed file
132 getStorageManager().deleteFileInMediaScan(oldPath
);
133 // notify to scan about new file
134 getStorageManager().triggerMediaScan(newPath
);
136 // else - NOTHING: the link to the local file is kept although the local name can't be updated
137 // TODO - study conditions when this could be a problem
140 getStorageManager().saveFile(mFile
);
144 * Checks if the new name to set is valid in the file system
146 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
147 * for downloads, out of any account, and then removed.
149 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
150 * could be formatted with a different file system.
152 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
154 * @return 'True' if a temporal file named with the name to set could be created in the file system where
155 * local files are stored.
156 * @throws IOException When the temporal folder can not be created.
158 private boolean isValidNewName() throws IOException
{
159 // check tricky names
160 if (mNewName
== null
|| mNewName
.length() <= 0 || mNewName
.contains(File
.separator
)) {
163 // create a test file
164 String tmpFolderName
= FileStorageUtils
.getTemporalPath("");
165 File testFile
= new File(tmpFolderName
+ mNewName
);
166 File tmpFolder
= testFile
.getParentFile();
168 if (!tmpFolder
.isDirectory()) {
169 throw new IOException("Unexpected error: temporal directory could not be created");
172 testFile
.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
173 } catch (IOException e
) {
174 Log_OC
.i(TAG
, "Test for validity of name " + mNewName
+ " in the file system failed");
177 boolean result
= (testFile
.exists() && testFile
.isFile());
179 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...