1 17 package org.alfresco.filesys.smb.server.repo; 18 19 import org.alfresco.filesys.locking.FileLock; 20 import org.alfresco.filesys.locking.FileLockList; 21 import org.alfresco.filesys.locking.LockConflictException; 22 import org.alfresco.filesys.locking.NotLockedException; 23 import org.alfresco.filesys.server.filesys.FileName; 24 import org.alfresco.filesys.server.filesys.FileOpenParams; 25 import org.alfresco.filesys.server.filesys.FileStatus; 26 import org.alfresco.filesys.smb.SharingMode; 27 import org.alfresco.filesys.smb.server.repo.pseudo.PseudoFile; 28 import org.alfresco.filesys.smb.server.repo.pseudo.PseudoFileList; 29 import org.alfresco.service.cmr.repository.NodeRef; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 41 public class FileState 42 { 43 private static final Log logger = LogFactory.getLog(FileState.class); 44 45 47 public final static long NoTimeout = -1L; 48 public final static long DefTimeout = 2 * 60000L; public final static long RenameTimeout = 1 * 60000L; 51 53 public enum FileStateStatus { NotExist, FileExists, FolderExists, Renamed }; 54 55 57 private String m_path; 58 59 61 private long m_tmo; 62 63 65 private FileStateStatus m_fileStatus = FileStateStatus.NotExist; 66 67 69 private int m_openCount; 70 71 73 private int m_sharedAccess = SharingMode.READWRITE; 74 75 77 private FileLockList m_lockList; 78 79 81 private NodeRef m_nodeRef; 82 83 85 private FileState m_newNameState; 86 87 89 private PseudoFileList m_pseudoFiles; 90 91 97 public FileState(String fname, boolean isdir) 98 { 99 100 102 setPath(fname); 103 setExpiryTime(System.currentTimeMillis() + DefTimeout); 104 105 107 setFileStatus( isdir ? FileStateStatus.FolderExists : FileStateStatus.FileExists); 108 } 109 110 115 public final String getPath() 116 { 117 return m_path; 118 } 119 120 125 public final FileStateStatus getFileStatus() 126 { 127 return m_fileStatus; 128 } 129 130 135 public final boolean exists() 136 { 137 if ( m_fileStatus == FileStateStatus.FileExists || 138 m_fileStatus == FileStateStatus.FolderExists) 139 return true; 140 return false; 141 } 142 143 148 public final boolean isDirectory() 149 { 150 return m_fileStatus == FileStateStatus.FolderExists ? true : false; 151 } 152 153 158 public final boolean hasNodeRef() 159 { 160 return m_nodeRef != null ? true : false; 161 } 162 163 168 public final NodeRef getNodeRef() 169 { 170 return m_nodeRef; 171 } 172 173 178 public final int getOpenCount() 179 { 180 return m_openCount; 181 } 182 183 188 public final int getSharedAccess() 189 { 190 return m_sharedAccess; 191 } 192 193 198 public final boolean hasActiveLocks() 199 { 200 if (m_lockList != null && m_lockList.numberOfLocks() > 0) 201 return true; 202 return false; 203 } 204 205 210 public final boolean hasNoTimeout() 211 { 212 return m_tmo == NoTimeout ? true : false; 213 } 214 215 222 public final boolean allowsOpen(FileOpenParams params) 223 { 224 225 227 if (getOpenCount() == 0) 228 return true; 229 230 232 if (getSharedAccess() == SharingMode.READWRITE && params.getSharedAccess() == SharingMode.READWRITE) 233 return true; 234 else if ((getSharedAccess() & SharingMode.READ) != 0 && params.isReadOnlyAccess()) 235 return true; 236 else if ((getSharedAccess() & SharingMode.WRITE) != 0 && params.isWriteOnlyAccess()) 237 return true; 238 239 241 return false; 242 } 243 244 249 public final synchronized int incrementOpenCount() 250 { 251 return m_openCount++; 252 } 253 254 259 public final synchronized int decrementOpenCount() 260 { 261 262 264 if (m_openCount <= 0) 265 logger.debug("@@@@@ File close name=" + getPath() + ", count=" + m_openCount + " <<ERROR>>"); 266 else 267 m_openCount--; 268 269 return m_openCount; 270 } 271 272 278 public final boolean hasExpired(long curTime) 279 { 280 if (m_tmo == NoTimeout) 281 return false; 282 if (curTime > m_tmo) 283 return true; 284 return false; 285 } 286 287 293 public final long getSecondsToExpire(long curTime) 294 { 295 if (m_tmo == NoTimeout) 296 return -1; 297 return (m_tmo - curTime) / 1000L; 298 } 299 300 305 public final boolean hasRenameState() 306 { 307 return m_newNameState != null ? true : false; 308 } 309 310 315 public final FileState getRenameState() 316 { 317 return m_newNameState; 318 } 319 320 325 public final boolean hasPseudoFiles() 326 { 327 if ( m_pseudoFiles != null) 328 return m_pseudoFiles.numberOfFiles() > 0; 329 return false; 330 } 331 332 337 public final PseudoFileList getPseudoFileList() 338 { 339 return m_pseudoFiles; 340 } 341 342 347 public final void addPseudoFile(PseudoFile pfile) 348 { 349 if ( m_pseudoFiles == null) 350 m_pseudoFiles = new PseudoFileList(); 351 m_pseudoFiles.addFile( pfile); 352 } 353 354 359 public final void setFileStatus(FileStateStatus status) 360 { 361 m_fileStatus = status; 362 } 363 364 369 public final void setFileStatus(int fsts) 370 { 371 if ( fsts == FileStatus.FileExists) 372 m_fileStatus = FileStateStatus.FileExists; 373 else if ( fsts == FileStatus.DirectoryExists) 374 m_fileStatus = FileStateStatus.FolderExists; 375 else if ( fsts == FileStatus.NotExist) 376 m_fileStatus = FileStateStatus.NotExist; 377 } 378 379 384 public final void setExpiryTime(long expire) 385 { 386 m_tmo = expire; 387 } 388 389 394 public final void setNodeRef(NodeRef nodeRef) 395 { 396 m_nodeRef = nodeRef; 397 } 398 399 404 public final void setRenameState(FileState fstate) 405 { 406 m_newNameState = fstate; 407 } 408 409 414 public final void setSharedAccess(int mode) 415 { 416 if (getOpenCount() == 0) 417 m_sharedAccess = mode; 418 } 419 420 425 public final void setPath(String path) 426 { 427 428 431 m_path = normalizePath(path); 432 } 433 434 439 public final int numberOfLocks() 440 { 441 if (m_lockList != null) 442 return m_lockList.numberOfLocks(); 443 return 0; 444 } 445 446 452 public final void addLock(FileLock lock) throws LockConflictException 453 { 454 455 457 if (m_lockList == null) 458 { 459 460 synchronized (this) 461 { 462 463 466 if (m_lockList == null) 467 m_lockList = new FileLockList(); 468 } 469 } 470 471 473 synchronized (m_lockList) 474 { 475 476 478 if (m_lockList.allowsLock(lock)) 479 { 480 481 483 m_lockList.addLock(lock); 484 } 485 else 486 throw new LockConflictException(); 487 } 488 } 489 490 496 public final void removeLock(FileLock lock) throws NotLockedException 497 { 498 499 501 if (m_lockList == null) 502 throw new NotLockedException(); 503 504 506 synchronized (m_lockList) 507 { 508 509 511 if (m_lockList.removeLock(lock) == null) 512 throw new NotLockedException(); 513 } 514 } 515 516 524 public final boolean canReadFile(long offset, long len, int pid) 525 { 526 527 529 if (m_lockList == null) 530 return true; 531 532 534 boolean readOK = false; 535 536 synchronized (m_lockList) 537 { 538 539 541 readOK = m_lockList.canReadFile(offset, len, pid); 542 } 543 544 546 return readOK; 547 } 548 549 557 public final boolean canWriteFile(long offset, long len, int pid) 558 { 559 560 562 if (m_lockList == null) 563 return true; 564 565 567 boolean writeOK = false; 568 569 synchronized (m_lockList) 570 { 571 572 574 writeOK = m_lockList.canWriteFile(offset, len, pid); 575 } 576 577 579 return writeOK; 580 } 581 582 588 public final static String normalizePath(String path) 589 { 590 591 594 String normPath = path; 595 596 if (path.length() > 3) 597 { 598 599 601 int pos = path.lastIndexOf(FileName.DOS_SEPERATOR); 602 if (pos != -1) 603 { 604 605 607 String pathPart = path.substring(0, pos).toUpperCase(); 608 String namePart = path.substring(pos); 609 610 612 normPath = pathPart + namePart; 613 } 614 } 615 616 618 return normPath; 619 } 620 621 626 public String toString() 627 { 628 StringBuffer str = new StringBuffer (); 629 630 str.append("["); 631 str.append(getPath()); 632 str.append(","); 633 str.append(getFileStatus()); 634 str.append(":Opn="); 635 str.append(getOpenCount()); 636 637 str.append(",Expire="); 638 str.append(getSecondsToExpire(System.currentTimeMillis())); 639 640 str.append(",Locks="); 641 str.append(numberOfLocks()); 642 643 str.append(",Ref="); 644 if ( hasNodeRef()) 645 str.append(getNodeRef().getId()); 646 else 647 str.append("Null"); 648 649 if ( isDirectory()) 650 { 651 str.append(",Pseudo="); 652 if ( hasPseudoFiles()) 653 str.append(getPseudoFileList().numberOfFiles()); 654 else 655 str.append(0); 656 } 657 str.append("]"); 658 659 return str.toString(); 660 } 661 } 662 | Popular Tags |