public bool UnlinkDirectory(short number)
{
const string module = "UnlinkDirectory";
if (m_mode == O_RDONLY)
{
ErrorExt(this, m_clientdata, module, "Can not unlink directory in read-only file");
return false;
}
// Go to the directory before the one we want
// to unlink and nab the offset of the link
// field we'll need to patch.
uint nextdir = m_header.tiff_diroff;
long off = sizeof(short) + sizeof(short);
for (int n = number - 1; n > 0; n--)
{
if (nextdir == 0)
{
ErrorExt(this, m_clientdata, module,
"Directory {0} does not exist", number);
return false;
}
if (!advanceDirectory(ref nextdir, out off))
return false;
}
// Advance to the directory to be unlinked and fetch the offset of the directory
// that follows.
long dummyOff;
if (!advanceDirectory(ref nextdir, out dummyOff))
return false;
// Go back and patch the link field of the preceding directory to point to the
// offset of the directory that follows.
seekFile(off, SeekOrigin.Begin);
if ((m_flags & TiffFlags.SWAB) == TiffFlags.SWAB)
SwabUInt(ref nextdir);
if (!writeIntOK((int)nextdir))
{
ErrorExt(this, m_clientdata, module, "Error writing directory link");
return false;
}
// Leave directory state setup safely. We don't have facilities for doing inserting
// and removing directories, so it's safest to just invalidate everything. This means
// that the caller can only append to the directory chain.
m_currentCodec.Cleanup();
if ((m_flags & TiffFlags.MYBUFFER) == TiffFlags.MYBUFFER && m_rawdata != null)
{
m_rawdata = null;
m_rawcc = 0;
}
m_flags &= ~(TiffFlags.BEENWRITING | TiffFlags.BUFFERSETUP | TiffFlags.POSTENCODE);
FreeDirectory();
setupDefaultDirectory();
m_diroff = 0; // force link on next write
m_nextdiroff = 0; // next write must be at end
m_curoff = 0;
m_row = -1;
m_curstrip = -1;
return true;
}