Skip to content

Commit

Permalink
Remove link and unlink macros on Windows.
Browse files Browse the repository at this point in the history
They provided source-code compatibility with the POSIX versions of these
functions, but they conflict with Boost. Replace with EMU_LINK and
EMU_UNLINK everywhere.
  • Loading branch information
qris committed Jul 23, 2017
1 parent c5e9e11 commit 04a7a8e
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 85 deletions.
6 changes: 3 additions & 3 deletions lib/backupclient/BackupClientRestore.cpp
Expand Up @@ -277,7 +277,7 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection,
"out of the way of restored directory. "
"Use specific restore with ID to "
"restore this object.");
if(::unlink(rLocalDirectoryName.c_str()) != 0)
if(EMU_UNLINK(rLocalDirectoryName.c_str()) != 0)
{
BOX_LOG_SYS_ERROR("Failed to delete "
"file '" <<
Expand Down Expand Up @@ -513,7 +513,7 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection,
// files already there.
if(ObjectExists(localFilename)
!= ObjectExists_NoObject &&
::unlink(localFilename.c_str()) != 0)
EMU_UNLINK(localFilename.c_str()) != 0)
{
BOX_LOG_SYS_ERROR("Failed to delete "
"file '" << localFilename <<
Expand Down Expand Up @@ -912,7 +912,7 @@ int BackupClientRestore(BackupProtocolCallable &rConnection,
}

// Delete the resume information file
::unlink(params.mRestoreResumeInfoFilename.c_str());
EMU_UNLINK(params.mRestoreResumeInfoFilename.c_str());

return params.ContinuedAfterError ? Restore_CompleteWithErrors
: Restore_Complete;
Expand Down
2 changes: 1 addition & 1 deletion lib/backupstore/BackupClientFileAttributes.cpp
Expand Up @@ -814,7 +814,7 @@ void BackupClientFileAttributes::WriteAttributes(const std::string& Filename,
Filename << "'");
#else
// Make a symlink, first deleting anything in the way
::unlink(Filename.c_str());
EMU_UNLINK(Filename.c_str());
if(::symlink((char*)(pattr + 1), Filename.c_str()) != 0)
{
BOX_LOG_SYS_ERROR("Failed to symlink '" << Filename <<
Expand Down
2 changes: 1 addition & 1 deletion lib/backupstore/BackupStoreFile.cpp
Expand Up @@ -671,7 +671,7 @@ void BackupStoreFile::DecodeFile(IOStream &rEncodedFile, const char *DecodedFile
}
catch(...)
{
::unlink(DecodedFilename);
EMU_UNLINK(DecodedFilename);
throw;
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/backupstore/BackupStoreRefCountDatabase.cpp
Expand Up @@ -68,7 +68,7 @@ void BackupStoreRefCountDatabase::Commit()
std::string Final_Filename = GetFilename(mAccount, false);

#ifdef WIN32
if(FileExists(Final_Filename) && unlink(Final_Filename.c_str()) != 0)
if(FileExists(Final_Filename) && EMU_UNLINK(Final_Filename.c_str()) != 0)
{
THROW_EMU_FILE_ERROR("Failed to delete old permanent refcount "
"database file", mFilename, CommonException,
Expand Down Expand Up @@ -106,7 +106,7 @@ void BackupStoreRefCountDatabase::Discard()
mapDatabaseFile.reset();
}

if(unlink(mFilename.c_str()) != 0)
if(EMU_UNLINK(mFilename.c_str()) != 0)
{
THROW_EMU_FILE_ERROR("Failed to delete temporary refcount "
"database file", mFilename, CommonException,
Expand Down Expand Up @@ -187,7 +187,7 @@ std::auto_ptr<BackupStoreRefCountDatabase>
{
BOX_WARNING(BOX_FILE_MESSAGE(Filename, "Overwriting existing "
"temporary reference count database"));
if (unlink(Filename.c_str()) != 0)
if (EMU_UNLINK(Filename.c_str()) != 0)
{
THROW_SYS_FILE_ERROR("Failed to delete old temporary "
"reference count database file", Filename,
Expand Down
10 changes: 5 additions & 5 deletions lib/bbackupd/BackupDaemon.cpp
Expand Up @@ -531,7 +531,7 @@ void BackupDaemon::Run()
mapCommandSocketInfo->mListeningSocket.Listen(
socketName);
#else
::unlink(socketName);
EMU_UNLINK(socketName);
mapCommandSocketInfo->mListeningSocket.Listen(
Socket::TypeUNIX, socketName);
#endif
Expand Down Expand Up @@ -2830,7 +2830,7 @@ void BackupDaemon::FillIDMapVector(std::vector<BackupClientInodeToIDMap *> &rVec
BOX_NOTICE("Found an incomplete ID map "
"database, deleting it to start "
"afresh: " << filename);
if(unlink(filename.c_str()) != 0)
if(EMU_UNLINK(filename.c_str()) != 0)
{
BOX_LOG_NATIVE_ERROR(BOX_FILE_MESSAGE(
filename, "Failed to delete "
Expand Down Expand Up @@ -2879,14 +2879,14 @@ void BackupDaemon::DeleteCorruptBerkelyDbFiles()

// Delete the file
BOX_TRACE("Deleting " << filename);
::unlink(filename.c_str());
EMU_UNLINK(filename.c_str());

// Add a suffix for the new map
filename += ".n";

// Delete that too
BOX_TRACE("Deleting " << filename);
::unlink(filename.c_str());
EMU_UNLINK(filename.c_str());
}
}

Expand Down Expand Up @@ -3637,7 +3637,7 @@ bool BackupDaemon::DeleteStoreObjectInfo() const
}

// Actually delete it
if(::unlink(storeObjectInfoFile.c_str()) != 0)
if(EMU_UNLINK(storeObjectInfoFile.c_str()) != 0)
{
BOX_LOG_SYS_ERROR("Failed to delete the old "
"StoreObjectInfoFile: " << storeObjectInfoFile);
Expand Down
12 changes: 6 additions & 6 deletions lib/bbackupquery/BackupQueries.cpp
Expand Up @@ -1003,17 +1003,17 @@ void BackupQueries::CommandGetObject(const std::vector<std::string> &args, const
{
BOX_ERROR("Object ID " << BOX_FORMAT_OBJECTID(id) <<
" does not exist on store.");
::unlink(args[1].c_str());
EMU_UNLINK(args[1].c_str());
}
else
{
BOX_ERROR("Error occured fetching object.");
::unlink(args[1].c_str());
EMU_UNLINK(args[1].c_str());
}
}
catch(...)
{
::unlink(args[1].c_str());
EMU_UNLINK(args[1].c_str());
BOX_ERROR("Error occured fetching object.");
}
}
Expand Down Expand Up @@ -1242,18 +1242,18 @@ void BackupQueries::CommandGet(std::vector<std::string> args, const bool *opts)
{
BOX_ERROR("Failed to fetch file: " <<
e.what());
::unlink(localName.c_str());
EMU_UNLINK(localName.c_str());
}
catch(std::exception &e)
{
BOX_ERROR("Failed to fetch file: " <<
e.what());
::unlink(localName.c_str());
EMU_UNLINK(localName.c_str());
}
catch(...)
{
BOX_ERROR("Failed to fetch file: unknown error");
::unlink(localName.c_str());
EMU_UNLINK(localName.c_str());
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/common/InvisibleTempFileStream.cpp
Expand Up @@ -31,7 +31,7 @@ InvisibleTempFileStream::InvisibleTempFileStream(const std::string& Filename,
#endif
{
#ifndef WIN32
if(unlink(Filename.c_str()) != 0)
if(EMU_UNLINK(Filename.c_str()) != 0)
{
MEMLEAKFINDER_NOT_A_LEAK(this);
THROW_EXCEPTION(CommonException, OSFileOpenError)
Expand Down
4 changes: 2 additions & 2 deletions lib/common/NamedLock.cpp
Expand Up @@ -256,7 +256,7 @@ void NamedLock::ReleaseLock()
// Windows, and there we need to close the file before deleting it,
// otherwise the system won't let us delete it.

if(::unlink(mFileName.c_str()) != 0)
if(EMU_UNLINK(mFileName.c_str()) != 0)
{
THROW_EMU_ERROR(
BOX_FILE_MESSAGE(mFileName, "Failed to delete lockfile"),
Expand Down Expand Up @@ -287,7 +287,7 @@ void NamedLock::ReleaseLock()
// On Windows we need to close the file before deleting it, otherwise
// the system won't let us delete it.

if(::unlink(mFileName.c_str()) != 0)
if(EMU_UNLINK(mFileName.c_str()) != 0)
{
THROW_EMU_ERROR(
BOX_FILE_MESSAGE(mFileName, "Failed to delete lockfile"),
Expand Down
4 changes: 2 additions & 2 deletions lib/common/Test.cpp
Expand Up @@ -122,7 +122,7 @@ bool setUp(const char* function_name)
int filetype = ObjectExists(filepath);
if(filetype == ObjectExists_File)
{
if(::unlink(filepath.c_str()) != 0)
if(EMU_UNLINK(filepath.c_str()) != 0)
{
TEST_FAIL_WITH_MESSAGE(BOX_SYS_ERROR_MESSAGE("Failed to delete "
"test fixture file: unlink(\"" << filepath << "\")"));
Expand Down Expand Up @@ -586,7 +586,7 @@ void TestRemoteProcessMemLeaksFunc(const char *filename,
}

// Delete it
::unlink(filename);
EMU_UNLINK(filename);
}
#endif
}
Expand Down
30 changes: 15 additions & 15 deletions lib/raidfile/RaidFileWrite.cpp
Expand Up @@ -358,7 +358,7 @@ void RaidFileWrite::Commit(bool ConvertToRaidNow)

#ifdef WIN32
// need to delete the target first
if(::unlink(renameTo.c_str()) != 0)
if(EMU_UNLINK(renameTo.c_str()) != 0)
{
DWORD errorNumber = GetLastError();
if (errorNumber != ERROR_FILE_NOT_FOUND)
Expand Down Expand Up @@ -421,9 +421,9 @@ void RaidFileWrite::Discard()
#ifdef WIN32
// On Win32 we must close it first
if (::close(mOSFileHandle) != 0 ||
::unlink(writeFilename.c_str()) != 0)
EMU_UNLINK(writeFilename.c_str()) != 0)
#else // !WIN32
if (::unlink(writeFilename.c_str()) != 0 ||
if (EMU_UNLINK(writeFilename.c_str()) != 0 ||
::close(mOSFileHandle) != 0)
#endif // !WIN32
{
Expand Down Expand Up @@ -670,7 +670,7 @@ void RaidFileWrite::TransformToRaidStorage()
// Must delete before renaming
#define CHECK_UNLINK(file) \
{ \
if (::unlink(file) != 0 && errno != ENOENT) \
if (EMU_UNLINK(file) != 0 && errno != ENOENT) \
{ \
THROW_EMU_ERROR("Failed to unlink raidfile " \
"stripe: " << file, RaidFileException, \
Expand All @@ -695,7 +695,7 @@ void RaidFileWrite::TransformToRaidStorage()
writeFile.Close();

// Finally delete the write file
if(::unlink(writeFilename.c_str()) != 0)
if(EMU_UNLINK(writeFilename.c_str()) != 0)
{
BOX_LOG_SYS_ERROR("Failed to delete file: " <<
writeFilename);
Expand All @@ -705,12 +705,12 @@ void RaidFileWrite::TransformToRaidStorage()
catch(...)
{
// Unlink all the dodgy files
::unlink(stripe1Filename.c_str());
::unlink(stripe2Filename.c_str());
::unlink(parityFilename.c_str());
::unlink(stripe1FilenameW.c_str());
::unlink(stripe2FilenameW.c_str());
::unlink(parityFilenameW.c_str());
EMU_UNLINK(stripe1Filename.c_str());
EMU_UNLINK(stripe2Filename.c_str());
EMU_UNLINK(parityFilename.c_str());
EMU_UNLINK(stripe1FilenameW.c_str());
EMU_UNLINK(stripe2FilenameW.c_str());
EMU_UNLINK(parityFilenameW.c_str());

// and send the error on its way
throw;
Expand Down Expand Up @@ -754,7 +754,7 @@ void RaidFileWrite::Delete()

// Attempt to delete it
bool deletedSomething = false;
if(::unlink(writeFilename.c_str()) == 0)
if(EMU_UNLINK(writeFilename.c_str()) == 0)
{
deletedSomething = true;
}
Expand All @@ -769,15 +769,15 @@ void RaidFileWrite::Delete()
std::string stripe1Filename(RaidFileUtil::MakeRaidComponentName(rdiscSet, mFilename, 0 % TRANSFORM_NUMBER_DISCS_REQUIRED));
std::string stripe2Filename(RaidFileUtil::MakeRaidComponentName(rdiscSet, mFilename, 1 % TRANSFORM_NUMBER_DISCS_REQUIRED));
std::string parityFilename(RaidFileUtil::MakeRaidComponentName(rdiscSet, mFilename, 2 % TRANSFORM_NUMBER_DISCS_REQUIRED));
if(::unlink(stripe1Filename.c_str()) == 0)
if(EMU_UNLINK(stripe1Filename.c_str()) == 0)
{
deletedSomething = true;
}
if(::unlink(stripe2Filename.c_str()) == 0)
if(EMU_UNLINK(stripe2Filename.c_str()) == 0)
{
deletedSomething = true;
}
if(::unlink(parityFilename.c_str()) == 0)
if(EMU_UNLINK(parityFilename.c_str()) == 0)
{
deletedSomething = true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/server/Daemon.cpp
Expand Up @@ -690,7 +690,7 @@ int Daemon::Main(const std::string &rConfigFileName)
}

// Delete the PID file
::unlink(pidFileName.c_str());
EMU_UNLINK(pidFileName.c_str());

// Log
BOX_NOTICE("Terminating daemon");
Expand Down
4 changes: 2 additions & 2 deletions lib/server/ServerControl.cpp
Expand Up @@ -239,7 +239,7 @@ bool KillServer(std::string pid_file, bool WaitForProcess)
#ifdef WIN32
if(WaitForProcess)
{
int unlink_result = unlink(pid_file.c_str());
int unlink_result = EMU_UNLINK(pid_file.c_str());
TEST_EQUAL_LINE(0, unlink_result, std::string("unlink ") + pid_file);
if(unlink_result != 0)
{
Expand Down Expand Up @@ -274,7 +274,7 @@ bool StopDaemon(int current_pid, const std::string& pid_file,
TEST_THAT_OR(!ServerIsAlive(current_pid), return false);

#ifdef WIN32
int unlink_result = unlink(pid_file.c_str());
int unlink_result = EMU_UNLINK(pid_file.c_str());
TEST_EQUAL_LINE(0, unlink_result, std::string("unlink ") + pid_file);
if(unlink_result != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/server/ServerStream.h
Expand Up @@ -204,7 +204,7 @@ class ServerStream : public Daemon
}

// unlink anything there
::unlink(c[1].c_str());
EMU_UNLINK(c[1].c_str());

psocket->Listen(Socket::TypeUNIX, c[1].c_str());
#endif // WIN32
Expand Down
22 changes: 14 additions & 8 deletions lib/win32/emu.h
Expand Up @@ -10,14 +10,18 @@

#ifdef WIN32
#define EMU_STRUCT_STAT struct emu_stat
#define EMU_STAT emu_stat
#define EMU_FSTAT emu_fstat
#define EMU_LSTAT emu_stat
#define EMU_STAT emu_stat
#define EMU_FSTAT emu_fstat
#define EMU_LSTAT emu_stat
#define EMU_LINK emu_link
#define EMU_UNLINK emu_unlink
#else
#define EMU_STRUCT_STAT struct stat
#define EMU_STAT ::stat
#define EMU_FSTAT ::fstat
#define EMU_LSTAT ::lstat
#define EMU_STAT ::stat
#define EMU_FSTAT ::fstat
#define EMU_LSTAT ::lstat
#define EMU_LINK ::link
#define EMU_UNLINK ::unlink
#endif

#ifdef WIN32
Expand Down Expand Up @@ -369,13 +373,15 @@ int emu_rename (const char* pOldName, const char* pNewName);

#define chdir(directory) emu_chdir (directory)
#define mkdir(path, mode) emu_mkdir (path)
#define link(oldpath, newpath) emu_link (oldpath, newpath)
#define unlink(file) emu_unlink (file)
#define utimes(buffer, times) emu_utimes (buffer, times)
#define chmod(file, mode) emu_chmod (file, mode)
#define getcwd(buffer, size) emu_getcwd (buffer, size)
#define rename(oldname, newname) emu_rename (oldname, newname)

// link() and unlink() conflict with Boost if implemented using macros like
// the others above, so I've removed the macros and you need to use EMU_LINK
// and EMU_UNLINK everywhere.

// Not safe to replace stat/fstat/lstat on mingw at least, as struct stat
// has a 16-bit st_ino and we need a 64-bit one.
//
Expand Down

0 comments on commit 04a7a8e

Please sign in to comment.