Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/libstore/builtins/buildenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ createLinks(State & state, const std::filesystem::path & srcDir, const std::file
auto target = canonPath(dstFile, true);
if (!S_ISDIR(lstat(target).st_mode))
throw Error("collision between %1% and non-directory %2%", PathFmt(srcFile), PathFmt(target));
if (unlink(dstFile.c_str()) == -1)
throw SysError("unlinking %1%", PathFmt(dstFile));
unlink(dstFile);
if (mkdir(
dstFile.c_str()
#ifndef _WIN32 // TODO abstract mkdir perms for Windows
Expand All @@ -108,8 +107,7 @@ createLinks(State & state, const std::filesystem::path & srcDir, const std::file
throw BuildEnvFileConflictError(readLink(dstFile), srcFile, priority);
if (prevPriority < priority)
continue;
if (unlink(dstFile.c_str()) == -1)
throw SysError("unlinking %1%", PathFmt(dstFile));
unlink(dstFile);
} else if (S_ISDIR(dstSt.st_mode))
throw Error("collision between non-directory '%1%' and directory '%2%'", srcFile, dstFile);
}
Expand Down
9 changes: 4 additions & 5 deletions src/libstore/gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void LocalStore::createTempRootsFile()
if (pathExists(fnTempRoots))
/* It *must* be stale, since there can be no two
processes with the same pid. */
unlink(fnTempRoots.string().c_str());
tryUnlink(fnTempRoots);

*fdTempRoots = openLockFile(fnTempRoots, true);

Expand Down Expand Up @@ -190,7 +190,7 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
we don't care about its temporary roots. */
if (lockFile(fd.get(), ltWrite, false)) {
printInfo("removing stale temporary roots file %1%", PathFmt(path));
unlink(path.string().c_str());
tryUnlink(path);
writeFull(fd.get(), "d");
continue;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ void LocalStore::findRoots(const std::filesystem::path & path, std::filesystem::
if (!pathExists(target)) {
if (isInDir(path, config->stateDir.get() / gcRootsDir / "auto")) {
printInfo("removing stale link from %1% to %2%", PathFmt(path), PathFmt(target));
unlink(path.string().c_str());
tryUnlink(path);
}
} else {
if (!std::filesystem::is_symlink(target))
Expand Down Expand Up @@ -782,8 +782,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)

printMsg(lvlTalkative, "deleting unused link %1%", PathFmt(path));

if (unlink(path.string().c_str()) == -1)
throw SysError("deleting %1%", PathFmt(path));
unlink(path);

/* Do not account for deleted file here. Rely on deletePath()
accounting. */
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ LocalStore::~LocalStore()
auto fdTempRoots(_fdTempRoots.lock());
if (*fdTempRoots) {
fdTempRoots->close();
unlink(fnTempRoots.string().c_str());
tryUnlink(fnTempRoots);
}
} catch (...) {
ignoreExceptionInDestructor();
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/unix/pathlocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void deleteLockFile(const std::filesystem::path & path, Descriptor desc)
races. Write a (meaningless) token to the file to indicate to
other processes waiting on this lock that the lock is stale
(deleted). */
unlink(path.c_str());
tryUnlink(path);
writeFull(desc, "d");
/* Note that the result of unlink() is ignored; removing the lock
/* We just try to unlink don't care if it fails; removing the lock
file is an optimisation, not a necessity. */
}

Expand Down
21 changes: 20 additions & 1 deletion src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ AutoCloseFD createAnonymousTempFile()
if (!fd2)
throw SysError("creating temporary file %s", PathFmt(path));
fd = std::move(fd2);
unlink(requireCString(path)); /* We only care about the file descriptor. */
tryUnlink(path); /* We only care about the file descriptor. */
#endif

return fd;
Expand Down Expand Up @@ -762,6 +762,25 @@ void chmod(const std::filesystem::path & path, mode_t mode)
throw SysError("setting permissions on %s", PathFmt(path));
}

#ifdef _WIN32
# define UNLINK_PROC ::_wunlink
#else
# define UNLINK_PROC ::unlink
#endif

void unlink(const std::filesystem::path & path)
{
if (UNLINK_PROC(path.c_str()) == -1)
throw SysError("removing %s", PathFmt(path));
}

void tryUnlink(const std::filesystem::path & path)
{
UNLINK_PROC(path.c_str());
}

#undef UNLINK_PROC

bool chmodIfNeeded(const std::filesystem::path & path, mode_t mode, mode_t mask)
{
auto prevMode = lstat(path).st_mode;
Expand Down
14 changes: 14 additions & 0 deletions src/libutil/include/nix/util/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,20 @@ bool chmodIfNeeded(const std::filesystem::path & path, mode_t mode, mode_t mask
*/
void chmod(const std::filesystem::path & path, mode_t mode);

/**
* Remove a file, throwing an exception on error.
*
* @param path Path to the file to remove.
*/
void unlink(const std::filesystem::path & path);

/**
* Try to remove a file, ignoring errors.
*
* @param path Path to the file to try to remove.
*/
void tryUnlink(const std::filesystem::path & path);

/**
* @brief A directory iterator that can be used to iterate over the
* contents of a directory. It is similar to std::filesystem::directory_iterator
Expand Down
7 changes: 1 addition & 6 deletions src/libutil/unix-domain-socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ bindConnectProcHelper(std::string_view operationName, auto && operation, Socket

void bind(Socket fd, const std::filesystem::path & path)
{
#ifdef _WIN32
_wunlink
#else
unlink
#endif
(path.c_str());
tryUnlink(path);

bindConnectProcHelper("bind", ::bind, fd, path.string());
}
Expand Down
3 changes: 1 addition & 2 deletions src/nix/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ static void update(const StringSet & channelNames)
if (lstat(nixDefExpr.c_str(), &st) == 0) {
if (S_ISLNK(st.st_mode))
// old-skool ~/.nix-defexpr
if (unlink(nixDefExpr.c_str()) == -1)
throw SysError("unlinking %1%", PathFmt(nixDefExpr));
unlink(nixDefExpr);
} else if (errno != ENOENT) {
throw SysError("getting status of %1%", PathFmt(nixDefExpr));
}
Expand Down
Loading