// // Example location script for 2BrightSparks SyncBackPro: Chaos. // // SBLang=Pascal // // http://www.2BrightSparks.com/ // November 2026 // // Chaos is a storage location that misbehaves on purpose. It keeps your files // in an ordinary folder, but it can fail copies, slow down, lose the // connection, quietly damage a file, report the wrong file size or leave files // out of the folder listing. Use it to see what SyncBack does when storage // goes wrong: which settings retry, which ones notice damage (e.g. verifying // files after they are copied), and what ends up in the log. // // Do not use it for real backups! // // Settings // -------- // Each profile that uses Chaos has a Chaos page in its profile setup, so // different profiles can misbehave in different ways: // // Folder Where the files are kept (blank = %LOCALAPPDATA%\SyncBack Chaos\) // Copies to fail Percent of copies to Chaos that fail // Copies from fail Percent of copies from Chaos that fail // Deletes fail Percent of deletes that fail // Damaged copies Percent of copies to Chaos that are damaged (one byte added) // Wrong sizes Percent of files listed with the wrong size // Hidden files Percent of files left out of the folder listing // Lose connection After this many copies or deletes (0 = never) // Slow down Wait this many seconds before each copy // Seed Repeat the same chaos every run (0 = different every time) // // Everything is 0 to start with, so Chaos behaves like an ordinary folder // until you turn something on. The settings are read each time the profile // connects, so you can change them between runs. Everything Chaos does on // purpose is written to Chaos.log in the folder above, and every error it // returns starts with "Chaos:". // // Paths: a profile path of \Backup\ is the folder Backup inside Root. // var gFSO, gRoot, gLogFile; gFailPut, gFailGet, gFailDelete, gCorruptPut, gWrongSize, gHideFiles; gDropAfter, gSlowSecs, gRand, gOps, gDropped; // // This is a location script // function Description(var ScriptType): String; begin Result:='Chaos'; ScriptType:=SCRIPTTYPE_LOCATION + SCRIPTTYPE_CONFIG; end; function Install(Interactive); begin Result:=''; end; procedure Uninstall; begin end; function FilesToExport(Interactive, Cnt); begin Result:=''; end; //----------------------------------------------------------------------------- // Helpers //----------------------------------------------------------------------------- // Write a line to Chaos.log procedure ChaosLog(Msg); var F; begin try F:=gFSO.OpenTextFile(gLogFile, 8, TRUE, -1); // append, create, Unicode F.WriteLine(FormatDateTime('yyyy-mm-dd hh:nn:ss', Now) + ' ' + Msg); F.Close; except end; end; // The next pseudo-random number, 0 to 99. With a seed this repeats exactly. function NextRandom; begin gRand:=(gRand * 421 + 54773) mod 259200; Result:=(gRand * 100) div 259200; end; // Should something happen that has a Pct percent chance? function Chance(Pct); begin if (Pct <= 0) then Result:=FALSE else if (Pct >= 100) then Result:=TRUE else Result:=(NextRandom < Pct); end; // The name each setting is stored under in the profile, by its control tag // on the Chaos page of the profile setup (see ConfigSetupDisplay) function SettingName(Tag); begin if (Tag = 1) then Result:='ChaosRoot' else if (Tag = 2) then Result:='ChaosFailPut' else if (Tag = 3) then Result:='ChaosFailGet' else if (Tag = 4) then Result:='ChaosFailDelete' else if (Tag = 5) then Result:='ChaosCorruptPut' else if (Tag = 6) then Result:='ChaosWrongSize' else if (Tag = 7) then Result:='ChaosHideFiles' else if (Tag = 8) then Result:='ChaosDropAfter' else if (Tag = 9) then Result:='ChaosSlowSecs' else if (Tag = 10) then Result:='ChaosSeed' else Result:=''; end; // A number from the profile's Chaos settings (0 if it is not set) function NumSetting(Tag); begin Result:=StrToIntDef(Trim(SBVariables.GetProperty(SettingName(Tag), '0', FALSE)), 0); end; // Read the profile's Chaos settings procedure LoadSettings; var Seed, Shell; begin gRoot:=Trim(SBVariables.GetProperty(SettingName(1), '', FALSE)); gFailPut:=NumSetting(2); gFailGet:=NumSetting(3); gFailDelete:=NumSetting(4); gCorruptPut:=NumSetting(5); gWrongSize:=NumSetting(6); gHideFiles:=NumSetting(7); gDropAfter:=NumSetting(8); gSlowSecs:=NumSetting(9); Seed:=NumSetting(10); Shell:=CreateOleObject('WScript.Shell'); if (gRoot = '') then gRoot:='%LOCALAPPDATA%\SyncBack Chaos\'; gRoot:=IncludeTrailingPathDelimiter(Shell.ExpandEnvironmentStrings(gRoot)); Shell:=Unassigned; // A seed repeats the same sequence; otherwise start from the time of day if (Seed <> 0) then gRand:=Abs(Seed) mod 259200 else gRand:=Trunc(Frac(Now) * 86400000) mod 259200; end; // The real file or folder for a location path, e.g. \Backup\a.txt function RealName(P); begin while (Copy(P, 1, 1) = '\') do P:=Copy(P, 2, Length(P)); Result:=gRoot + P; end; // Create a folder and any parent folders it needs procedure ForceDir(D); var P; begin D:=ExcludeTrailingPathDelimiter(D); if (D = '') or DirectoryExists(D) then Exit; P:=ExtractFilePath(D); if (P <> '') and (ExcludeTrailingPathDelimiter(P) <> D) then ForceDir(P); CreateDir(D); end; // Count a file operation, and lose the connection when it is time to function CountOp; begin gOps:=gOps + 1; if (gDropAfter > 0) and (gOps >= gDropAfter) and not gDropped then begin gDropped:=TRUE; ChaosLog('Connection lost after ' + IntToStr(gOps) + ' operations'); end; Result:=gDropped; end; procedure MakeWritable(Filename); var FileObj; begin if gFSO.FileExists(Filename) then begin FileObj:=gFSO.GetFile(Filename); if (FileObj.Attributes and 1 <> 0) then FileObj.Attributes:=FileObj.Attributes - 1; end; end; //----------------------------------------------------------------------------- // Connecting //----------------------------------------------------------------------------- function LocConnect(MainThread); begin gFSO:=CreateOleObject('Scripting.FileSystemObject'); LoadSettings; // Beside the files, not beside the script: an installed script is in // Program Files, which SyncBack cannot write to gLogFile:=gRoot + 'Chaos.log'; gOps:=0; gDropped:=FALSE; ForceDir(gRoot); if not DirectoryExists(gRoot) then begin Result:='Chaos: cannot create the folder ' + gRoot; Exit; end; ChaosLog('Connected to ' + gRoot + ' (FailPut=' + IntToStr(gFailPut) + ' FailGet=' + IntToStr(gFailGet) + ' FailDelete=' + IntToStr(gFailDelete) + ' CorruptPut=' + IntToStr(gCorruptPut) + ' WrongSize=' + IntToStr(gWrongSize) + ' HideFiles=' + IntToStr(gHideFiles) + ' DropAfter=' + IntToStr(gDropAfter) + ' SlowSecs=' + IntToStr(gSlowSecs) + ')'); Result:=''; end; function LocDisconnect; begin gFSO:=Unassigned; Result:=''; end; function LocScanPrepare; begin Result:=''; end; // // SyncBack asks this before it uses the location. Once the connection has been // "lost" it stays lost until SyncBack calls LocReconnect. // function LocConnected; begin if gDropped then Result:='Chaos: the connection was lost' else Result:=''; end; function LocReconnect; begin if gDropped then ChaosLog('Reconnected'); gDropped:=FALSE; gOps:=0; Result:=''; end; //----------------------------------------------------------------------------- // Folders //----------------------------------------------------------------------------- function LocDirExists(FullPath); begin if gFSO.FolderExists(RealName(FullPath)) then Result:='' else Result:='Folder does not exist'; end; function LocMakeDir(FullPath); begin Result:=''; if not gFSO.FolderExists(RealName(FullPath)) then ForceDir(RealName(FullPath)); if not gFSO.FolderExists(RealName(FullPath)) then Result:='Cannot create the folder'; end; function LocRemoveDir(FullPath); var Folder; begin Result:=''; if (RealName(FullPath) = gRoot) or not gFSO.FolderExists(RealName(FullPath)) then Exit; Folder:=gFSO.GetFolder(RealName(FullPath)); if (Folder.Files.Count > 0) or (Folder.SubFolders.Count > 0) then Result:='Directory is not empty' else Folder.Delete(TRUE); end; function LocRenameDir(FromPath, ToPath); var FromDir, ToDir, TempDir; begin FromDir:=ExcludeTrailingPathDelimiter(RealName(FromPath)); ToDir:=ExcludeTrailingPathDelimiter(RealName(ToPath)); if not gFSO.FolderExists(FromDir) then begin Result:='Source directory does not exist'; Exit; end; // A change of case only must be done in two steps if SBSystem.SameFilenames(FromDir, ToDir, FALSE) then begin TempDir:=ToDir + '.' + SBSystem.UniqueID; gFSO.MoveFolder(FromDir, TempDir); gFSO.MoveFolder(TempDir, ToDir); end else begin if gFSO.FolderExists(ToDir) then begin Result:='To destination directory already exists'; Exit; end; gFSO.MoveFolder(FromDir, ToDir); end; Result:=''; end; // // List a folder. Some files may be listed with the wrong size, or not at all. // function LocScanList(FullPath); var Folder, SubFol, FileItem, FoldersEnum, FilesEnum, Size; begin Result:=''; if not gFSO.FolderExists(RealName(FullPath)) then begin Result:='Folder does not exist'; Exit; end; Folder:=gFSO.GetFolder(RealName(FullPath)); FoldersEnum:=TEnumVariant.Create(Folder.SubFolders); try while FoldersEnum.ForEach(SubFol) do if not SBLocation.AddDirEx2(SubFol.Name, SubFol.Attributes, SubFol.DateLastModified, SubFol.DateCreated, '') then Exit; finally FoldersEnum.Free; end; FilesEnum:=TEnumVariant.Create(Folder.Files); try while FilesEnum.ForEach(FileItem) do begin // Chaos.log lives in the root folder but is not one of your files if (UpperCase(IncludeTrailingPathDelimiter(RealName(FullPath)) + FileItem.Name) = UpperCase(gLogFile)) then Continue; if Chance(gHideFiles) then begin ChaosLog('Hid ' + FullPath + FileItem.Name + ' from the listing'); Continue; end; Size:=VarToInt64(FileItem.Size); if Chance(gWrongSize) then begin ChaosLog('Listed ' + FullPath + FileItem.Name + ' with the wrong size'); Size:=Size + 1; end; if not SBLocation.AddFileEx(FileItem.Name, '', Size, FileItem.Attributes, FileItem.DateLastModified, FileItem.DateCreated, '') then Exit; end; finally FilesEnum.Free; end; end; //----------------------------------------------------------------------------- // Files //----------------------------------------------------------------------------- function LocFileExists(FullPath); begin if gFSO.FileExists(RealName(FullPath)) then Result:='' else Result:='File does not exist'; end; function LocRenameFile(FromName, ToName); var FromFile, ToFile, FileObj, Attrs; begin FromFile:=RealName(FromName); ToFile:=RealName(ToName); if not gFSO.FileExists(FromFile) then begin Result:='Source file does not exist'; Exit; end; if gFSO.FileExists(ToFile) and not SBSystem.SameFilenames(FromFile, ToFile, FALSE) then begin Result:='To destination file already exists'; Exit; end; // A rename gives the file the archive attribute, so put the old ones back FileObj:=gFSO.GetFile(FromFile); Attrs:=FileObj.Attributes; gFSO.MoveFile(FromFile, ToFile); FileObj:=gFSO.GetFile(ToFile); FileObj.Attributes:=Attrs; Result:=''; end; function LocDeleteFile(FullPath; var DoesNotExist); begin Result:=''; DoesNotExist:=not gFSO.FileExists(RealName(FullPath)); if DoesNotExist then Exit; if CountOp then begin Result:='Chaos: the connection was lost'; Exit; end; if Chance(gFailDelete) then begin ChaosLog('Failed to delete ' + FullPath); Result:='Chaos: the delete failed'; Exit; end; MakeWritable(RealName(FullPath)); gFSO.DeleteFile(RealName(FullPath), TRUE); end; function LocCRC32(FullPath; var CRC32); begin CRC32:=SBSystem.CRC32(RealName(FullPath)); Result:=''; end; function LocSetModDateTime(FullPath, ModDateTime); begin Result:=SBSystem.SetLastModDateTime(RealName(FullPath), ModDateTime); end; function LocSetCreateDateTime(FullPath, CreateDateTime); begin Result:=SBSystem.SetCreateDateTime(RealName(FullPath), CreateDateTime); end; function LocGetAttributes(Filename; var Attrs); var Real; begin Real:=RealName(Filename); if gFSO.FolderExists(Real) then Attrs:=gFSO.GetFolder(Real).Attributes else if gFSO.FileExists(Real) then Attrs:=gFSO.GetFile(Real).Attributes else begin Result:='File or folder does not exist'; Exit; end; Result:=''; end; function LocSetAttributes(Filename, Attrs); var Real; begin Real:=RealName(Filename); if gFSO.FolderExists(Real) then gFSO.GetFolder(Real).Attributes:=Attrs else if gFSO.FileExists(Real) then gFSO.GetFile(Real).Attributes:=Attrs else begin Result:='File or folder does not exist'; Exit; end; Result:=''; end; function LocFreeSpace; begin Result:=-1; try Result:=VarToInt64(gFSO.GetDrive(gFSO.GetDriveName(gRoot)).FreeSpace); except Result:=-1; end; end; // // The abilities are those of an ordinary folder (see Vault.pas for the list) // function LocAbilities; begin Result:=CAN_COPYDIRATTRS + CAN_USEATTRIBUTES + CAN_CHANGEATTRIBUTES + CAN_CHANGEDATETIME + CAN_HAVEEMPTYPATH + CAN_VERSION + CAN_MOVE_FILES + CAN_USECRC32 + CAN_MOVE_FOLDERS; end; // // Copy a file to Chaos. It may be slow, fail, or arrive damaged. // function LocPutEx(fromFName, toFName, fromAttrs, fromDateTime, fromCreateDateTime, fromFileSize, fromNTFSSec, DoSafeCopy; var SafeFName); var Target, r, F; begin if CountOp then begin Result:='Chaos: the connection was lost'; Exit; end; if (gSlowSecs > 0) then SBSystem.Sleep(gSlowSecs); if Chance(gFailPut) then begin ChaosLog('Failed to copy ' + fromFName + ' to ' + toFName); Result:='Chaos: the copy failed'; Exit; end; if DoSafeCopy then begin SafeFName:=toFName + '.$$$'; Target:=RealName(SafeFName); end else begin SafeFName:=''; Target:=RealName(toFName); end; MakeWritable(Target); r:=CopyFile(fromFName, Target); if (r <> 0) then begin Result:=SysErrorMessage(r); Exit; end; // Damage it, without saying so if Chance(gCorruptPut) then begin ChaosLog('Damaged ' + toFName + ' (one byte added)'); F:=gFSO.OpenTextFile(Target, 8, FALSE); F.Write('!'); F.Close; end; if (fromAttrs >= 0) then gFSO.GetFile(Target).Attributes:=fromAttrs; if (fromDateTime > 1.0) then SBSystem.SetLastModDateTime(Target, fromDateTime); if (fromCreateDateTime > 1.0) then SBSystem.SetCreateDateTime(Target, fromCreateDateTime); Result:=''; end; // // Copy a file from Chaos. It may be slow or fail. // function LocGet(fromFName, toFName); var r; begin if CountOp then begin Result:='Chaos: the connection was lost'; Exit; end; if (gSlowSecs > 0) then SBSystem.Sleep(gSlowSecs); if Chance(gFailGet) then begin ChaosLog('Failed to copy ' + fromFName + ' to ' + toFName); Result:='Chaos: the copy failed'; Exit; end; if not gFSO.FileExists(RealName(fromFName)) then begin Result:='File does not exist'; Exit; end; MakeWritable(toFName); r:=CopyFile(RealName(fromFName), toFName); if (r = 0) then Result:='' else Result:=SysErrorMessage(r); end; //----------------------------------------------------------------------------- // The Chaos page in the profile setup //----------------------------------------------------------------------------- // Only in profiles that use Chaos as their location function ConfigWantSetupNode(IsGroup); begin Result:=not IsGroup and SBProfile.UsesScript; end; function ConfigNodeCaption; begin Result:='Chaos'; end; function ConfigCanRevert; begin Result:=TRUE; end; function ConfigCanClose; begin Result:=TRUE; end; procedure ConfigFactoryDefaults; var Tag; begin for Tag:=1 to 10 do SBVariables.DeleteProperty(SettingName(Tag)); end; procedure ConfigSetupDisplay; begin SBProfile.AddLabel('Chaos misbehaves on purpose. Do not use it for real backups.', 20); SBProfile.AddEdit('Folder where Chaos keeps the files (blank for %LOCALAPPDATA%\SyncBack Chaos\)', 260, FALSE, FALSE, 1); SBProfile.AddEdit('Percent of copies to Chaos that fail', 3, TRUE, FALSE, 2); SBProfile.AddEdit('Percent of copies from Chaos that fail', 3, TRUE, FALSE, 3); SBProfile.AddEdit('Percent of deletes that fail', 3, TRUE, FALSE, 4); SBProfile.AddEdit('Percent of copies to Chaos that are damaged (one byte added)', 3, TRUE, FALSE, 5); SBProfile.AddEdit('Percent of files listed with the wrong size', 3, TRUE, FALSE, 6); SBProfile.AddEdit('Percent of files left out of the folder listing', 3, TRUE, FALSE, 7); SBProfile.AddEdit('Lose the connection after this many copies or deletes (0 = never)', 6, TRUE, FALSE, 8); SBProfile.AddEdit('Seconds to wait before each copy', 4, TRUE, FALSE, 9); SBProfile.AddEdit('Seed, to repeat the same chaos every run (0 = different every time)', 9, TRUE, FALSE, 10); end; function ConfigLoadSettings; var Tag; begin Result:=''; SBProfile.SetEdit(SBVariables.GetProperty(SettingName(1), '', FALSE), 1); for Tag:=2 to 10 do SBProfile.SetEdit(IntToStr(NumSetting(Tag)), Tag); end; // Store a setting in the profile. One that has never been set and is still // the default is left unset, so just looking at this page and pressing Cancel // does not count as a change to the profile. procedure SaveSetting(Name, Value, Default); begin if (SBVariables.GetProperty(Name, '!NOTSET!', FALSE) = '!NOTSET!') and (Value = Default) then Exit; SBVariables.SetProperty(Name, Value); end; function ConfigSaveSettings(Silent); var Tag; begin Result:=''; for Tag:=2 to 7 do if (StrToIntDef(Trim(SBProfile.GetEdit(Tag)), 0) > 100) then begin Result:='Chaos: a percentage cannot be more than 100'; Exit; end; SaveSetting(SettingName(1), Trim(SBProfile.GetEdit(1)), ''); for Tag:=2 to 10 do SaveSetting(SettingName(Tag), IntToStr(StrToIntDef(Trim(SBProfile.GetEdit(Tag)), 0)), '0'); end; procedure ConfigUpdateConditionals; begin end;