Source code: com/flexstor/common/io/xfile/webnfs/WebNfsDelete.java
1 /*
2 * WebNfsDelete.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.io.xfile.webnfs;
12
13 import com.flexstor.common.errorlogger.FlexError;
14 import com.flexstor.common.io.xfile.XFileDeleteI;
15 import com.sun.nfs.XFileExtensionAccessor;
16
17
18 /**
19 * WebNfsDelete uses WebNFS to delete files from the filesystem
20 * This Class assumes that all paths will be preceded by nfs:// if on another
21 * filesystem. It will make no attempt at determining if the path
22 * is local or on another server.
23 **/
24 public class WebNfsDelete
25 implements XFileDeleteI
26 {
27 // To get the version number from MKS
28 public final static String IDENTIFIER="$Id: WebNfsDelete.java,v 1.2 2003/08/11 02:22:31 aleric Exp $";
29
30 private String sThisService = "XFileDelete";
31 private XFileExtensionAccessor xfeaNFS = null;
32 private String sHost;
33 private String sUser;
34 private String sPassword;
35 private boolean bDoLogin = false;
36 private boolean bConnected = false;
37
38
39 /**
40 * Connects to host using the credentials specified.
41 * Returns true if connection was sucessful; false otherwise.
42 */
43 public boolean connect( String sHost, String sUser, String sPassword )
44 {
45 this.sHost = sHost;
46 this.sUser = sUser;
47 this.sPassword = sPassword;
48 this.bDoLogin = true;
49 // This method will always return true; the real connect test will take place during every
50 // request.
51 return true;
52 }
53
54 private boolean connect( String sPath )
55 {
56 if ( !bConnected && bDoLogin )
57 {
58 WebNfsFile xFile = new WebNfsFile( sPath );
59 xfeaNFS = new XFileExtensionAccessor( xFile );
60
61 if ( xfeaNFS == null )
62 {
63 // 5535=Unable to get Extension Accessor for File System: %%1 .
64 new FlexError(FlexError.CRITICAL, sThisService, IDENTIFIER, 5535, new String[] { xFile.getFileSystemName() });
65 return bConnected;
66 }
67
68 if ( xfeaNFS.loginPCNFSD( sHost, sUser, sPassword ) == false )
69 {
70 // 5532=Invalid host, user name or password.
71 new FlexError(FlexError.CRITICAL, sThisService, IDENTIFIER, 5532);
72 return bConnected;
73 }
74 }
75
76 bConnected = true;
77 return bConnected;
78 }
79
80 /**
81 * Disconnects from host.
82 */
83 public void disconnect()
84 {
85 if ( xfeaNFS != null )
86 xfeaNFS.logoutPCNFSD();
87 }
88
89 public boolean deleteFile( String sSource )
90 {
91 return deleteFile( sSource, true );
92 }
93
94 public boolean deleteFile( String sSource, boolean bRemoveParentIfEmpty )
95 {
96 System.out.println("Deleting " + sSource);
97
98 if ( connect( sSource ) )
99 {
100 WebNfsFile source = new WebNfsFile( sSource );
101 if (source.exists() && source.canWrite() && source.isFile())
102 {
103 source.delete();
104 if ( bRemoveParentIfEmpty )
105 deleteParent( source.getParent() );
106
107 return true;
108 }
109 else
110 {
111 //5510=Unable to read input file: %%1
112 System.out.println("Unable to read input file, delete failed");
113 new FlexError(FlexError.CRITICAL, sThisService, IDENTIFIER, 5510, new String[] { source.getAbsolutePath() });
114 return false;
115 }
116 }
117 else
118 return false;
119 }
120
121 private void deleteParent( String sParentDir )
122 {
123 WebNfsFile parentDir = new WebNfsFile( sParentDir );
124
125 if ( parentDir.exists() )
126 parentDir.delete();
127 }
128
129 public boolean deleteDirectory( String sDir )
130 {
131 return deleteDirectory( sDir, true );
132 }
133
134 public boolean deleteDirectory( String sDir, boolean bDeleteContent )
135 {
136 if ( connect( sDir ) )
137 {
138 WebNfsFile dir = new WebNfsFile(sDir);
139 if ( dir.isDirectory() == false )
140 return false;
141
142 String[] saContent = dir.list();
143 if ( saContent != null && saContent.length > 0 )
144 {
145 if ( bDeleteContent )
146 {
147 for ( int i = 0; i < saContent.length; i++ )
148 {
149 WebNfsFile file = new WebNfsFile( dir, saContent[i] );
150 if ( file.isDirectory() )
151 deleteDirectory( file.toString(), true );
152 else
153 file.delete();
154 }
155 return dir.delete();
156 }
157 else
158 return false; // Directory isn't empty
159 }
160 else
161 return dir.delete();
162 }
163 else
164 return false;
165 }
166 }