/usr/share/doc/libjcifs-java/examples/FileOps.java is in libjcifs-java-doc 1.3.19-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | /* Test the following file operations:
*
* canRead
* false - a target that is already open by another process
* false - the target does not exist
* true - the file exists and there are no sharing issues
* canWrite
* true - the file exists and there are no sharing issues
* false - the file is marked read-only
* false - the file does not exist
* delete
* true - the file existed and was succcessfully deleted
* false - the target did not exist
* false - the target is a share, server, workgroup or similar
* false - the target or file under the target directory failed was read-only
* exists
* true - the target, share, IPC share, named pipe, server, or workgroup exists
* false - the opposite of the above
* isDirectory
* true - the target is a workgroup, server, share, or directory
* false - the target is not one of the above or does not exist
* isFile
* direct opposite of isDirectory
* isHidden
* true - target is share ending in $ or marked as hidden
* length
* the file was created to be no larger than ~2G and reports back the size specified
* mkdir
* true - a directory was created successfuly
* false - the directory could not be created
* renameTo
* true - the target was renamed
*/
import jcifs.smb.*;
import java.io.IOException;
import java.util.Date;
public class FileOps {
static final int ATTR_ALL = SmbFile.ATTR_ARCHIVE | SmbFile.ATTR_HIDDEN | SmbFile.ATTR_READONLY | SmbFile.ATTR_SYSTEM;
public static void main( String argv[] ) throws Exception {
if( argv.length != 1 ) {
System.out.println( "Must provide an SMB URL of a remote location on which tests will be conducted." );
System.exit( 1 );
}
SmbFile s = new SmbFile( argv[0] );
SmbFile d = new SmbFile( s, "JcifsTestOpsDir/" );
// delete - Delete the directory if it exists
try {
d.delete();
} catch( SmbException se ) {
System.out.println( "okay - delete " + d + " failed: " + se.getMessage() );
}
System.out.println( "okay - delete " + d + " successful" );
// exists - Test the directory that should not exist
if( d.exists() ) {
System.out.println( "fail - " + d + " still exists" );
System.exit( 1 );
} else {
System.out.println( "okay - " + d + " does not exist" );
}
// mkdir - Create the directory
d.mkdir();
System.out.println( "okay - mkdir " + d + " successful" );
// exist - Test the directory which should exist now
if( d.exists() ) {
System.out.println( "okay - " + d + " exists" );
} else {
System.out.println( "fail - " + d + " was not successfuly created" );
System.exit( 1 );
}
// mkdir - Try to create a directory even though it already exists
try {
d.mkdir();
System.out.println( "fail - mkdir " + d + " successful" );
System.exit( 1 );
} catch( SmbException se ) {
System.out.println( "okay - mkdir " + d + " failed: " + se.getMessage() );
}
// Create a file to test against
SmbFile f = null;
try {
f = new SmbFile( d, "foo.txt" );
SmbFileOutputStream o = new SmbFileOutputStream( f );
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() );
o.close();
} catch( IOException ioe ) {
System.out.println( "fail - could not create file " + d + "foo.txt: " + ioe.getMessage() );
}
System.out.println( "okay - created file " + d + "foo.txt" );
// canRead - Test to see if the new file can be read
if( f.canRead() ) {
System.out.println( "okay - canRead " + f + " successful" );
} else {
System.out.println( "fail - canRead " + f + " failed" );
System.exit( 1 );
}
// canWrite, getAttributes - Test the file for writing
if( f.canWrite() && (f.getAttributes() & SmbFile.ATTR_READONLY) == 0 ) {
System.out.println( "okay - canWrite " + f + " successful" );
} else {
System.out.println( "fail - canWrite " + f + " failed" );
System.exit( 1 );
}
// setReadOnly
try {
f.setReadOnly();
System.out.println( "okay - setReadOnly " + f + " successful" );
} catch( SmbException se ) {
System.out.println( "fail - setReadOnly " + f + " failed: " + se.getMessage() );
}
// canWrite - Test the file for writing
if( f.canWrite() ) {
System.out.println( "fail - canWrite " + f + " returned true but it should have been marked read-only ... continuing on" );
} else {
System.out.println( "okay - canWrite " + f + " failed" );
}
// Try to open the file for writing
try {
SmbFileOutputStream w = new SmbFileOutputStream( f );
w.close();
System.out.println( "fail - successfuly opened " + f + " for writing even though it should be marked read-only ... continuing on" );
} catch( IOException ioe ) {
System.out.println( "okay - correctly failed to open " + f + " for writing: " + ioe.getMessage() );
}
// renameTo - rename the file to bar.txt
SmbFile b = new SmbFile( d, "bar.txt" );
try {
f.renameTo( b );
System.out.println( "okay - renameTo " + f + " to " + b + " successful even with read-only" );
try {
b.renameTo(f);
} catch( SmbException se ) {
System.out.println( "fail - but failed to rename file back to original!" );
throw se;
}
} catch( SmbException se ) {
System.out.println( "fail - renameTo " + f + " should have been successful even though the file is marked read-only: " + se.getMessage() );
}
// setAttributes
try {
f.setAttributes( 0xFFFF );
System.out.println( "okay - setAttributes " + f + " successful" );
} catch( SmbException se ) {
System.out.println( "fail - setAttributes " + f + " failed: " + se.getMessage() );
}
// getAttributes
int attr;
if((( attr = f.getAttributes() ) & ATTR_ALL ) == ATTR_ALL ) {
System.out.println( "okay - getAttributes " + f + " successful" );
} else {
System.out.println( "fail - getAttributes " + f + " failed: 0x" + jcifs.util.Hexdump.toHexString( attr, 4 ));
System.exit( 1 );
}
// isHidden - Test to see if the file is hidden
if( f.isHidden() ) {
System.out.println( "okay - isHidden " + f + " is hidden" );
} else {
System.out.println( "fail - isHidden " + f + " is not hidden but it should be ... continuing on" );
}
// canRead - Test canRead again with both hidden and read-only on
if( f.canRead() ) {
System.out.println( "okay - canRead " + f + " was successful with read-only and hidden both on" );
} else {
System.out.println( "fail - canRead " + f + " failed with read-only and hidden both on" );
}
// canWrite - Test the file for writing again with read-only and hidden
if( f.canWrite() ) {
System.out.println( "fail - canWrite " + f + " was successful even though read-only is set ... continuing on" );
} else {
System.out.println( "okay - canWrite " + f + " failed as it should being that read-only is set" );
}
// isDirectory - Test file as a directory
if( f.isDirectory() ) {
System.out.println( "fail - isDirectory " + f + " returned true but it is NOT a directory" );
} else {
System.out.println( "okay - isDirectory " + f + " is not a directory" );
}
// isDirectory - Test directory as a directory
if( d.isDirectory() ) {
System.out.println( "okay - isDirectory " + d + " is a directory" );
} else {
System.out.println( "fail - isDirectory " + d + " returned false but it really is a directory" );
}
// isDirectory - Test directory that does not exist
b = new SmbFile( d, "bogus" );
if( b.isDirectory() ) {
System.out.println( "fail - isDirectory " + b + " returned true but it does not exist" );
} else {
System.out.println( "okay - isDirectory " + b + " does not exist" );
}
// isFile - Test file as a file
if( f.isFile() ) {
System.out.println( "okay - isFile " + f + " is a file" );
} else {
System.out.println( "fail - isFile " + f + " return false but it is NOT a file" );
}
// isFile - Test directory as a file
if( d.isFile() ) {
System.out.println( "fail - isFile " + d + " returned true but it is NOT a file" );
} else {
System.out.println( "okay - isFile " + d + " is not a file" );
}
// length - Check to ensure that the length of the file is correct
if( f.length() == 363 ) {
System.out.println( "okay - length " + f + " is correct" );
} else {
System.out.println( "fail - length " + f + " is wrong: " + f.length() );
}
// setReadWrite
try {
f.setReadWrite();
System.out.println( "okay - setReadWrite " + f + " successful" );
} catch( SmbException se ) {
System.out.println( "fail - setReadWrite " + f + " failed: " + se.getMessage() );
}
// setLastModified
long t = (new Date()).getTime() - 1000 * 60;
try {
f.setLastModified( t );
System.out.println( "okay - setLastModified " + f + " successful" );
} catch( SmbException se ) {
System.out.println( "fail - setLastModified " + f + " failed: " + se.getMessage() );
}
// lastModified
if( f.lastModified() == t ) {
System.out.println( "okay - lastModified " + f + " is correct" );
} else {
System.out.println( "fail - lastModified " + f + " is wrong: " + f.lastModified() + " vs " + t );
}
// setCreateTime
try {
f.setCreateTime( t );
System.out.println( "okay - setCreateTime " + f + " successful" );
} catch( SmbException se ) {
System.out.println( "fail - setCreateTime " + f + " failed: " + se.getMessage() );
}
// createTime
if( f.createTime() == t ) {
System.out.println( "okay - createTime " + f + " is correct" );
} else {
System.out.println( "fail - createTime " + f + " is wrong: " + f.createTime() + " vs " + t );
}
// createNewFile
// delete - See if we can delete the file even though it's read-only
try {
f.delete();
System.out.println( "okay - delete " + f + " successful even though the file was read-only" );
} catch( SmbException se ) {
System.out.println( "fail - delete " + f + " should have turned off the read-only attribute to deleted the file: " + se.getMessage() );
}
SmbFile r = new SmbFile( d.getParent(), "JcifsDeleteMe/" );
// Must delete any left over directory from a previous run
try {
r.delete();
System.out.println( "okay - delete " + r + " successful" );
} catch( SmbException se ) {
System.out.println( "okay - delete " + r + " probably wasn't there: " + se.getMessage() );
}
// renameTo - Rename the whole directory to JcifsDeleteMe
try {
d.renameTo( r );
System.out.println( "okay - renameTo " + d + " successful even though it is a directory" );
} catch( SmbException se ) {
System.out.println( "fail - renameTo " + d + " failed: " + se.getMessage() );
}
// delete - Now delete the whole workspace
try {
r.delete();
System.out.println( "okay - delete " + r + " successful" );
} catch( SmbException se ) {
System.out.println( "fail - delete " + r + " failed: " + se.getMessage() );
}
}
}
|