Files
CoreUtils/File.h
T
Brad ArantandClaude Sonnet 5 05374bdb15 Fix MString::read() infinite retry loop, HTTPRequest parser fd leak, Log deadlock, log rotation symlink handling, and JString bounds checks
- MString::read() now gives up after repeated I/O errors instead of
  looping forever and crashing via realloc() failure (root cause of a
  recurring barant.com production crash).
- HTTPRequest::parse() no longer leaks lineParser/headerParser on a
  malformed request.
- Log gained size-based rotation (config-driven) and a non-static-mutex
  fix; a separate fix stopped rotation from destroying the logs symlink
  on its first trigger.
- File.cpp: symlink-preserving rotation support.
- JString: fixed an array-index out-of-bounds and a setString issue.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-06 17:38:14 +00:00

68 lines
1.7 KiB
C++

#ifndef __File_h__
#define __File_h__
#include <string>
#include <fcntl.h>
#include "ZString.h"
///
/// File
///
/// File abstraction class for accessing local file system files.
///
namespace coreutils {
class File {
public:
File(ZString fileName, int mode = O_RDONLY, int authority = 0664);
virtual ~File();
void setBufferSize(size_t size);
int read();
int readLine();
void write(std::string data);
void write(coreutils::ZString &data);
std::string asString();
coreutils::ZString asZString();
char *buffer;
size_t size;
bool eof();
///
/// Real, on-disk size of this file right now, queried directly
/// from the OS via fstat() - not the "size" member above, which
/// is only ever set at open()/read() time and goes stale the
/// moment anything else appends to this fd (exactly the case for
/// a long-lived, append-mode file such as Log's static logFile).
///
size_t currentSize();
///
/// Close this file, shift any existing ".1" .. ".maxBackups-1"
/// backups up by one (deleting whatever already sits at
/// ".maxBackups"), rename the current file to ".1", then reopen
/// a fresh file at the original path/mode/authority. Meant for a
/// long-lived, append-mode file that needs a size cap - not
/// meaningful for a file opened for reading.
///
void rotate(int maxBackups);
coreutils::ZString fileName;
private:
void open(coreutils::ZString fileName, int mode, int authority);
int fd;
int mode;
int authority;
coreutils::ZString zstring;
bool mEof = false;
};
}
#endif