/*
* px *
Copyright (c) 2008 Shawn Hladky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pxheaders.h"
#include "md5.h"
ErrorId md5_usage = { ErrorOf( ES_CLIENT, 0, E_INFO, EV_USAGE, 0 ),
"\n"
" md5 -- Display the md5 digest of local files.\n"
"\n"
" px md5 [-t [binary|text]] files...\n"
"\n"
" 'px md5' displays the md5 digest for local files.\n"
" md5 uses Perforce logic in that line endings are normalized\n"
" before computing the digest. md5 uses Perforce's file type\n"
" detection, although typemap is ignored for local files.\n"
"\n"
" The -t flag forces binary or text filetype when computing\n"
" the digest.\n"
"\n"};
void md5Command::Help(Error *e)
{
e->Set(md5_usage);
}
void md5Command::Run(ClientUser* cu, ClientApi* api, char *const *argv, int argc)
{
Options opts;
Error e;
int iargc = argc;
char** iargv = (char**) argv;
ErrorId usage = {E_INFO , "Usage: parse optionstring flag args" };
opts.Parse( iargc, iargv, "t:", OPT_SOME, usage, &e);
if (e.Test())
{
cu->Message(&e);
return;
}
ErrorId digestInfo = {ErrorOf( ES_CLIENT, 0, E_INFO, EV_CLIENT, 2) , "%localFile% - %digest%" };
ErrorId fileNotFoundErr = {ErrorOf( ES_CLIENT, 0, E_FAILED, EV_CLIENT, 1) , "File not found: %localFile%!" };
if(iargc == 0)
{
// shouldn't opts error if this is the case?
}
else
{
for (int i = 0; i < iargc; i++)
{
StrBuf digest;
// first try to open the file as binary
FileSysType type = FST_BINARY;
StrRef arg(iargv[i]);
FileSys* file = FileSys::Create(type);
file->Set(iargv[i]);
if (! (file->Stat() & FSF_EXISTS) )
{
file->Close(&e);
e.Clear();
e.Set(fileNotFoundErr);
e << iargv[i];
}
// if the file is not binary, re-open as the correct type
else
{
if (!((type = file->CheckType()) & FST_BINARY))
{
file->Close(&e);
file = FileSys::Create(type);
file->Set(iargv[i]);
}
file->Digest(&digest, &e);
file->Close(&e);
if (!e.Test())
{
e.Clear();
e.Set(digestInfo);
e << file->Path();
e << digest;
}
}
cu->Message(&e);
}
}
}