Update contrib.
1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32\sfsrv\cl_find.cpp
20 #define gPathDelimiter TChar(';')
21 enum TMode {EFindByDrives,EFindByDrivesInPath,EFindByPath};
23 TInt TFindFile::DoFindInDir()
25 // Look for aFileName in aDir
32 TInt r=iFs->Entry(iFile.FullName(),entry);
33 if (r==KErrNone /*|| r==KErrAccessDenied*/)
35 else if (r==KErrNoMemory)
37 else if (r==KErrPermissionDenied)
38 return (KErrPermissionDenied);
42 TInt r=iFs->GetDir(iFile.FullName(),KEntryAttMaskSupported|KEntryAttAllowUid,ESortByName,*iDir);
45 else if (r==KErrPermissionDenied)
49 if ((*iDir)->Count()==0)
59 TInt TFindFile::DoFindNextInPath()
61 // Look for aFileName along the path and increment aPathPos
65 if (iMode==EFindByDrivesInPath)
67 TInt r=DoFindNextInDriveList();
76 if (iPath->Length()<iPathPos)
78 TPtrC path(iPath->Ptr()+iPathPos,iPath->Length()-iPathPos);
79 TInt r=path.Locate(gPathDelimiter);
82 path.Set(path.Ptr(),r);
84 TFileName fileName=iFile.NameAndExt();
85 iFile.Set(fileName,&path,NULL);
86 if (iFile.FullName().Length()>=2 && iFile.FullName()[1]==KDriveDelimiter)
95 iMode=EFindByDrivesInPath;
97 r=FindByDir(fileName,path);
106 TInt TFindFile::DoFindNextInDriveList()
108 // Look for aFileName in all available drives in order
113 TDriveInfo driveInfo;
114 const TUint matchedFlags= iMatchMask & KDriveAttMatchedFlags; //KDriveAttMatchedFlags = 0xFFF
115 const TUint matchedAtt = iMatchMask & KDriveAttMatchedAtt; //KDriveAttMatchedAtt = 0x0FFF0000
117 if (iMatchMask == (KDriveAttExclude | KDriveAttMatchedFlags ) ) //If all drives are excluded.
124 TInt currentDrive=iCurrentDrive;
127 if (iCurrentDrive==-1)
128 currentDrive=EDriveZ;
131 if (currentDrive<=-2)
132 return(KErrNotFound);
137 if (!iDrvList[currentDrive])
140 TInt err = iFs->Drive(driveInfo,currentDrive);
144 if (iDrvList[currentDrive] & KDriveAttRemote)
145 continue; // NOT allowed on REMOTE DRIVE
146 if ((iDrvList[currentDrive] & (KDriveAttLocal|KDriveAttRom))==0)
152 if(matchedFlags != 0 )
156 { //found ==0 means that this drive attributes didn't match the flags
158 case KDriveAttExclude :
159 found = (driveInfo.iDriveAtt & matchedFlags ) ? 0:iDrvList[currentDrive] ;
163 found = (driveInfo.iDriveAtt & matchedFlags) ? iDrvList[currentDrive]:0 ;
167 case KDriveAttExclusive :
168 if(matchedFlags != KDriveAttLogicallyRemovable)
171 found = ((TUint8)(driveInfo.iDriveAtt) == matchedFlags) ?iDrvList[currentDrive]:0;
176 found = (driveInfo.iDriveAtt == (matchedFlags | KDriveAttRemovable) ) ?iDrvList[currentDrive]:0;
181 case KDriveAttExclude | KDriveAttExclusive:
182 if(matchedFlags != KDriveAttLogicallyRemovable)
185 found = ((TUint8)(driveInfo.iDriveAtt) == matchedFlags) ?0:iDrvList[currentDrive];
190 found = (driveInfo.iDriveAtt == (matchedFlags | KDriveAttRemovable)) ?0:iDrvList[currentDrive];
196 Panic(EFindFileIllegalAttribute);
201 else //matchedFlags == 0.
204 if (matchedAtt== KDriveAttAll)
205 found= iDrvList[currentDrive];
207 Panic(EFindFileIllegalAttribute);
218 // Don't scan a locked drive
222 if(driveInfo.iMediaAtt & KMediaAttLocked)
229 RFs::DriveToChar(currentDrive,driveLetter);
232 drive[0]=(TText)driveLetter;
234 TPtrC nameAndExt(iFile.NameAndExt());
235 TPtrC path(iFile.Path());
236 fileName.Set(nameAndExt,&path,&drive);
238 TInt r=DoFindInDir();
249 EXPORT_C TFindFile::TFindFile(RFs& aFs)
250 : iFs(&aFs), iPathPos(0), iCurrentDrive(0), iMode(-1), iMatchMask(0)
252 Constructor taking a file server session.
254 @param aFs File server session.
258 iFile.Set(_L(""),NULL,NULL);
264 TInt TFindFile::DoFindByPath(const TDesC& aFileName,const TDesC* aPath)
266 // Look for a file in each directory in the path
267 // Make initial check for aFileName in the current directory
271 if (aFileName.Length() <= 0)
272 return(KErrArgument);
274 TInt r=iFile.Set(aFileName,NULL,NULL);
287 if ((iPath==NULL) || (iPath->Length()==0))
288 return(KErrNotFound);
291 r=DoFindNextInPath();
295 TInt TFindFile::DoFindByDir(const TDesC& aFileName,const TDesC& aDir)
297 // Look for aFileName in aDir on each connected drive
298 // Make initial check for aFileName in aDir on current drive
302 if (aFileName.Length() <= 0)
303 return(KErrArgument);
305 TInt r=iFs->Parse(aFileName,aDir,iFile);
308 TInt searchResult=DoFindInDir();
309 if(searchResult==KErrNoMemory)
310 return(searchResult);
312 if(searchResult==KErrPermissionDenied)
313 return (KErrPermissionDenied);
315 r=iFs->DriveList(iDrvList,KDriveAttAll);
319 r=RFs::CharToDrive(iFile.Drive()[0],drive);
322 iDrvList[drive]=0; // Drive 'drive' has already been searched
323 iCurrentDrive=EDriveY;
325 if (searchResult==KErrNone)
329 return(DoFindNextInDriveList());
335 EXPORT_C TInt TFindFile::FindByPath(const TDesC& aFileName,const TDesC* aPath)
337 Searches for a file/directory in one or more directories in the path.
339 The search ends when the file/directory is found, or when every directory
340 specified in the path list has been unsuccessfully searched.
344 1. For the equivalent search using wildcards, use FindWildByPath().
346 2. After a file has been found, use TFindFile::File() to get the fully qualified path of the file. To
347 search for the next occurrence, use TFindFile::Find().
349 @param aFileName The filename to search for. If this specifies a directory as well
350 as a filename, then that directory is searched first.
351 @param aPath A list of directories to be searched. Paths in this list must
352 be separated by a semicolon character, but a semicolon is not
353 required after the final path. The directories are searched in
354 the order in which they occur in the list. If a path in
355 the list contains a drive letter, that drive alone is searched.
356 If a path contains no drive letter, the function searches for
357 the file in that directory on every available drive in turn,
358 beginning with drive Y:, in descending alphabetical order
359 and ending with drive Z:.When path is empty then session path
360 will be used for the search.
362 @return KErrNone, if the filename was found;
363 KErrNotFound, if the filename was not found.
364 KErrArgument, if the filename is empty.
366 @see TFindFile::FindWildByPath
373 return(DoFindByPath(aFileName,aPath));
379 EXPORT_C TInt TFindFile::FindByDir(const TDesC& aFileName,const TDesC& aDir)
381 Searches for a file/directory in a directory on all available drives.
383 The search ends when the file/directory is found, or when every available
384 drive has been unsuccessfully searched.
388 1. A drive letter may be specified in aDirPath.
389 If a drive is specified, then that drive is searched first. If no drive is
390 specified, then the drive specified in the session path is searched first.
391 The remaining available drives are then searched in descending alphabetical
392 order, from Y: to A:, and ending with the Z: drive. Using function SetFindMask
393 it is possible to specify a combination of attributes that the drives to be
397 2. For the corresponding search using wildcards, use FindWildByDir().
399 3. After a file has been found, use TFindFile::File() to get the fully
400 qualified path and filename. To search for the next occurrence,
401 use TFindFile::Find().
403 @param aFileName The filename to search for. If a path is specified, it
404 overrides the path specified in aDir. If no path is specified,
405 the path contained in aDir is used.
406 @param aDir A single path indicating a directory to be searched on each
407 drive.When path is empty then session path will be used for the search.
409 @return KErrNone, if the file was found, otherwise one of the system-wide error codes, including:
410 KErrNotFound, if the file was not found;
411 KErrPermissionDenied, if the client does not have appropriate capabilities for the directory to be searched;
412 KErrArgument, if the filename is empty.
414 @see TFindFile::FindWildByDir()
415 @see TFindFile::File()
416 @see TFindFile::Find()
417 @see TFindFile::SetFindMask()
422 return(DoFindByDir(aFileName,aDir));
428 EXPORT_C TInt TFindFile::FindWildByPath(const TDesC& aFileName,const TDesC* aPath,CDir*& aDir)
430 Searches for one or more files/directories in the directories contained in a
433 Wildcard characters can be specified. The search ends when one or more
434 filenames matching aFileName is found, or when every
435 directory in the path list has been unsuccessfully searched.
436 To begin searching again after a successful match has been made,
439 Using function SetFindMask it is possible to specify a combination of
440 attributes that the drives to be searched must match.
444 1. The caller of the function is responsible for deleting
445 aDir after the function has returned.
447 2. Calling TFindFile::File() after a successful search gets the drive letter
448 and directory containing the file(s). The filenames can be retrieved via
449 the array of TEntry::iName objects contained in aDir. If you want to
450 retrieve the fully qualified path of a file, you need to parse the path and
453 @param aFileName The filename to search for. May contain wildcards. If
454 it specifies a directory as well as a filename, then that
455 directory is searched first.
456 @param aPath List of directories to search. Paths in this list must be
457 separated by a semicolon character, but a semicolon is not
458 required after the final path. The directories are searched
459 in the order in which they occur in the list.
460 Directories must be fully qualified, including
461 a drive letter, and the name must end with a backslash.
462 @param aDir On return, contains the entries for all files matching
463 aFileName in the first directory in which a match occurred.
465 @return KErrNone, if one or more matching files was found;
466 KErrNotFound, if no matching file was found in any of the directories.
467 KErrArgument, if the filename is empty.
469 @see TFindFile::FindWild
472 @see TFindFile::SetFindMask()
477 return(DoFindByPath(aFileName,aPath));
483 EXPORT_C TInt TFindFile::FindWildByDir(const TDesC& aFileName,const TDesC& aDirPath,CDir*& aDir)
485 Searches, using wildcards, for one or more files/directories in a specified
488 If no matching file is found in that directory, all available drives are
489 searched in descending alphabetical order, from Y: to A:, and ending
490 with the Z: drive.Using function SetFindMask it is possible to specify a
491 combination of attributes that the drives to be searched must match.
493 The search ends when one or more matching filenames are found, or when every
494 available drive has been unsuccessfully searched. To begin searching again
495 after a successful match has been made, use FindWild(). Wildcards may be
496 specified in the filename.
500 1. A drive letter may be specified in aDirPath (or in aFileName). If a drive
501 is specified, that drive is searched first, followed by the other available
502 drives, in descending alphabetical order. If no drive is specified, the drive
503 contained in the session path is searched first.
505 2. The function sets aDir to NULL, then allocates memory for it before appending
506 entries to the list. Therefore, aDir should have no memory allocated to it
507 before this function is called, otherwise this memory will become orphaned.
509 3. The caller of this function is responsible for deleting aDir after the function
512 4. Calling TFindFile::File() after a successful search returns the drive letter
513 and directory containing the file(s). Filenames may be retrieved via the array
514 of TEntry::iNames contained in aDir. If you want to retrieve the fully
515 qualified path of a file, you will need to parse the path and the filename.
517 @param aFileName The filename to search for. May contain wildcards. If a path
518 is specified, it overrides the path specified in aDirPath.
519 If no path is specified, the path contained in aDirPath is
521 @param aDirPath Path indicating a directory to search on each drive.
522 @param aDir On return, contains the entries for all files
525 @return KErrNone if one or more matching files was found;
526 KErrNotFound if no matching file was found in the directory on any
528 KErrArgument, if the filename is empty.
530 @see TFindFile::FindWild
532 @see TFindFile::SetFindMask()
537 return(DoFindByDir(aFileName,aDirPath));
543 TInt TFindFile::DoFind()
545 // Find the next match
553 ret=DoFindNextInDriveList();
556 case EFindByDrivesInPath:
557 ret=DoFindNextInPath();
560 Panic(EFindFileIllegalMode);
568 EXPORT_C TInt TFindFile::Find()
570 Searches for the next file/directory.
572 This should be used after a successful call to FindByPath() or FindByDir(),
573 to find the next occurrence of the filename in the path or drive list.
575 Using function SetFindMask it is possible to specify a combination of
576 attributes that the drives to be searched must match.
580 1. After a file/directory has been found, use TFindFile::File() to get the
581 fully qualified path and filename.
583 @return KErrNone, if another occurrence of the file was found;
584 KErrNotFound, if no more occurrences were found.
586 @see TFindFile::FindByPath
587 @see TFindFile::FindByDir
589 @see TFindFile::SetFindMask()
601 EXPORT_C TInt TFindFile::FindWild(CDir*& aDir)
603 Searches for the next file/directory.
605 This should be used after a successful call to FindWildByPath()
606 or FindWildByDir(), for the next occurrences of the filename in the
607 path or drive list.Using function SetFindMask it is possible to specify a
608 combination of attributes that the drives to be searched must match.
612 1. The caller of this function is responsible for deleting aDir after
613 the function has returned
615 2. Calling TFindFile::File() after a successful search, will return
616 the drive letter and the directory containing the file(s).
617 The filenames may be retrieved via the array of TEntry::iName objects
618 contained in aDir. If you want to retrieve the fully qualified
619 path of a file, you will need to parse the path and the filename using
620 the TParse class or derived classes.
622 @param aDir On return, contains the entries for all matching files found in
625 @return KErrNone, if further occurrences were found;
626 KErrNotFound, if no more matching files were found.
632 @see TFindFile::FindWildByPath
633 @see TFindFile::FindWildByDir
634 @see TFindFile::SetFindMask()
644 EXPORT_C TInt TFindFile::SetFindMask(TUint aMask)
646 Can be used in order to specify a combination of drive attributes that the drives
647 to be searched must match. When searching without specifying a mask, all drives, except the
648 remote ones will be returned.
650 @param aMask The combination of drive attributes that we want the drives to match.
652 @return KErrNone, if the mask supplied is correct.
653 KErrArgument, if the mask supplied is invalid.
657 TInt r =ValidateMatchMask(aMask);
665 iMatchMask = (KDriveAttExclude |KDriveAttMatchedFlags) ; //KDriveAttMatchedFlags ==0xFF so this exclude all drives in DoFindNextInDriveList