1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-22 15:53:58 +02:00

new API IFileService.setReadOnly()

This commit is contained in:
David McKnight 2007-01-17 18:06:45 +00:00
parent 141616a037
commit 74822bd918
11 changed files with 159 additions and 24 deletions

View file

@ -19,6 +19,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.rse.model.ISystemResourceChangeEvents; import org.eclipse.rse.model.ISystemResourceChangeEvents;
import org.eclipse.rse.services.files.RemoteFileIOException; import org.eclipse.rse.services.files.RemoteFileIOException;
import org.eclipse.rse.services.files.RemoteFileSecurityException; import org.eclipse.rse.services.files.RemoteFileSecurityException;
@ -220,7 +221,7 @@ public class SystemFilePropertyPage extends SystemBasePropertyPage
{ {
cbReadonlyPrompt.setSelection(!file.canWrite()); cbReadonlyPrompt.setSelection(!file.canWrite());
wasReadOnly = !file.canWrite(); wasReadOnly = !file.canWrite();
if (wasReadOnly || file instanceof IVirtualRemoteFile) if (file instanceof IVirtualRemoteFile)
cbReadonlyPrompt.setEnabled(false); cbReadonlyPrompt.setEnabled(false);
} }
} }
@ -252,11 +253,14 @@ public class SystemFilePropertyPage extends SystemBasePropertyPage
public boolean performOk() public boolean performOk()
{ {
boolean ok = super.performOk(); boolean ok = super.performOk();
if (ok && (cbReadonlyPrompt!=null) && cbReadonlyPrompt.getSelection() && !wasReadOnly) boolean readOnlySelected = cbReadonlyPrompt != null ? cbReadonlyPrompt.getSelection() : false;
if (ok && (cbReadonlyPrompt!=null) &&
((readOnlySelected && !wasReadOnly) ||
(!readOnlySelected && wasReadOnly)))
{ {
try try
{ {
getRemoteFile().getParentRemoteFileSubSystem().setReadOnly(getRemoteFile()); getRemoteFile().getParentRemoteFileSubSystem().setReadOnly(new NullProgressMonitor(), getRemoteFile(), readOnlySelected);
RSEUIPlugin.getTheSystemRegistry().fireEvent( RSEUIPlugin.getTheSystemRegistry().fireEvent(
new org.eclipse.rse.model.SystemResourceChangeEvent( new org.eclipse.rse.model.SystemResourceChangeEvent(
getRemoteFile(),ISystemResourceChangeEvents.EVENT_PROPERTY_CHANGE,null)); getRemoteFile(),ISystemResourceChangeEvents.EVENT_PROPERTY_CHANGE,null));

View file

@ -1257,20 +1257,64 @@ private DataElement createDataElementFromLSString(DataElement subject,
*/ */
public DataElement handleSetReadOnly(DataElement subject, DataElement status) { public DataElement handleSetReadOnly(DataElement subject, DataElement status) {
File filename = new File(subject.getAttribute(DE.A_VALUE) File filename = new File(subject.getAttribute(DE.A_VALUE), subject.getAttribute(DE.A_NAME));
+ File.separatorChar + subject.getName());
if (!filename.exists())
{
filename = new File(subject.getAttribute(DE.A_VALUE));
}
if (!filename.exists()) if (!filename.exists())
status.setAttribute(DE.A_SOURCE, FAILED_WITH_DOES_NOT_EXIST); status.setAttribute(DE.A_SOURCE, FAILED_WITH_DOES_NOT_EXIST);
else { else {
try { try {
boolean done = filename.setReadOnly(); String str = subject.getAttribute(DE.A_SOURCE);
if (done) { boolean readOnly = false;
if (str.equals("true")) //$NON-NLS-1$
{
readOnly = true;
}
else
{
readOnly = false;
}
boolean done = false;
if (readOnly)
{
done = filename.setReadOnly();
}
else
{
// doesn't handle non-unix
if (!_isWindows)
{
// make this read-write
String[] cmd = new String[3];
cmd[0] = "chmod"; //$NON-NLS-1$
cmd[1] = "a+w"; //$NON-NLS-1$
cmd[2] = filename.getAbsolutePath();
Process p = Runtime.getRuntime().exec(cmd);
int exitValue = p.waitFor();
done = (exitValue == 0);
}
else
{
done = false;
}
}
if (done)
{
status.setAttribute(DE.A_SOURCE, SUCCESS); status.setAttribute(DE.A_SOURCE, SUCCESS);
subject.setAttribute(DE.A_SOURCE, setProperties(filename)); }
_dataStore.refresh(subject); else
} else { {
status.setAttribute(DE.A_SOURCE, FAILED); status.setAttribute(DE.A_SOURCE, FAILED);
} }
// update filename?
filename = new File(filename.getAbsolutePath());
subject.setAttribute(DE.A_SOURCE, setProperties(filename));
_dataStore.refresh(subject);
} catch (Exception e) { } catch (Exception e) {
UniversalServerUtilities.logError(CLASSNAME, UniversalServerUtilities.logError(CLASSNAME,
"handleSetreadOnly", e); //$NON-NLS-1$ "handleSetreadOnly", e); //$NON-NLS-1$
@ -1286,7 +1330,12 @@ private DataElement createDataElementFromLSString(DataElement subject,
public DataElement handleSetLastModified(DataElement subject, public DataElement handleSetLastModified(DataElement subject,
DataElement status) DataElement status)
{ {
File filename = new File(subject.getAttribute(DE.A_VALUE)); File filename = new File(subject.getAttribute(DE.A_VALUE), subject.getAttribute(DE.A_NAME));
if (!filename.exists())
{
filename = new File(subject.getAttribute(DE.A_VALUE));
}
if (!filename.exists()) if (!filename.exists())
status.setAttribute(DE.A_SOURCE, FAILED_WITH_DOES_NOT_EXIST); status.setAttribute(DE.A_SOURCE, FAILED_WITH_DOES_NOT_EXIST);
else { else {
@ -1296,13 +1345,18 @@ private DataElement createDataElementFromLSString(DataElement subject,
long date = Long.parseLong(str); long date = Long.parseLong(str);
boolean done = filename.setLastModified(date); boolean done = filename.setLastModified(date);
filename = new File(subject.getAttribute(DE.A_VALUE));
if (done) { if (done) {
status.setAttribute(DE.A_SOURCE, SUCCESS); status.setAttribute(DE.A_SOURCE, SUCCESS);
subject.setAttribute(DE.A_SOURCE, setProperties(filename)); }
_dataStore.refresh(subject); else
} else {
status.setAttribute(DE.A_SOURCE, FAILED); status.setAttribute(DE.A_SOURCE, FAILED);
}
filename = new File(filename.getAbsolutePath());
subject.setAttribute(DE.A_SOURCE, setProperties(filename));
_dataStore.refresh(subject);
} catch (Exception e) { } catch (Exception e) {
UniversalServerUtilities.logError(CLASSNAME, UniversalServerUtilities.logError(CLASSNAME,
"handleSetLastModified", e); //$NON-NLS-1$ "handleSetLastModified", e); //$NON-NLS-1$

View file

@ -1238,8 +1238,6 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
DataElement setCmd = getCommandDescriptor(de, C_SET_LASTMODIFIED); DataElement setCmd = getCommandDescriptor(de, C_SET_LASTMODIFIED);
if (setCmd != null) if (setCmd != null)
{ {
// first modify the source attribute to temporarily be the date field // first modify the source attribute to temporarily be the date field
de.setAttribute(DE.A_SOURCE, timestamp + ""); //$NON-NLS-1$ de.setAttribute(DE.A_SOURCE, timestamp + ""); //$NON-NLS-1$
ds.command(setCmd, de, true); ds.command(setCmd, de, true);
@ -1249,6 +1247,33 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
return false; return false;
} }
public boolean setReadOnly(IProgressMonitor monitor, String parent,
String name, boolean readOnly) throws SystemMessageException
{
String remotePath = parent + getSeparator(parent) + name;
DataElement de = getElementFor(remotePath);
DataStore ds = de.getDataStore();
DataElement setCmd = getCommandDescriptor(de, C_SET_READONLY);
if (setCmd != null)
{
String flag = readOnly ? "true" : "false";
de.setAttribute(DE.A_SOURCE, flag);
DataElement status = ds.command(setCmd, de, true);
try
{
getStatusMonitor(ds).waitForUpdate(status);
}
catch (Exception e)
{
}
return true;
}
return false;
}
} }

View file

@ -356,6 +356,10 @@ public class DStoreHostFile implements IHostFile
public boolean canRead() { public boolean canRead() {
String str = getAttribute(_element.getSource(), ATTRIBUTE_CAN_READ); String str = getAttribute(_element.getSource(), ATTRIBUTE_CAN_READ);
if (str == null)
{
System.out.println("HELP:"+_element.toString());
}
return(str.equals("true")); //$NON-NLS-1$ return(str.equals("true")); //$NON-NLS-1$
} }

View file

@ -987,6 +987,12 @@ public class FTPService extends AbstractFileService implements IFileService, IFT
// TODO - implement this to set the timestamp // TODO - implement this to set the timestamp
return false; return false;
} }
public boolean setReadOnly(IProgressMonitor monitor, String parent,
String name, boolean readOnly) throws SystemMessageException {
// TODO Auto-generated method stub
return false;
}
} }

View file

@ -1316,5 +1316,20 @@ public class LocalFileService extends AbstractFileService implements IFileServic
return file.setLastModified(timestamp); return file.setLastModified(timestamp);
} }
public boolean setReadOnly(IProgressMonitor monitor, String parent,
String name, boolean readOnly) throws SystemMessageException
{
File file = new File(parent, name);
if (readOnly)
{
return file.setReadOnly();
}
else
{
// not implemented yet
return false;
}
}
} }

View file

@ -759,4 +759,10 @@ public class SftpFileService extends AbstractFileService implements IFileService
return false; return false;
} }
public boolean setReadOnly(IProgressMonitor monitor, String parent,
String name, boolean readOnly) throws SystemMessageException {
// TODO Auto-generated method stub
return false;
}
} }

View file

@ -274,4 +274,14 @@ public interface IFileService extends IService
* @return true if the file timestamp was changed successfully * @return true if the file timestamp was changed successfully
*/ */
public boolean setLastModified(IProgressMonitor monitor, String parent, String name, long timestamp) throws SystemMessageException; public boolean setLastModified(IProgressMonitor monitor, String parent, String name, long timestamp) throws SystemMessageException;
/**
* Sets the readonly permission of the file or folder
* @param monitor the progress monitor
* @param parent the parent path of the file to set
* @param name the name of the file to set
* @param readOnly indicates whether to make the file readonly or read-write
* @return true if the readonly permission was changed successfully
*/
public boolean setReadOnly(IProgressMonitor monitor, String parent, String name, boolean readOnly) throws SystemMessageException;
} }

View file

@ -188,8 +188,7 @@ public abstract class AbstractRemoteFile extends RemoteFile implements IRemoteFi
public boolean showReadOnlyProperty() public boolean showReadOnlyProperty()
{ {
// TODO Auto-generated method stub return true;
return false;
} }
public IHostFile getHostFile() public IHostFile getHostFile()

View file

@ -778,10 +778,20 @@ public final class FileServiceSubSystem extends RemoteFileSubSystem implements I
} }
} }
public boolean setReadOnly(IRemoteFile folderOrFile) throws RemoteFileSecurityException, RemoteFileIOException public boolean setReadOnly(IProgressMonitor monitor, IRemoteFile folderOrFile, boolean readOnly) throws RemoteFileSecurityException, RemoteFileIOException
{ {
// TODO Auto-generated method stub String name = folderOrFile.getName();
return false; String parent = folderOrFile.getParentPath();
try
{
return _hostFileService.setReadOnly(monitor, parent, name, readOnly);
}
catch (SystemMessageException e)
{
SystemMessageDialog dlg = new SystemMessageDialog(getShell(), e.getSystemMessage());
dlg.open();
return false;
}
} }
public ILanguageUtilityFactory getLanguageUtilityFactory() public ILanguageUtilityFactory getLanguageUtilityFactory()

View file

@ -354,13 +354,15 @@ public interface IRemoteFileSubSystem extends ISubSystem{
public boolean setLastModified(IProgressMonitor monitor, IRemoteFile folderOrFile, long newDate) throws RemoteFileSecurityException, RemoteFileIOException; public boolean setLastModified(IProgressMonitor monitor, IRemoteFile folderOrFile, long newDate) throws RemoteFileSecurityException, RemoteFileIOException;
/** /**
* Set a file to readonly. * Set a files readonly permissions.
* Folder or file must exist on disk for this to succeed. * Folder or file must exist on disk for this to succeed.
* *
* @param monitor the progress monitor
* @param folderOrFile represents the object to be renamed. * @param folderOrFile represents the object to be renamed.
* @param readOnly whether to set it to be readonly or not
* @return false if the given folder/file didn't exist on disk (operation fails), else true. Throws an exception if anything fails. * @return false if the given folder/file didn't exist on disk (operation fails), else true. Throws an exception if anything fails.
*/ */
public boolean setReadOnly(IRemoteFile folderOrFile) throws RemoteFileSecurityException, RemoteFileIOException; public boolean setReadOnly(IProgressMonitor monitor, IRemoteFile folderOrFile, boolean readOnly) throws RemoteFileSecurityException, RemoteFileIOException;
// ---------------------------- // ----------------------------