mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
3396c97da9
Now that the new CMake build system is encouraging us to lay out the code like a set of libraries, it seems like a good idea to make them look more _like_ libraries, by putting things into separate modules as far as possible. This fixes several previous annoyances in which you had to link against some object in order to get a function you needed, but that object also contained other functions you didn't need which included link-time symbol references you didn't want to have to deal with. The usual offender was subsidiary supporting programs including misc.c for some innocuous function and then finding they had to deal with the requirements of buildinfo(). This big reorganisation introduces three new subdirectories called 'utils', one at the top level and one in each platform subdir. In each case, the directory contains basically the same files that were previously placed in the 'utils' build-time library, except that the ones that were extremely miscellaneous (misc.c, utils.c, uxmisc.c, winmisc.c, winmiscs.c, winutils.c) have been split up into much smaller pieces.
45 lines
1.5 KiB
C
45 lines
1.5 KiB
C
/*
|
|
* Unix implementation of open_for_write_would_lose_data().
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include "putty.h"
|
|
|
|
bool open_for_write_would_lose_data(const Filename *fn)
|
|
{
|
|
struct stat st;
|
|
|
|
if (stat(fn->path, &st) < 0) {
|
|
/*
|
|
* If the file doesn't even exist, we obviously want to return
|
|
* false. If we failed to stat it for any other reason,
|
|
* ignoring the precise error code and returning false still
|
|
* doesn't seem too unreasonable, because then we'll try to
|
|
* open the file for writing and report _that_ error, which is
|
|
* likely to be more to the point.
|
|
*/
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* OK, something exists at this pathname and we've found out
|
|
* something about it. But an open-for-write will only
|
|
* destructively truncate it if it's a regular file with nonzero
|
|
* size. If it's empty, or some other kind of special thing like a
|
|
* character device (e.g. /dev/tty) or a named pipe, then opening
|
|
* it for write is already non-destructive and it's pointless and
|
|
* annoying to warn about it just because the same file can be
|
|
* opened for reading. (Indeed, if it's a named pipe, opening it
|
|
* for reading actually _causes inconvenience_ in its own right,
|
|
* even before the question of whether it gives misleading
|
|
* information.)
|
|
*/
|
|
if (S_ISREG(st.st_mode) && st.st_size > 0) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|