#include "diskrep.h"
#include <sys/stat.h>
#include <CoreFoundation/CFBundlePriv.h>
#include "filediskrep.h"
#include "bundlediskrep.h"
#include "cfmdiskrep.h"
#include "slcrep.h"
namespace Security {
namespace CodeSigning {
using namespace UnixPlusPlus;
DiskRep::DiskRep()
{
}
DiskRep::~DiskRep()
{
CODESIGN_DISKREP_DESTROY(this);
}
DiskRep *DiskRep::base()
{
return this;
}
DiskRep::Writer *DiskRep::writer()
{
MacOSError::throwMe(errSecCSBadObjectFormat);
}
void DiskRep::Writer::addDiscretionary(CodeDirectory::Builder &)
{
}
DiskRep *DiskRep::bestGuess(const char *path, const Context *ctx)
{
try {
if (!(ctx && ctx->fileOnly)) {
struct stat st;
if (::stat(path, &st))
UnixError::throwMe();
if ((st.st_mode & S_IFMT) == S_IFDIR) return new BundleDiskRep(path, ctx);
if (CFRef<CFURLRef> pathURL = makeCFURL(path))
if (CFRef<CFBundleRef> bundle = _CFBundleCreateWithExecutableURLIfMightBeBundle(NULL, pathURL))
return new BundleDiskRep(bundle, ctx);
}
AutoFileDesc fd(path, O_RDONLY);
if (MachORep::candidate(fd))
return new MachORep(path, ctx);
if (CFMDiskRep::candidate(fd))
return new CFMDiskRep(path);
if (DYLDCacheRep::candidate(fd))
return new DYLDCacheRep(path);
return new FileDiskRep(path);
} catch (const CommonError &error) {
switch (error.unixError()) {
case ENOENT:
MacOSError::throwMe(errSecCSStaticCodeNotFound);
default:
throw;
}
}
}
DiskRep *DiskRep::bestFileGuess(const char *path, const Context *ctx)
{
Context dctx;
if (ctx)
dctx = *ctx;
dctx.fileOnly = true;
return bestGuess(path, &dctx);
}
DiskRep *DiskRep::bestGuess(const char *path, size_t archOffset)
{
try {
if (CFRef<CFURLRef> pathURL = makeCFURL(path))
if (CFRef<CFBundleRef> bundle = _CFBundleCreateWithExecutableURLIfMightBeBundle(NULL, pathURL)) {
Context ctx; ctx.offset = archOffset;
return new BundleDiskRep(bundle, &ctx); }
Context ctx; ctx.offset = archOffset;
return new MachORep(path, &ctx);
} catch (const CommonError &error) {
switch (error.unixError()) {
case ENOENT:
MacOSError::throwMe(errSecCSStaticCodeNotFound);
default:
throw;
}
}
}
string DiskRep::resourcesRootPath()
{
return ""; }
CFDictionaryRef DiskRep::defaultResourceRules()
{
return NULL; }
void DiskRep::adjustResources(ResourceBuilder &builder)
{
}
const Requirements *DiskRep::defaultRequirements(const Architecture *)
{
return NULL; }
Universal *DiskRep::mainExecutableImage()
{
return NULL; }
size_t DiskRep::pageSize()
{
return monolithicPageSize; }
size_t DiskRep::signingBase()
{
return 0; }
CFArrayRef DiskRep::modifiedFiles()
{
CFRef<CFURLRef> mainURL = makeCFURL(mainExecutablePath());
return makeCFArray(1, mainURL.get());
}
void DiskRep::flush()
{
}
DiskRep::Writer::Writer(uint32_t attrs)
: mArch(CPU_TYPE_ANY), mAttributes(attrs)
{
}
DiskRep::Writer::~Writer()
{ }
uint32_t DiskRep::Writer::attributes() const
{ return mAttributes; }
void DiskRep::Writer::flush()
{ }
void DiskRep::Writer::remove()
{
MacOSError::throwMe(errSecCSNotSupported);
}
} }