#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void need(bool ok,const char* why){if(!ok){std::fprintf(stderr,"FAIL: %s\n",why);std::exit(1);}} std::mutex mu; std::map> files; struct Stream{std::string path;}; std::map> live; std::vector messages; std::string mode; unsigned writes=0,closes=0,fail_write=0; const std::vector previous={'p','r','e','v','i','o','u','s'}; void logf(const char* format,...){char text[2048];va_list args;va_start(args,format);std::vsnprintf(text,sizeof text,format,args);va_end(args);std::lock_guard lk(mu);messages.emplace_back(text);} FILE* png_open(const char* name,const char* access){ std::lock_guard lk(mu); if(mode=="open-failure"){errno=EACCES;return nullptr;} if(std::strchr(access,'x') && files.count(name)){errno=EEXIST;return nullptr;} files[name].clear();auto s=std::make_unique();s->path=name; FILE* f=reinterpret_cast(s.get());live.emplace(f,std::move(s));return f; } size_t png_write(const void* data,size_t size,size_t count,FILE* f){ std::lock_guard lk(mu);auto& out=files.at(live.at(f)->path);writes++; const bool fail=(fail_write && writes==fail_write); size_t n=size*count;if(fail && n)n--; auto p=static_cast(data);out.insert(out.end(),p,p+n); if(fail){errno=ENOSPC;return size?n/size:0;}return count; } int png_close(FILE* f){std::lock_guard lk(mu);need(live.erase(f)==1,"exactly one close per owned stream");closes++;if(mode=="close-failure"){errno=ENOSPC;return EOF;}return 0;} int png_remove(const char* path){std::lock_guard lk(mu);if(mode=="cleanup-failure"){errno=EACCES;return -1;}if(!files.erase(path)){errno=ENOENT;return -1;}return 0;} int png_rename(const char* from,const char* to){ std::lock_guard lk(mu);if(mode=="publish-failure"){errno=EACCES;return -1;} files[to]=std::move(files.at(from));files.erase(from);return 0; } [[maybe_unused]] int png_move(const char* from,const char* to,unsigned){return png_rename(from,to)==0;} [[maybe_unused]] unsigned png_last_error(){return 5;} #ifndef MOVEFILE_REPLACE_EXISTING #define MOVEFILE_REPLACE_EXISTING 1 #endif #define fopen png_open #define fwrite png_write #define fclose png_close #define remove png_remove #define rename png_rename #define MoveFileExA png_move #define GetLastError png_last_error /* PNG */ #undef fopen #undef fwrite #undef fclose #undef remove #undef rename #undef MoveFileExA #undef GetLastError const uint32_t kShotW=257,kShotH=251; /* ANNOUNCE */ static std::vector pixels(unsigned seed){ std::vector out(size_t(kShotW)*kShotH*4); for(size_t i=0;i ready{0};std::atomic go{false};std::vector workers; for(unsigned i=1;i<=12;i++)workers.emplace_back([&,i]{ready++;while(!go.load())std::this_thread::yield();need(saved(mode=="same-target"?target:std::to_string(i)+".png",i),"concurrent capture succeeds");}); while(ready.load()!=12)std::this_thread::yield(); go=true; for(auto& t:workers)t.join(); need(live.empty(),"parallel files all closed");emit(argv[2]);return 0; } if(mode=="collisions" || mode=="collision-limit"){ unsigned count=mode=="collisions"?3:128; for(unsigned i=0;i>(pixels(17));announce(px,target,1); bool success=mode=="announce-success"; unsigned said=0;for(auto& m:messages)said+=m.find("written to")!=std::string::npos; need(said==(success?1u:0u),"success announcement matches actual publication"); need(success || files[target]==previous,"failed announced capture preserves target");emit(argv[2]);return 0; } const bool healthy=mode=="healthy"; bool ok=saved(target,17);need(ok==healthy,"publication status reflects write/close/replace result"); need(live.empty(),"all file handles closed on success and failure"); if(!healthy){ need(files[target]==previous,"failed capture preserves previous complete file"); need(!messages.empty() && messages.back().find("capture.png")!=std::string::npos,"failure identifies capture path"); need(files.size()==(mode=="cleanup-failure"?2u:1u),"only owned incomplete temporary may remain on cleanup failure"); if(mode=="cleanup-failure")return 0; mode="healthy";fail_write=0;need(saved(target,19),"subsequent capture recovers without disabling output"); } emit(argv[2]);return 0; }