commands/drvinfo/drvinfo.cpp
changeset 0 7f656887cf89
child 81 72ffa331d78d
child 83 2a78c4ff2eab
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // drvinfo.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <f32file.h>
       
    14 #include <fshell/ioutils.h>
       
    15 
       
    16 using namespace IoUtils;
       
    17 
       
    18 class CCmdDrvinfo : public CCommandBase
       
    19 	{
       
    20 public:
       
    21 	static CCommandBase* NewLC();
       
    22 	~CCmdDrvinfo();
       
    23 private:
       
    24 	CCmdDrvinfo();
       
    25 	void ArgumentErrorL();
       
    26 	void PrintDriveInfoL(TInt aDriveNum);
       
    27 	void FormatVolInfoL(const TVolumeInfo& volInfo, IoUtils::CTextBuffer& aPrintBuf);
       
    28 private: // From CCommandBase.
       
    29 	virtual const TDesC& Name() const;
       
    30 	virtual void DoRunL();
       
    31 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    32 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    33 private:
       
    34 	HBufC* iDriveLetter;
       
    35 	TBool iVerbose;
       
    36 	TBool iHuman;
       
    37 	};
       
    38 
       
    39 
       
    40 CCommandBase* CCmdDrvinfo::NewLC()
       
    41 	{
       
    42 	CCmdDrvinfo* self = new(ELeave) CCmdDrvinfo();
       
    43 	CleanupStack::PushL(self);
       
    44 	self->BaseConstructL();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CCmdDrvinfo::~CCmdDrvinfo()
       
    49 	{
       
    50 	delete iDriveLetter;
       
    51 	}
       
    52 
       
    53 CCmdDrvinfo::CCmdDrvinfo()
       
    54 	{
       
    55 	}
       
    56 
       
    57 void CCmdDrvinfo::ArgumentErrorL()
       
    58 	{
       
    59 	Stderr().Write(_L("Invalid drive specification - use \'<drive_letter>:\', e.g. \'drvinfo c:\'\r\n"));
       
    60 	User::Leave(KErrArgument);
       
    61 	}
       
    62 
       
    63 void FormatLineEndL(IoUtils::CTextBuffer& aPrintBuf)
       
    64 	{
       
    65 	_LIT(KSeparator, " | ");
       
    66 	if (aPrintBuf.Descriptor().Right(KSeparator().Length()) == KSeparator)
       
    67 		{
       
    68 		aPrintBuf.Delete(aPrintBuf.Length() - KSeparator().Length(), KSeparator().Length());
       
    69 		}
       
    70 	if (aPrintBuf.Descriptor()[aPrintBuf.Length() - 1] == '(')
       
    71 		{
       
    72 		aPrintBuf.Delete(aPrintBuf.Length() - 2, 2);
       
    73 		}
       
    74 	else
       
    75 		{
       
    76 		aPrintBuf.AppendL(_L(")"));
       
    77 		}
       
    78 	aPrintBuf.AppendL(_L("\r\n"));
       
    79 	}
       
    80 
       
    81 void FormatDrvMediaTypeInfoL(const TDriveInfo& aDrvInfo, IoUtils::CTextBuffer& aPrintBuf)
       
    82 	{
       
    83 	aPrintBuf.AppendFormatL(_L("TMediaType:\t%d ("), aDrvInfo.iType);
       
    84 	switch(aDrvInfo.iType)
       
    85 		{
       
    86 		case EMediaNotPresent:  aPrintBuf.AppendL(_L("EMediaNotPresent"));   break;
       
    87 		case EMediaUnknown:	    aPrintBuf.AppendL(_L("EMediaUnknown"));      break;
       
    88 		case EMediaFloppy:      aPrintBuf.AppendL(_L("EMediaFloppy"));       break;
       
    89 		case EMediaHardDisk:    aPrintBuf.AppendL(_L("EMediaHardDisk"));     break;
       
    90 		case EMediaCdRom:		aPrintBuf.AppendL(_L("EMediaCdRom"));        break;
       
    91 		case EMediaRam:         aPrintBuf.AppendL(_L("EMediaRam"));          break;
       
    92 		case EMediaFlash:       aPrintBuf.AppendL(_L("EMediaFlash"));        break;
       
    93 		case EMediaRom:         aPrintBuf.AppendL(_L("EMediaRom"));          break;
       
    94 		case EMediaRemote:      aPrintBuf.AppendL(_L("EMediaRemote"));       break;
       
    95 		case EMediaNANDFlash:   aPrintBuf.AppendL(_L("EMediaNANDFlash"));    break;
       
    96 		default:                aPrintBuf.AppendL(_L("??? Unknown Type"));   break;
       
    97 		};
       
    98 	FormatLineEndL(aPrintBuf);
       
    99 	}
       
   100 
       
   101 void FormatDriveAttInfoL(const TDriveInfo& aDrvInfo, IoUtils::CTextBuffer& aPrintBuf)
       
   102 	{
       
   103 	aPrintBuf.AppendFormatL(_L("DriveAtt:\t0x%x ("), aDrvInfo.iDriveAtt);
       
   104 	if (aDrvInfo.iDriveAtt & KDriveAttLocal)         aPrintBuf.AppendL(_L("KDriveAttLocal | "));
       
   105 	if (aDrvInfo.iDriveAtt & KDriveAttRom)           aPrintBuf.AppendL(_L("KDriveAttRom | "));
       
   106 	if (aDrvInfo.iDriveAtt & KDriveAttRedirected)    aPrintBuf.AppendL(_L("KDriveAttRedirected | "));
       
   107 	if (aDrvInfo.iDriveAtt & KDriveAttSubsted)       aPrintBuf.AppendL(_L("KDriveAttSubsted | "));
       
   108 	if (aDrvInfo.iDriveAtt & KDriveAttInternal)      aPrintBuf.AppendL(_L("KDriveAttInternal | "));
       
   109 	if (aDrvInfo.iDriveAtt & KDriveAttRemovable)     aPrintBuf.AppendL(_L("KDriveAttRemovable | "));
       
   110 	if (aDrvInfo.iDriveAtt & KDriveAttRemote)        aPrintBuf.AppendL(_L("KDriveAttRemote | "));
       
   111 	if (aDrvInfo.iDriveAtt & KDriveAttTransaction)   aPrintBuf.AppendL(_L("KDriveAttTransaction | "));
       
   112 	FormatLineEndL(aPrintBuf);
       
   113 	}
       
   114 
       
   115 void FormatMediaAttInfoL(const TDriveInfo& aDrvInfo, IoUtils::CTextBuffer& aPrintBuf)
       
   116 	{
       
   117 	aPrintBuf.AppendFormatL(_L("MediaAtt:\t0x%x ("),aDrvInfo.iMediaAtt);
       
   118 	if (aDrvInfo.iMediaAtt & KMediaAttVariableSize)      aPrintBuf.AppendL(_L("KMediaAttVariableSize | "));
       
   119 	if (aDrvInfo.iMediaAtt & KMediaAttDualDensity)       aPrintBuf.AppendL(_L("KMediaAttDualDensity | "));
       
   120 	if (aDrvInfo.iMediaAtt & KMediaAttFormattable)       aPrintBuf.AppendL(_L("KMediaAttFormattable | "));
       
   121 	if (aDrvInfo.iMediaAtt & KMediaAttWriteProtected)    aPrintBuf.AppendL(_L("KMediaAttWriteProtected | "));
       
   122 	if (aDrvInfo.iMediaAtt & KMediaAttLockable)          aPrintBuf.AppendL(_L("KMediaAttLockable | "));
       
   123 	if (aDrvInfo.iMediaAtt & KMediaAttLocked)            aPrintBuf.AppendL(_L("KMediaAttLocked | "));
       
   124 	if (aDrvInfo.iMediaAtt & KMediaAttHasPassword)       aPrintBuf.AppendL(_L("KMediaAttHasPassword | "));
       
   125 	if (aDrvInfo.iMediaAtt & KMediaAttReadWhileWrite)    aPrintBuf.AppendL(_L("KMediaAttReadWhileWrite | "));
       
   126 	if (aDrvInfo.iMediaAtt & KMediaAttDeleteNotify)      aPrintBuf.AppendL(_L("KMediaAttDeleteNotify | "));
       
   127 	FormatLineEndL(aPrintBuf);
       
   128 	}
       
   129 
       
   130 void CCmdDrvinfo::FormatVolInfoL(const TVolumeInfo& volInfo, IoUtils::CTextBuffer& aPrintBuf)
       
   131 	{
       
   132 	if (iVerbose)
       
   133 		{
       
   134 		aPrintBuf.AppendFormatL(_L("VolId:\t0x%x\r\n"), volInfo.iUniqueID);
       
   135 		}
       
   136 	aPrintBuf.AppendL(_L("VolSize:\t"));
       
   137 	if (iHuman)
       
   138 		{
       
   139 		aPrintBuf.AppendHumanReadableSizeL(volInfo.iSize);
       
   140 		aPrintBuf.AppendL(_L("\r\n"));
       
   141 		}
       
   142 	else
       
   143 		{
       
   144 		aPrintBuf.AppendFormatL(_L("%ld\r\n"), volInfo.iSize);
       
   145 		}
       
   146 	aPrintBuf.AppendL(_L("Free:\t"));
       
   147 	if (iHuman)
       
   148 		{
       
   149 		aPrintBuf.AppendHumanReadableSizeL(volInfo.iFree);
       
   150 		aPrintBuf.AppendL(_L("\r\n"));
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		aPrintBuf.AppendFormatL(_L("%ld\r\n"), volInfo.iFree);
       
   155 		}
       
   156 	if (iVerbose)
       
   157 		{
       
   158 		aPrintBuf.AppendFormatL(_L("VolName:\t\'%S\'"), &volInfo.iName);
       
   159 		}
       
   160 	aPrintBuf.AppendL(_L("\r\n"));
       
   161 	}
       
   162 
       
   163 void CCmdDrvinfo::PrintDriveInfoL(TInt aDriveNum)
       
   164 	{
       
   165 	IoUtils::CTextBuffer* buf = IoUtils::CTextBuffer::NewLC(0x100);
       
   166 
       
   167 	TDriveInfo 	driveInfo;
       
   168 	User::LeaveIfError(FsL().Drive(driveInfo, aDriveNum));
       
   169 
       
   170 	TVolumeInfo volInfo;
       
   171 	User::LeaveIfError(Fs().Volume(volInfo, aDriveNum));
       
   172 
       
   173 	if (iVerbose || (iDriveLetter == NULL))
       
   174 		{
       
   175 		buf->AppendFormatL(_L("Drive:\t%c:\r\n"), 'A'+ aDriveNum);
       
   176 		}
       
   177 
       
   178 	if (iVerbose)
       
   179 		{
       
   180 		buf->AppendFormatL(_L("Number:\t%d\r\n"), aDriveNum);
       
   181 
       
   182 		TBuf<256> tbuf;
       
   183 		if (Fs().FileSystemName(tbuf, aDriveNum) == KErrNone)
       
   184 			{
       
   185 			buf->AppendFormatL(_L("FS name:\t\'%S\'\r\n"), &tbuf);
       
   186 			}
       
   187 
       
   188 		FormatDrvMediaTypeInfoL(driveInfo, *buf);
       
   189 		buf->AppendFormatL(_L("BatteryState:\t%d\r\n"),driveInfo.iBattery);
       
   190 		FormatDriveAttInfoL(driveInfo, *buf);
       
   191 		FormatMediaAttInfoL(driveInfo, *buf);
       
   192 		}
       
   193 
       
   194 	FormatVolInfoL(volInfo, *buf);
       
   195 
       
   196 	CTextFormatter* formatter = CTextFormatter::NewLC(Stdout());
       
   197 	formatter->TabulateL(0, 2, buf->Descriptor());
       
   198 	Write(formatter->Descriptor());
       
   199 
       
   200 	CleanupStack::PopAndDestroy(2, buf);
       
   201 	}
       
   202 
       
   203 const TDesC& CCmdDrvinfo::Name() const
       
   204 	{
       
   205 	_LIT(KName, "drvinfo");	
       
   206 	return KName;
       
   207 	}
       
   208 
       
   209 void CCmdDrvinfo::DoRunL()
       
   210 	{
       
   211 	TInt driveNum = -1;
       
   212 
       
   213 	if (iDriveLetter)
       
   214 		{
       
   215 		const TInt len = iDriveLetter->Length();
       
   216 
       
   217 		if ((len < 2) || (len > 3) || ((*iDriveLetter)[1] != ':'))
       
   218 			{
       
   219 			ArgumentErrorL();
       
   220 			}
       
   221 
       
   222 		if ((len == 3) && ((*iDriveLetter)[2] != '\\'))
       
   223 			{
       
   224 			ArgumentErrorL();
       
   225 			}
       
   226 
       
   227 		driveNum = TChar((*iDriveLetter)[0]).GetUpperCase() - 'A';
       
   228 
       
   229 		if (driveNum > EDriveZ)
       
   230 			{
       
   231 			ArgumentErrorL();
       
   232 			}
       
   233 		}
       
   234 
       
   235 
       
   236 	TDriveList driveList;
       
   237 	User::LeaveIfError(FsL().DriveList(driveList));
       
   238 
       
   239 	if (driveNum >= 0)
       
   240 		{
       
   241 		if (!driveList[driveNum])
       
   242 			{
       
   243 			ArgumentErrorL();
       
   244 			}
       
   245 
       
   246 		PrintDriveInfoL(driveNum);
       
   247 		}
       
   248 	else
       
   249 		{
       
   250 		for (TInt i = 0; i < KMaxDrives; ++i)
       
   251 			{
       
   252 			if (driveList[i])
       
   253 				{
       
   254 				TRAPD(err, PrintDriveInfoL(i));
       
   255 				if (err)
       
   256 					{
       
   257 					PrintWarning(_L("Couldn't read drive info for %c"), 'A'+ i);
       
   258 					}
       
   259 				Printf(_L("\r\n"));
       
   260 				}
       
   261 			}
       
   262 		}
       
   263 	}
       
   264 
       
   265 void CCmdDrvinfo::ArgumentsL(RCommandArgumentList& aArguments)
       
   266 	{
       
   267 	_LIT(KArg, "drive_letter");
       
   268 	aArguments.AppendStringL(iDriveLetter, KArg);
       
   269 	}
       
   270 
       
   271 void CCmdDrvinfo::OptionsL(RCommandOptionList& aOptions)
       
   272 	{
       
   273 	_LIT(KOptVerbose, "verbose");
       
   274 	_LIT(KOptHuman, "human");
       
   275 
       
   276 	aOptions.AppendBoolL(iVerbose, KOptVerbose);
       
   277 	aOptions.AppendBoolL(iHuman, KOptHuman);
       
   278 	}
       
   279 
       
   280 
       
   281 EXE_BOILER_PLATE(CCmdDrvinfo)
       
   282