Files
JetCore/__write.cpp
T
Brad Arant b0cadc3f08 Multi-day accumulated fixes: tag dispatch, session/pooling correctness, SPA rollout, jetc compiler
JetCore engine (Tag.cpp/.h, Global.cpp/.h, __*.cpp tag classes):
- Numerous __mysql.cpp connection-pooling correctness fixes (stale
  result sets, double-indirection sessionid bugs, field caching,
  pool keying) and a session-registration-before-connection-confirmed
  ordering fix (dangling pointer on a failed connect)
- Tag.cpp: large body of fixes to the tag-dispatch/container-scanning
  logic (bare-name fallback, database aliasing, single-quote handling,
  @-prefix session variable resolution, TAGDEF caching, nested split-tag
  scanning for if/while/until/ifrow so an inner one doesn't corrupt an
  outer one's own boundary scan) plus the process-first rendering
  order fix (a <process> now runs before <checksession>/page
  resolution, fixing a real "blank page after successful login" bug)
- Global.h/.cpp: added imapSessions (parallel to the existing __mysql*
  session map) for the new <imap> tag family
- __whiledir.cpp/__include.cpp: caching and directory-name-aliasing
  fixes
- __call.cpp/.h: fixed a dangling-pointer bug
- __write.cpp: fixed an O_WRONLY-only open that couldn't create a new
  file
- __ifrow.cpp/__sql.cpp: fixed the same sessionid double-indirection
  bug __mysql.cpp had (resolveKeyword's result was being used as a map
  key lookup instead of the resolved value itself)

jetc compiler (jetc/JetParser.cpp/.h, JetCodegen.cpp/.h, JetAST.h,
jetc.cpp, build-shared.sh, build-compiled.sh):
- Parser fixes: inline <include>, nested-quote handling, void-tag
  recognition
- Codegen fixes and expanded AST support
- build-shared.sh: atomic rename into place for a freshly-built .jtc
  (a live server dlopen()ing the same path mid-rebuild was corrupting
  its own already-mapped view of the old file) and a staleness check
  so a cached JetCore .o doesn't silently keep serving yesterday's
  fixes forever
- Added -lssl to both build scripts' link lines (needed once the new
  <imap> tag's client-side TLS code existed - previously nothing in
  this codebase used OpenSSL's SSL/TLS client API, only libcrypto)

Site content (jetc/barant-site/): SPA (fetch-and-swap navigation)
rollout across essentially every .page/.jet/.section/.tag/.proc/.method
file - dynamic-content fixes, fragment-mode stripping, native
$[@name] session variables replacing older mechanisms, and this
session's own checkout.page redesign, login start-param redirect fix,
and per-page side-menu (menu.bar/$[PAGE].menu) refresh support.
Completed the earlier main.css/control.css move from assets/ to
assets/images/ (git still showed the old paths as tracked-but-deleted
until now).

Deliberately left out of this commit: dozens of .pre-<description>-
<date> manual backup files (this session's and earlier ones' own
before/after snapshots, not meant for permanent history), compiled
.jtc build outputs, ad hoc test binaries and scripts under jetc/
(login_asan, wcwc_test, optest2_bin, etc.), and jetc/__pycache__/ -
none of these are meant to be tracked; a .gitignore covering them
would be worth adding separately.
2026-09-03 05:20:48 +00:00

76 lines
3.8 KiB
C++

#include "__write.h"
#include "Exception.h"
#include "Operand.h"
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
namespace jet {
__write::__write(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) {
output = false;
int mode = 0;
int len;
processContainer(container);
if(!keywordDefined("file"))
throw coreutils::Exception("write tag must have file defined.");
if(!keywordDefined("expr") && keywordDefined("value") && hasContainer)
throw coreutils::Exception("write tag cannot have both value and a container.");
if(keywordDefined("expr") && !keywordDefined("value") && hasContainer)
throw coreutils::Exception("write tag cannot have both expr and a container.");
if(keywordDefined("expr") && keywordDefined("value") && !hasContainer)
throw coreutils::Exception("write tag cannot have both expr and value.");
if(!keywordDefined("expr") && !keywordDefined("value") && !hasContainer)
throw coreutils::Exception("write tag must have a value, expr or a container.");
if(!keywordDefined("mode"))
throw coreutils::Exception("write tag must have a mode keyword.");
// resolveKeyword("mode") already returns the resolved VALUE (e.g.
// "overwrite") - re-indexing keywords[...] with that value treats
// it as if it were a MAP KEY instead (auto-vivifying an empty
// string, since no keyword is literally named "overwrite"/
// "append"), so this comparison was false for every real input
// and <write> threw "mode keyword must be..." unconditionally.
// Same double-indirection bug already fixed elsewhere in this
// codebase (__mysql.cpp's own constructor, __sql.cpp's sessionid
// handling) - confirmed via a real repro building the jet-2.0
// htmleditor port's file-based storage.
coreutils::MString modeValue = resolveKeyword("mode");
if(modeValue == "append")
mode = O_APPEND;
else if(modeValue == "overwrite")
mode = O_TRUNC;
else
throw coreutils::Exception("mode keyword must be 'overwrite' or 'append'.");
// mode alone (O_APPEND or O_TRUNC) has no access-mode bits at
// all - open()'s access mode defaults to O_RDONLY (0) unless
// O_WRONLY/O_RDWR is explicitly OR'd in, so the write() call below
// was failing on every real invocation (EBADF - can't write a
// read-only fd), and without O_CREAT this could never create a
// NEW file either, only (theoretically) write an existing one.
// Confirmed via a real repro: <write file="..." mode="overwrite">
// silently produced a 0-byte or missing file every time.
// Same double-indirection bug as "mode" above.
int fd = open(resolveKeyword("file").c_str(), mode | O_WRONLY | O_CREAT, 0644); // TODO: Need to add AUTH flags.
if(hasContainer && !evaluate)
len = write(fd, container.getData(), container.getLength());
else if(hasContainer && evaluate)
len = write(fd, out.getData(), out.getLength());
else if(!hasContainer && keywordDefined("value")) {
// Same double-indirection bug as "mode"/"file" above:
// variables[resolveKeyword("value")] used the resolved VALUE
// itself as a lookup key into global.variables (a DIFFERENT map
// entirely), not the literal value to write - looking up a
// variable literally named e.g. "hello world" auto-vivifies an
// empty entry, so this wrote 0 bytes for every real invocation.
// Confirmed via a real repro: <write value="hello world"
// mode="overwrite"> produced a genuinely empty file.
coreutils::MString writeValue = resolveKeyword("value");
len = write(fd, writeValue.getData(), writeValue.getLength());
}
else if(!hasContainer && keywordDefined("expr"))
len = write(fd, keywords["expr"].getData(), keywords["expr"].getLength());
close(fd);
}
}