1 34 35 import jcifs.smb.*; 36 import java.io.IOException ; 37 import java.util.Date ; 38 39 public class FileOps { 40 41 static final int ATTR_ALL = SmbFile.ATTR_ARCHIVE | SmbFile.ATTR_HIDDEN | SmbFile.ATTR_READONLY | SmbFile.ATTR_SYSTEM; 42 43 public static void main( String argv[] ) throws Exception { 44 45 if( argv.length != 1 ) { 46 System.out.println( "Must provide an SMB URL of a remote location on which tests will be conducted." ); 47 System.exit( 1 ); 48 } 49 50 SmbFile s = new SmbFile( argv[0] ); 51 SmbFile d = new SmbFile( s, "JcifsTestOpsDir/" ); 52 53 55 try { 56 d.delete(); 57 } catch( SmbException se ) { 58 System.out.println( "okay - delete " + d + " failed: " + se.getMessage() ); 59 } 60 System.out.println( "okay - delete " + d + " successful" ); 61 62 64 if( d.exists() ) { 65 System.out.println( "fail - " + d + " still exists" ); 66 System.exit( 1 ); 67 } else { 68 System.out.println( "okay - " + d + " does not exist" ); 69 } 70 71 73 d.mkdir(); 74 System.out.println( "okay - mkdir " + d + " successful" ); 75 76 78 if( d.exists() ) { 79 System.out.println( "okay - " + d + " exists" ); 80 } else { 81 System.out.println( "fail - " + d + " was not successfuly created" ); 82 System.exit( 1 ); 83 } 84 85 87 try { 88 d.mkdir(); 89 System.out.println( "fail - mkdir " + d + " successful" ); 90 System.exit( 1 ); 91 } catch( SmbException se ) { 92 System.out.println( "okay - mkdir " + d + " failed: " + se.getMessage() ); 93 } 94 95 97 SmbFile f = null; 98 try { 99 f = new SmbFile( d, "foo.txt" ); 100 SmbFileOutputStream o = new SmbFileOutputStream( f ); 101 o.write( "The Common Internet File System (CIFS) is the de-facto file sharing protocol on the Microsoft Windows platform. It is the underlying networking protocol used when accessing shares with Windows Explorer, the Network Neighborhood, via a Map Network Drive... dialog, the C:\\> net use * \\\\server\\share commands, or smbclient on UNIX, smbfs on Linux, and elsewhere.\r\n".getBytes() ); 102 o.close(); 103 } catch( IOException ioe ) { 104 System.out.println( "fail - could not create file " + d + "foo.txt: " + ioe.getMessage() ); 105 } 106 System.out.println( "okay - created file " + d + "foo.txt" ); 107 108 110 if( f.canRead() ) { 111 System.out.println( "okay - canRead " + f + " successful" ); 112 } else { 113 System.out.println( "fail - canRead " + f + " failed" ); 114 System.exit( 1 ); 115 } 116 117 119 if( f.canWrite() && (f.getAttributes() & SmbFile.ATTR_READONLY) == 0 ) { 120 System.out.println( "okay - canWrite " + f + " successful" ); 121 } else { 122 System.out.println( "fail - canWrite " + f + " failed" ); 123 System.exit( 1 ); 124 } 125 126 128 try { 129 f.setReadOnly(); 130 System.out.println( "okay - setReadOnly " + f + " successful" ); 131 } catch( SmbException se ) { 132 System.out.println( "fail - setReadOnly " + f + " failed: " + se.getMessage() ); 133 } 134 135 137 if( f.canWrite() ) { 138 System.out.println( "fail - canWrite " + f + " returned true but it should have been marked read-only ... continuing on" ); 139 } else { 140 System.out.println( "okay - canWrite " + f + " failed" ); 141 } 142 143 145 try { 146 SmbFileOutputStream w = new SmbFileOutputStream( f ); 147 w.close(); 148 System.out.println( "fail - successfuly opened " + f + " for writing even though it should be marked read-only ... continuing on" ); 149 } catch( IOException ioe ) { 150 System.out.println( "okay - correctly failed to open " + f + " for writing: " + ioe.getMessage() ); 151 } 152 153 155 SmbFile b = new SmbFile( d, "bar.txt" ); 156 157 try { 158 f.renameTo( b ); 159 System.out.println( "okay - renameTo " + f + " to " + b + " successful even with read-only" ); 160 try { 161 b.renameTo(f); 162 } catch( SmbException se ) { 163 System.out.println( "fail - but failed to rename file back to original!" ); 164 throw se; 165 } 166 } catch( SmbException se ) { 167 System.out.println( "fail - renameTo " + f + " should have been successful even though the file is marked read-only: " + se.getMessage() ); 168 } 169 170 172 try { 173 f.setAttributes( 0xFFFF ); 174 System.out.println( "okay - setAttributes " + f + " successful" ); 175 } catch( SmbException se ) { 176 System.out.println( "fail - setAttributes " + f + " failed: " + se.getMessage() ); 177 } 178 179 181 int attr; 182 183 if((( attr = f.getAttributes() ) & ATTR_ALL ) == ATTR_ALL ) { 184 System.out.println( "okay - getAttributes " + f + " successful" ); 185 } else { 186 System.out.println( "fail - getAttributes " + f + " failed: 0x" + jcifs.util.Hexdump.toHexString( attr, 4 )); 187 System.exit( 1 ); 188 } 189 190 192 if( f.isHidden() ) { 193 System.out.println( "okay - isHidden " + f + " is hidden" ); 194 } else { 195 System.out.println( "fail - isHidden " + f + " is not hidden but it should be ... continuing on" ); 196 } 197 198 200 if( f.canRead() ) { 201 System.out.println( "okay - canRead " + f + " was successful with read-only and hidden both on" ); 202 } else { 203 System.out.println( "fail - canRead " + f + " failed with read-only and hidden both on" ); 204 } 205 206 208 if( f.canWrite() ) { 209 System.out.println( "fail - canWrite " + f + " was successful even though read-only is set ... continuing on" ); 210 } else { 211 System.out.println( "okay - canWrite " + f + " failed as it should being that read-only is set" ); 212 } 213 214 216 if( f.isDirectory() ) { 217 System.out.println( "fail - isDirectory " + f + " returned true but it is NOT a directory" ); 218 } else { 219 System.out.println( "okay - isDirectory " + f + " is not a directory" ); 220 } 221 222 224 if( d.isDirectory() ) { 225 System.out.println( "okay - isDirectory " + d + " is a directory" ); 226 } else { 227 System.out.println( "fail - isDirectory " + d + " returned false but it really is a directory" ); 228 } 229 230 232 b = new SmbFile( d, "bogus" ); 233 234 if( b.isDirectory() ) { 235 System.out.println( "fail - isDirectory " + b + " returned true but it does not exist" ); 236 } else { 237 System.out.println( "okay - isDirectory " + b + " does not exist" ); 238 } 239 240 242 if( f.isFile() ) { 243 System.out.println( "okay - isFile " + f + " is a file" ); 244 } else { 245 System.out.println( "fail - isFile " + f + " return false but it is NOT a file" ); 246 } 247 248 250 if( d.isFile() ) { 251 System.out.println( "fail - isFile " + d + " returned true but it is NOT a file" ); 252 } else { 253 System.out.println( "okay - isFile " + d + " is not a file" ); 254 } 255 256 258 if( f.length() == 363 ) { 259 System.out.println( "okay - length " + f + " is correct" ); 260 } else { 261 System.out.println( "fail - length " + f + " is wrong: " + f.length() ); 262 } 263 264 266 try { 267 f.setReadWrite(); 268 System.out.println( "okay - setReadWrite " + f + " successful" ); 269 } catch( SmbException se ) { 270 System.out.println( "fail - setReadWrite " + f + " failed: " + se.getMessage() ); 271 } 272 273 275 long t = (new Date ()).getTime() - 1000 * 60; 276 277 try { 278 f.setLastModified( t ); 279 System.out.println( "okay - setLastModified " + f + " successful" ); 280 } catch( SmbException se ) { 281 System.out.println( "fail - setLastModified " + f + " failed: " + se.getMessage() ); 282 } 283 284 286 if( f.lastModified() == t ) { 287 System.out.println( "okay - lastModified " + f + " is correct" ); 288 } else { 289 System.out.println( "fail - lastModified " + f + " is wrong: " + f.lastModified() + " vs " + t ); 290 } 291 292 294 try { 295 f.setCreateTime( t ); 296 System.out.println( "okay - setCreateTime " + f + " successful" ); 297 } catch( SmbException se ) { 298 System.out.println( "fail - setCreateTime " + f + " failed: " + se.getMessage() ); 299 } 300 301 303 if( f.createTime() == t ) { 304 System.out.println( "okay - createTime " + f + " is correct" ); 305 } else { 306 System.out.println( "fail - createTime " + f + " is wrong: " + f.createTime() + " vs " + t ); 307 } 308 309 311 313 try { 314 f.delete(); 315 System.out.println( "okay - delete " + f + " successful even though the file was read-only" ); 316 } catch( SmbException se ) { 317 System.out.println( "fail - delete " + f + " should have turned off the read-only attribute to deleted the file: " + se.getMessage() ); 318 } 319 320 SmbFile r = new SmbFile( d.getParent(), "JcifsDeleteMe/" ); 321 322 324 try { 325 r.delete(); 326 System.out.println( "okay - delete " + r + " successful" ); 327 } catch( SmbException se ) { 328 System.out.println( "okay - delete " + r + " probably wasn't there: " + se.getMessage() ); 329 } 330 331 333 try { 334 d.renameTo( r ); 335 System.out.println( "okay - renameTo " + d + " successful even though it is a directory" ); 336 } catch( SmbException se ) { 337 System.out.println( "fail - renameTo " + d + " failed: " + se.getMessage() ); 338 } 339 340 342 try { 343 r.delete(); 344 System.out.println( "okay - delete " + r + " successful" ); 345 } catch( SmbException se ) { 346 System.out.println( "fail - delete " + r + " failed: " + se.getMessage() ); 347 } 348 } 349 } 350 351 | Popular Tags |