Fossil SCM

fossil-scm / src / manifest.c
Source Blame History 3254 lines
dbda8d6… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2007 D. Richard Hipp
dbda8d6… drh 3 **
dbda8d6… drh 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
c06edd2… drh 7
dbda8d6… drh 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
dbda8d6… drh 11 **
dbda8d6… drh 12 ** Author contact information:
dbda8d6… drh 13 ** [email protected]
dbda8d6… drh 14 ** http://www.hwaci.com/drh/
dbda8d6… drh 15 **
dbda8d6… drh 16 *******************************************************************************
dbda8d6… drh 17 **
d0305b3… aku 18 ** This file contains code used to cross link control files and
d0305b3… aku 19 ** manifests. The file is named "manifest.c" because it was
d0305b3… aku 20 ** original only used to parse manifests. Then later clusters
d0305b3… aku 21 ** and control files and wiki pages and tickets were added.
dbda8d6… drh 22 */
dbda8d6… drh 23 #include "config.h"
dbda8d6… drh 24 #include "manifest.h"
dbda8d6… drh 25 #include <assert.h>
dbda8d6… drh 26
dbda8d6… drh 27 #if INTERFACE
dbda8d6… drh 28 /*
d0305b3… aku 29 ** Types of control files
d0305b3… aku 30 */
d13054c… drh 31 #define CFTYPE_ANY 0
d0305b3… aku 32 #define CFTYPE_MANIFEST 1
d0305b3… aku 33 #define CFTYPE_CLUSTER 2
d0305b3… aku 34 #define CFTYPE_CONTROL 3
d0305b3… aku 35 #define CFTYPE_WIKI 4
d0305b3… aku 36 #define CFTYPE_TICKET 5
7c2577b… drh 37 #define CFTYPE_ATTACHMENT 6
d96c4a4… drh 38 #define CFTYPE_EVENT 7
99fcc43… drh 39 #define CFTYPE_FORUM 8
e4f1c1f… drh 40
e4f1c1f… drh 41 /*
e4f1c1f… drh 42 ** File permissions used by Fossil internally.
e4f1c1f… drh 43 */
e4f1c1f… drh 44 #define PERM_REG 0 /* regular file */
e4f1c1f… drh 45 #define PERM_EXE 1 /* executable */
e4f1c1f… drh 46 #define PERM_LNK 2 /* symlink */
e4f1c1f… drh 47
e4f1c1f… drh 48 /*
1311841… jan.nijtmans 49 ** Flags for use with manifest_crosslink().
1311841… jan.nijtmans 50 */
1311841… jan.nijtmans 51 #define MC_NONE 0 /* default handling */
1311841… jan.nijtmans 52 #define MC_PERMIT_HOOKS 1 /* permit hooks to execute */
bf3db3d… drh 53 #define MC_NO_ERRORS 2 /* do not issue errors for a bad parse */
1311841… jan.nijtmans 54
1311841… jan.nijtmans 55 /*
d13054c… drh 56 ** A single F-card within a manifest
d13054c… drh 57 */
52b35c8… jan.nijtmans 58 struct ManifestFile {
d13054c… drh 59 char *zName; /* Name of a file */
fd9b7bd… drh 60 char *zUuid; /* Artifact hash for the file */
d13054c… drh 61 char *zPerm; /* File permissions */
d13054c… drh 62 char *zPrior; /* Prior name if the name was changed */
d13054c… drh 63 };
d13054c… drh 64
d0305b3… aku 65
d0305b3… aku 66 /*
bbcb632… aku 67 ** A parsed manifest or cluster.
dbda8d6… drh 68 */
dbda8d6… drh 69 struct Manifest {
dbda8d6… drh 70 Blob content; /* The original content blob */
7c2577b… drh 71 int type; /* Type of artifact. One of CFTYPE_xxxxx */
d13054c… drh 72 int rid; /* The blob-id for this manifest */
4cf8dbe… stephan 73 const char *zBaseline;/* Baseline manifest. The B card. */
d13054c… drh 74 Manifest *pBaseline; /* The actual baseline manifest */
7c2577b… drh 75 char *zComment; /* Decoded comment. The C card. */
7c2577b… drh 76 double rDate; /* Date and time from D card. 0.0 if no D card. */
7c2577b… drh 77 char *zUser; /* Name of the user from the U card. */
7c2577b… drh 78 char *zRepoCksum; /* MD5 checksum of the baseline content. R card. */
7c2577b… drh 79 char *zWiki; /* Text of the wiki page. W card. */
7c2577b… drh 80 char *zWikiTitle; /* Name of the wiki page. L card. */
4dcea80… drh 81 char *zMimetype; /* Mime type of wiki or comment text. N card. */
99fcc43… drh 82 char *zThreadTitle; /* The forum thread title. H card */
d96c4a4… drh 83 double rEventDate; /* Date of an event. E card. */
fd9b7bd… drh 84 char *zEventId; /* Artifact hash for an event. E card. */
7c2577b… drh 85 char *zTicketUuid; /* UUID for a ticket. K card. */
7c2577b… drh 86 char *zAttachName; /* Filename of an attachment. A card. */
fd9b7bd… drh 87 char *zAttachSrc; /* Artifact hash for document being attached. A card. */
7c2577b… drh 88 char *zAttachTarget; /* Ticket or wiki that attachment applies to. A card */
99fcc43… drh 89 char *zThreadRoot; /* Thread root artifact. G card */
99fcc43… drh 90 char *zInReplyTo; /* Forum in-reply-to artifact. I card */
7c2577b… drh 91 int nFile; /* Number of F cards */
dbda8d6… drh 92 int nFileAlloc; /* Slots allocated in aFile[] */
d13054c… drh 93 int iFile; /* Index of current file in iterator */
d13054c… drh 94 ManifestFile *aFile; /* One entry for each F-card */
7c2577b… drh 95 int nParent; /* Number of parents. */
dbda8d6… drh 96 int nParentAlloc; /* Slots allocated in azParent[] */
fd9b7bd… drh 97 char **azParent; /* Hashes of parents. One for each P card argument */
7fcbbb1… drh 98 int nCherrypick; /* Number of entries in aCherrypick[] */
52b35c8… jan.nijtmans 99 struct {
fd9b7bd… drh 100 char *zCPTarget; /* Hash for cherry-picked version w/ +|- prefix */
fd9b7bd… drh 101 char *zCPBase; /* Hash for cherry-pick baseline. NULL for singletons */
7fcbbb1… drh 102 } *aCherrypick;
bbcb632… aku 103 int nCChild; /* Number of cluster children */
bbcb632… aku 104 int nCChildAlloc; /* Number of closts allocated in azCChild[] */
fd9b7bd… drh 105 char **azCChild; /* Hashes of referenced objects in a cluster. M cards */
7c2577b… drh 106 int nTag; /* Number of T Cards */
d0305b3… aku 107 int nTagAlloc; /* Slots allocated in aTag[] */
5b1a685… jan.nijtmans 108 struct TagType {
d0305b3… aku 109 char *zName; /* Name of the tag */
fd9b7bd… drh 110 char *zUuid; /* Hash of artifact that the tag is applied to */
d0305b3… aku 111 char *zValue; /* Value if the tag is really a property */
7c2577b… drh 112 } *aTag; /* One for each T card */
7c2577b… drh 113 int nField; /* Number of J cards */
d0305b3… aku 114 int nFieldAlloc; /* Slots allocated in aField[] */
52b35c8… jan.nijtmans 115 struct {
d0305b3… aku 116 char *zName; /* Key or field name */
d0305b3… aku 117 char *zValue; /* Value of the field */
7c2577b… drh 118 } *aField; /* One for each J card */
dbda8d6… drh 119 };
dbda8d6… drh 120 #endif
fd9b7bd… drh 121
fd9b7bd… drh 122 /*
99fcc43… drh 123 ** Allowed and required card types in each style of artifact
99fcc43… drh 124 */
99fcc43… drh 125 static struct {
99fcc43… drh 126 const char *zAllowed; /* Allowed cards. Human-readable */
99fcc43… drh 127 const char *zRequired; /* Required cards. Human-readable */
99fcc43… drh 128 } manifestCardTypes[] = {
99fcc43… drh 129 /* Allowed Required */
b3ccc4b… drh 130 /* CFTYPE_MANIFEST 1 */ { "BCDFNPQRTUZ", "DZ" },
b3ccc4b… drh 131 /* Wants to be "CDUZ" ----^^^^
b3ccc4b… drh 132 ** but we must limit for historical compatibility */
99fcc43… drh 133 /* CFTYPE_CLUSTER 2 */ { "MZ", "MZ" },
99fcc43… drh 134 /* CFTYPE_CONTROL 3 */ { "DTUZ", "DTUZ" },
cad57db… drh 135 /* CFTYPE_WIKI 4 */ { "CDLNPUWZ", "DLUWZ" },
99fcc43… drh 136 /* CFTYPE_TICKET 5 */ { "DJKUZ", "DJKUZ" },
99fcc43… drh 137 /* CFTYPE_ATTACHMENT 6 */ { "ACDNUZ", "ADZ" },
99fcc43… drh 138 /* CFTYPE_EVENT 7 */ { "CDENPTUWZ", "DEWZ" },
99fcc43… drh 139 /* CFTYPE_FORUM 8 */ { "DGHINPUWZ", "DUWZ" },
99fcc43… drh 140 };
99fcc43… drh 141
99fcc43… drh 142 /*
99fcc43… drh 143 ** Names of manifest types
99fcc43… drh 144 */
70a94d0… jan.nijtmans 145 static const char *const azNameOfMType[] = {
99fcc43… drh 146 "manifest",
99fcc43… drh 147 "cluster",
99fcc43… drh 148 "tag",
99fcc43… drh 149 "wiki",
99fcc43… drh 150 "ticket",
99fcc43… drh 151 "attachment",
99fcc43… drh 152 "technote",
99fcc43… drh 153 "forum post"
99fcc43… drh 154 };
99fcc43… drh 155
99fcc43… drh 156 /*
0a55d16… drh 157 ** A cache of parsed manifests. This reduces the number of
0a55d16… drh 158 ** calls to manifest_parse() when doing a rebuild.
0a55d16… drh 159 */
d13054c… drh 160 #define MX_MANIFEST_CACHE 6
0a55d16… drh 161 static struct {
0a55d16… drh 162 int nxAge;
0a55d16… drh 163 int aAge[MX_MANIFEST_CACHE];
d13054c… drh 164 Manifest *apManifest[MX_MANIFEST_CACHE];
0a55d16… drh 165 } manifestCache;
0a55d16… drh 166
80c42a3… drh 167 /*
80c42a3… drh 168 ** True if manifest_crosslink_begin() has been called but
80c42a3… drh 169 ** manifest_crosslink_end() is still pending.
80c42a3… drh 170 */
80c42a3… drh 171 static int manifest_crosslink_busy = 0;
4cf8dbe… stephan 172
4cf8dbe… stephan 173 /*
169ba8d… drh 174 ** There are some triggers that need to fire whenever new content
169ba8d… drh 175 ** is added to the EVENT table, to make corresponding changes to the
169ba8d… drh 176 ** PENDING_ALERT and CHAT tables. These are done with TEMP triggers
169ba8d… drh 177 ** which are created as needed. The reasons for using TEMP triggers:
169ba8d… drh 178 **
169ba8d… drh 179 ** * A small minority of invocations of Fossil need to use those triggers.
169ba8d… drh 180 ** So we save CPU cycles in the common case by not having to parse the
169ba8d… drh 181 ** trigger definition
169ba8d… drh 182 **
169ba8d… drh 183 ** * We don't have to worry about dangling table references inside
169ba8d… drh 184 ** of triggers. For example, we can create a trigger that adds
169ba8d… drh 185 ** to the CHAT table. But an admin can still drop that CHAT table
169ba8d… drh 186 ** at any moment, since the trigger that refers to CHAT is a TEMP
169ba8d… drh 187 ** trigger and won't persist to cause problems.
169ba8d… drh 188 **
169ba8d… drh 189 ** * Because TEMP triggers are defined by the specific version of the
169ba8d… drh 190 ** application that is running, we don't have to worry with legacy
169ba8d… drh 191 ** compatibility of the triggers.
169ba8d… drh 192 **
169ba8d… drh 193 ** This boolean variable is set when the TEMP triggers for EVENT
169ba8d… drh 194 ** have been created.
169ba8d… drh 195 */
169ba8d… drh 196 static int manifest_event_triggers_are_enabled = 0;
169ba8d… drh 197
169ba8d… drh 198 /*
dbda8d6… drh 199 ** Clear the memory allocated in a manifest object
dbda8d6… drh 200 */
d13054c… drh 201 void manifest_destroy(Manifest *p){
d13054c… drh 202 if( p ){
d13054c… drh 203 blob_reset(&p->content);
b7ff537… stephan 204 fossil_free(p->aFile);
b7ff537… stephan 205 fossil_free(p->azParent);
b7ff537… stephan 206 fossil_free(p->azCChild);
b7ff537… stephan 207 fossil_free(p->aTag);
b7ff537… stephan 208 fossil_free(p->aField);
b7ff537… stephan 209 fossil_free(p->aCherrypick);
d13054c… drh 210 if( p->pBaseline ) manifest_destroy(p->pBaseline);
d22519e… drh 211 memset(p, 0, sizeof(*p));
d13054c… drh 212 fossil_free(p);
d13054c… drh 213 }
99fcc43… drh 214 }
99fcc43… drh 215
99fcc43… drh 216 /*
99fcc43… drh 217 ** Given a string of upper-case letters, compute a mask of the letters
99fcc43… drh 218 ** present. For example, "ABC" computes 0x0007. "DE" gives 0x0018".
99fcc43… drh 219 */
99fcc43… drh 220 static unsigned int manifest_card_mask(const char *z){
99fcc43… drh 221 unsigned int m = 0;
99fcc43… drh 222 char c;
99fcc43… drh 223 while( (c = *(z++))>='A' && c<='Z' ){
99fcc43… drh 224 m |= 1 << (c - 'A');
99fcc43… drh 225 }
99fcc43… drh 226 return m;
99fcc43… drh 227 }
99fcc43… drh 228
99fcc43… drh 229 /*
99fcc43… drh 230 ** Given an integer mask representing letters A-Z, return the
99fcc43… drh 231 ** letter which is the first bit set in the mask. Example:
99fcc43… drh 232 ** 0x03520 gives 'F' since the F-bit is the lowest.
99fcc43… drh 233 */
99fcc43… drh 234 static char maskToType(unsigned int x){
99fcc43… drh 235 char c = 'A';
99fcc43… drh 236 if( x==0 ) return '?';
99fcc43… drh 237 while( (x&1)==0 ){ x >>= 1; c++; }
99fcc43… drh 238 return c;
0a55d16… drh 239 }
0a55d16… drh 240
0a55d16… drh 241 /*
0a55d16… drh 242 ** Add an element to the manifest cache using LRU replacement.
0a55d16… drh 243 */
d13054c… drh 244 void manifest_cache_insert(Manifest *p){
d13054c… drh 245 while( p ){
d13054c… drh 246 int i;
d13054c… drh 247 Manifest *pBaseline = p->pBaseline;
d13054c… drh 248 p->pBaseline = 0;
d13054c… drh 249 for(i=0; i<MX_MANIFEST_CACHE; i++){
d13054c… drh 250 if( manifestCache.apManifest[i]==0 ) break;
d13054c… drh 251 }
d13054c… drh 252 if( i>=MX_MANIFEST_CACHE ){
d13054c… drh 253 int oldest = 0;
d13054c… drh 254 int oldestAge = manifestCache.aAge[0];
d13054c… drh 255 for(i=1; i<MX_MANIFEST_CACHE; i++){
d13054c… drh 256 if( manifestCache.aAge[i]<oldestAge ){
d13054c… drh 257 oldest = i;
d13054c… drh 258 oldestAge = manifestCache.aAge[i];
d13054c… drh 259 }
d13054c… drh 260 }
d13054c… drh 261 manifest_destroy(manifestCache.apManifest[oldest]);
d13054c… drh 262 i = oldest;
d13054c… drh 263 }
d13054c… drh 264 manifestCache.aAge[i] = ++manifestCache.nxAge;
d13054c… drh 265 manifestCache.apManifest[i] = p;
d13054c… drh 266 p = pBaseline;
d13054c… drh 267 }
0a55d16… drh 268 }
0a55d16… drh 269
0a55d16… drh 270 /*
0a55d16… drh 271 ** Try to extract a line from the manifest cache. Return 1 if found.
0a55d16… drh 272 ** Return 0 if not found.
0a55d16… drh 273 */
d13054c… drh 274 static Manifest *manifest_cache_find(int rid){
0a55d16… drh 275 int i;
d13054c… drh 276 Manifest *p;
0a55d16… drh 277 for(i=0; i<MX_MANIFEST_CACHE; i++){
d13054c… drh 278 if( manifestCache.apManifest[i] && manifestCache.apManifest[i]->rid==rid ){
d13054c… drh 279 p = manifestCache.apManifest[i];
d13054c… drh 280 manifestCache.apManifest[i] = 0;
d13054c… drh 281 return p;
0a55d16… drh 282 }
0a55d16… drh 283 }
0a55d16… drh 284 return 0;
0a55d16… drh 285 }
0a55d16… drh 286
0a55d16… drh 287 /*
0a55d16… drh 288 ** Clear the manifest cache.
0a55d16… drh 289 */
0a55d16… drh 290 void manifest_cache_clear(void){
0a55d16… drh 291 int i;
0a55d16… drh 292 for(i=0; i<MX_MANIFEST_CACHE; i++){
d13054c… drh 293 if( manifestCache.apManifest[i] ){
d13054c… drh 294 manifest_destroy(manifestCache.apManifest[i]);
0a55d16… drh 295 }
0a55d16… drh 296 }
0a55d16… drh 297 memset(&manifestCache, 0, sizeof(manifestCache));
7129882… drh 298 }
7129882… drh 299
7129882… drh 300 #ifdef FOSSIL_DONT_VERIFY_MANIFEST_MD5SUM
7129882… drh 301 # define md5sum_init(X)
7129882… drh 302 # define md5sum_step_text(X,Y)
7129882… drh 303 #endif
7129882… drh 304
7129882… drh 305 /*
696668a… drh 306 ** Return true if z points to the first character after a blank line.
696668a… drh 307 ** Tolerate either \r\n or \n line endings.
696668a… drh 308 */
696668a… drh 309 static int after_blank_line(const char *z){
696668a… drh 310 if( z[-1]!='\n' ) return 0;
696668a… drh 311 if( z[-2]=='\n' ) return 1;
696668a… drh 312 if( z[-2]=='\r' && z[-3]=='\n' ) return 1;
696668a… drh 313 return 0;
696668a… drh 314 }
696668a… drh 315
696668a… drh 316 /*
d13054c… drh 317 ** Remove the PGP signature from the artifact, if there is one.
d13054c… drh 318 */
0f7eff9… drh 319 static void remove_pgp_signature(const char **pz, int *pn){
0f7eff9… drh 320 const char *z = *pz;
d13054c… drh 321 int n = *pn;
d13054c… drh 322 int i;
0f0184e… danield 323 if( strncmp(z, "-----BEGIN PGP SIGNED MESSAGE-----", 34)==0 ) i = 34;
0f0184e… danield 324 else if( strncmp(z, "-----BEGIN SSH SIGNED MESSAGE-----", 34)==0 ) i = 34;
0f0184e… danield 325 else return;
0f0184e… danield 326 for(; i<n && !after_blank_line(z+i); i++){}
d13054c… drh 327 if( i>=n ) return;
d13054c… drh 328 z += i;
d13054c… drh 329 n -= i;
d13054c… drh 330 *pz = z;
d13054c… drh 331 for(i=n-1; i>=0; i--){
0f0184e… danield 332 if( z[i]=='\n' &&
0f0184e… danield 333 (strncmp(&z[i],"\n-----BEGIN PGP SIGNATURE-----", 29)==0
0f0184e… danield 334 || strncmp(&z[i],"\n-----BEGIN SSH SIGNATURE-----", 29)==0 )){
d13054c… drh 335 n = i+1;
d13054c… drh 336 break;
d13054c… drh 337 }
d13054c… drh 338 }
d13054c… drh 339 *pn = n;
d13054c… drh 340 return;
d13054c… drh 341 }
d13054c… drh 342
d13054c… drh 343 /*
d13054c… drh 344 ** Verify the Z-card checksum on the artifact, if there is such a
d13054c… drh 345 ** checksum. Return 0 if there is no Z-card. Return 1 if the Z-card
d13054c… drh 346 ** exists and is correct. Return 2 if the Z-card exists and has the wrong
d13054c… drh 347 ** value.
d13054c… drh 348 **
52b35c8… jan.nijtmans 349 ** 0123456789 123456789 123456789 123456789
d13054c… drh 350 ** Z aea84f4f863865a8d59d0384e4d2a41c
d13054c… drh 351 */
cad57db… drh 352 static int verify_z_card(const char *z, int n, Blob *pErr){
cad57db… drh 353 const char *zHash;
d13054c… drh 354 if( n<35 ) return 0;
d13054c… drh 355 if( z[n-35]!='Z' || z[n-34]!=' ' ) return 0;
d13054c… drh 356 md5sum_init();
d13054c… drh 357 md5sum_step_text(z, n-35);
cad57db… drh 358 zHash = md5sum_finish(0);
cad57db… drh 359 if( memcmp(&z[n-33], zHash, 32)==0 ){
d13054c… drh 360 return 1;
d13054c… drh 361 }else{
992b62f… stephan 362 if(pErr!=0){
992b62f… stephan 363 blob_appendf(pErr, "incorrect Z-card cksum: expected %.32s", zHash);
992b62f… stephan 364 }
d13054c… drh 365 return 2;
d13054c… drh 366 }
d13054c… drh 367 }
d13054c… drh 368
d13054c… drh 369 /*
d13054c… drh 370 ** A structure used for rapid parsing of the Manifest file
d13054c… drh 371 */
d13054c… drh 372 typedef struct ManifestText ManifestText;
d13054c… drh 373 struct ManifestText {
d13054c… drh 374 char *z; /* The first character of the next token */
d13054c… drh 375 char *zEnd; /* One character beyond the end of the manifest */
d13054c… drh 376 int atEol; /* True if z points to the start of a new line */
d13054c… drh 377 };
d13054c… drh 378
d13054c… drh 379 /*
d13054c… drh 380 ** Return a pointer to the next token. The token is zero-terminated.
d13054c… drh 381 ** Return NULL if there are no more tokens on the current line.
d13054c… drh 382 */
d13054c… drh 383 static char *next_token(ManifestText *p, int *pLen){
d13054c… drh 384 char *zStart;
3d802ec… drh 385 int n;
d13054c… drh 386 if( p->atEol ) return 0;
3d802ec… drh 387 zStart = p->z;
3d802ec… drh 388 n = strcspn(p->z, " \n");
3d802ec… drh 389 p->atEol = p->z[n]=='\n';
3d802ec… drh 390 p->z[n] = 0;
3d802ec… drh 391 p->z += n+1;
3d802ec… drh 392 if( pLen ) *pLen = n;
d13054c… drh 393 return zStart;
d13054c… drh 394 }
d13054c… drh 395
d13054c… drh 396 /*
d13054c… drh 397 ** Return the card-type for the next card. Or, return 0 if there are no
d13054c… drh 398 ** more cards or if we are not at the end of the current card.
d13054c… drh 399 */
d13054c… drh 400 static char next_card(ManifestText *p){
d13054c… drh 401 char c;
d13054c… drh 402 if( !p->atEol || p->z>=p->zEnd ) return 0;
d13054c… drh 403 c = p->z[0];
d13054c… drh 404 if( p->z[1]==' ' ){
d13054c… drh 405 p->z += 2;
d13054c… drh 406 p->atEol = 0;
d13054c… drh 407 }else if( p->z[1]=='\n' ){
d13054c… drh 408 p->z += 2;
d13054c… drh 409 p->atEol = 1;
d13054c… drh 410 }else{
d13054c… drh 411 c = 0;
d13054c… drh 412 }
d13054c… drh 413 return c;
d13054c… drh 414 }
d13054c… drh 415
d13054c… drh 416 /*
aab9e66… drh 417 ** Shorthand for a control-artifact parsing error
aab9e66… drh 418 */
aab9e66… drh 419 #define SYNTAX(T) {zErr=(T); goto manifest_syntax_error;}
3d802ec… drh 420
3d802ec… drh 421 /*
4cf8dbe… stephan 422 ** A cache of manifest IDs which manifest_parse() has seen in this
4cf8dbe… stephan 423 ** session.
4cf8dbe… stephan 424 */
4cf8dbe… stephan 425 static Bag seenManifests = Bag_INIT;
4cf8dbe… stephan 426 /*
4cf8dbe… stephan 427 ** Frees all memory owned by the manifest "has-seen" cache. Intended
4cf8dbe… stephan 428 ** to be called only from the app's atexit() handler.
4cf8dbe… stephan 429 */
4cf8dbe… stephan 430 void manifest_clear_cache(){
4cf8dbe… stephan 431 bag_clear(&seenManifests);
4cf8dbe… stephan 432 }
4cf8dbe… stephan 433
4cf8dbe… stephan 434 /*
d0305b3… aku 435 ** Parse a blob into a Manifest object. The Manifest object
d0305b3… aku 436 ** takes over the input blob and will free it when the
dbda8d6… drh 437 ** Manifest object is freed. Zeros are inserted into the blob
dbda8d6… drh 438 ** as string terminators so that blob should not be used again.
dbda8d6… drh 439 **
ba418ee… drh 440 ** Return a pointer to an allocated Manifest object if the content
b098c93… jan.nijtmans 441 ** really is a structural artifact of some kind. The returned Manifest
fd9b7bd… drh 442 ** object needs to be freed by a subsequent call to manifest_destroy().
fd9b7bd… drh 443 ** Return NULL if there are syntax errors or if the input blob does
fd9b7bd… drh 444 ** not describe a valid structural artifact.
d0305b3… aku 445 **
fd9b7bd… drh 446 ** This routine is strict about the format of a structural artifacts.
d0305b3… aku 447 ** The format must match exactly or else it is rejected. This
fd9b7bd… drh 448 ** rule minimizes the risk that a content artifact will be mistaken
fd9b7bd… drh 449 ** for a structural artifact simply because they look the same.
dbda8d6… drh 450 **
ba418ee… drh 451 ** The pContent is reset. If a pointer is returned, then pContent will
ba418ee… drh 452 ** be reset when the Manifest object is cleared. If NULL is
dbda8d6… drh 453 ** returned then the Manifest object is cleared automatically
dbda8d6… drh 454 ** and pContent is reset before the return.
d0305b3… aku 455 **
fd9b7bd… drh 456 ** The entire input blob can be PGP clear-signed. The signature is ignored.
fd9b7bd… drh 457 ** The artifact consists of zero or more cards, one card per line.
d0305b3… aku 458 ** (Except: the content of the W card can extend of multiple lines.)
d0305b3… aku 459 ** Each card is divided into tokens by a single space character.
d0305b3… aku 460 ** The first token is a single upper-case letter which is the card type.
d0305b3… aku 461 ** The card type determines the other parameters to the card.
d0305b3… aku 462 ** Cards must occur in lexicographical order.
dbda8d6… drh 463 */
aab9e66… drh 464 Manifest *manifest_parse(Blob *pContent, int rid, Blob *pErr){
d13054c… drh 465 Manifest *p;
d0305b3… aku 466 int i, lineNo=0;
d13054c… drh 467 ManifestText x;
d0305b3… aku 468 char cPrevType = 0;
d13054c… drh 469 char cType;
d13054c… drh 470 char *z;
d13054c… drh 471 int n;
d13054c… drh 472 char *zUuid;
d03718a… drh 473 int sz = 0;
99fcc43… drh 474 int isRepeat;
99fcc43… drh 475 int nSelfTag = 0; /* Number of T cards referring to this manifest */
99fcc43… drh 476 int nSimpleTag = 0; /* Number of T cards with "+" prefix */
aab9e66… drh 477 const char *zErr = 0;
99fcc43… drh 478 unsigned int m;
99fcc43… drh 479 unsigned int seenCard = 0; /* Which card types have been seen */
99fcc43… drh 480 char zErrBuf[100]; /* Write error messages here */
dbda8d6… drh 481
cd4e088… drh 482 if( rid==0 ){
cd4e088… drh 483 isRepeat = 1;
4cf8dbe… stephan 484 }else if( bag_find(&seenManifests, rid) ){
f25e5e5… drh 485 isRepeat = 1;
f25e5e5… drh 486 }else{
f25e5e5… drh 487 isRepeat = 0;
4cf8dbe… stephan 488 bag_insert(&seenManifests, rid);
f25e5e5… drh 489 }
f25e5e5… drh 490
fd9b7bd… drh 491 /* Every structural artifact ends with a '\n' character. Exit early
d13054c… drh 492 ** if that is not the case for this artifact.
d13054c… drh 493 */
f25e5e5… drh 494 if( !isRepeat ) g.parseCnt[0]++;
0538980… drh 495 z = blob_materialize(pContent);
d13054c… drh 496 n = blob_size(pContent);
6013aea… drh 497 if( n<=0 || z[n-1]!='\n' ){
d13054c… drh 498 blob_reset(pContent);
992b62f… stephan 499 if(pErr!=0){
992b62f… stephan 500 blob_appendf(pErr, "%s", n ? "not terminated with \\n" : "zero-length");
992b62f… stephan 501 }
d13054c… drh 502 return 0;
d13054c… drh 503 }
d13054c… drh 504
960fb04… jan.nijtmans 505 /* Strip off the PGP signature if there is one.
d13054c… drh 506 */
0f7eff9… drh 507 remove_pgp_signature((const char**)&z, &n);
d13054c… drh 508
d13054c… drh 509 /* Verify that the first few characters of the artifact look like
d13054c… drh 510 ** a control artifact.
d13054c… drh 511 */
d13054c… drh 512 if( n<10 || z[0]<'A' || z[0]>'Z' || z[1]!=' ' ){
d13054c… drh 513 blob_reset(pContent);
65dbc19… stephan 514 if(pErr!=0){
65dbc19… stephan 515 blob_appendf(pErr, "line 1 not recognized");
65dbc19… stephan 516 }
960fb04… jan.nijtmans 517 return 0;
960fb04… jan.nijtmans 518 }
960fb04… jan.nijtmans 519 /* Then verify the Z-card.
960fb04… jan.nijtmans 520 */
f741baa… drh 521 #if 1
f741baa… drh 522 /* Disable this ***ONLY*** (ONLY!) when testing hand-written inputs
f741baa… drh 523 for card-related syntax errors. */
cad57db… drh 524 if( verify_z_card(z, n, pErr)==2 ){
960fb04… jan.nijtmans 525 blob_reset(pContent);
7954ccb… drh 526 return 0;
7954ccb… drh 527 }
f741baa… drh 528 #else
f741baa… drh 529 #warning ACHTUNG - z-card check is disabled for testing purposes.
f741baa… drh 530 if(0 && verify_z_card(NULL, 0, NULL)){
f741baa… drh 531 /*avoid unused static func error*/
f741baa… drh 532 }
f741baa… drh 533 #endif
7954ccb… drh 534
d13054c… drh 535 /* Allocate a Manifest object to hold the parsed control artifact.
d13054c… drh 536 */
d13054c… drh 537 p = fossil_malloc( sizeof(*p) );
dbda8d6… drh 538 memset(p, 0, sizeof(*p));
dbda8d6… drh 539 memcpy(&p->content, pContent, sizeof(p->content));
d13054c… drh 540 p->rid = rid;
dbda8d6… drh 541 blob_zero(pContent);
dbda8d6… drh 542 pContent = &p->content;
dbda8d6… drh 543
d13054c… drh 544 /* Begin parsing, card by card.
d13054c… drh 545 */
d13054c… drh 546 x.z = z;
d13054c… drh 547 x.zEnd = &z[n];
d13054c… drh 548 x.atEol = 1;
023fdde… drh 549 while( (cType = next_card(&x))!=0 ){
023fdde… drh 550 if( cType<cPrevType ){
023fdde… drh 551 /* Cards must be in increasing order. However, out-of-order detection
023fdde… drh 552 ** was broken prior to 2021-02-10 due to a bug. Furthermore, there
023fdde… drh 553 ** was a bug in technote generation (prior to 2021-02-10) that caused
023fdde… drh 554 ** the P card to occur before the N card. Hence, for historical
023fdde… drh 555 ** compatibility, we do allow the N card of a technote to occur after
023fdde… drh 556 ** the P card. See tickets 15d04de574383d61 and 5e67a7f4041a36ad.
023fdde… drh 557 */
023fdde… drh 558 if( cType!='N' || cPrevType!='P' || p->zEventId==0 ){
023fdde… drh 559 SYNTAX("cards not in lexicographical order");
023fdde… drh 560 }
023fdde… drh 561 }
d0305b3… aku 562 lineNo++;
99fcc43… drh 563 if( cType<'A' || cType>'Z' ) SYNTAX("bad card type");
99fcc43… drh 564 seenCard |= 1 << (cType-'A');
023fdde… drh 565 cPrevType = cType;
d13054c… drh 566 switch( cType ){
d0305b3… aku 567 /*
7c2577b… drh 568 ** A <filename> <target> ?<source>?
d0305b3… aku 569 **
d0305b3… aku 570 ** Identifies an attachment to either a wiki page or a ticket.
7c2577b… drh 571 ** <source> is the artifact that is the attachment. <source>
7c2577b… drh 572 ** is omitted to delete an attachment. <target> is the name of
7c2577b… drh 573 ** a wiki page or ticket to which that attachment is connected.
d0305b3… aku 574 */
d0305b3… aku 575 case 'A': {
7c2577b… drh 576 char *zName, *zTarget, *zSrc;
d03718a… drh 577 int nTarget = 0, nSrc = 0;
d13054c… drh 578 zName = next_token(&x, 0);
d13054c… drh 579 zTarget = next_token(&x, &nTarget);
d13054c… drh 580 zSrc = next_token(&x, &nSrc);
52b35c8… jan.nijtmans 581 if( zName==0 || zTarget==0 ) goto manifest_syntax_error;
7c2577b… drh 582 if( p->zAttachName!=0 ) goto manifest_syntax_error;
d0305b3… aku 583 defossilize(zName);
b4aadf2… drh 584 if( !file_is_simple_pathname_nonstrict(zName) ){
aab9e66… drh 585 SYNTAX("invalid filename on A-card");
7c2577b… drh 586 }
7c2577b… drh 587 defossilize(zTarget);
fd9b7bd… drh 588 if( !hname_validate(zTarget,nTarget)
7c2577b… drh 589 && !wiki_name_is_wellformed((const unsigned char *)zTarget) ){
aab9e66… drh 590 SYNTAX("invalid target on A-card");
7c2577b… drh 591 }
fd9b7bd… drh 592 if( zSrc && !hname_validate(zSrc,nSrc) ){
aab9e66… drh 593 SYNTAX("invalid source on A-card");
7c2577b… drh 594 }
7c2577b… drh 595 p->zAttachName = (char*)file_tail(zName);
7c2577b… drh 596 p->zAttachSrc = zSrc;
7c2577b… drh 597 p->zAttachTarget = zTarget;
99fcc43… drh 598 p->type = CFTYPE_ATTACHMENT;
d13054c… drh 599 break;
d13054c… drh 600 }
d13054c… drh 601
d13054c… drh 602 /*
d13054c… drh 603 ** B <uuid>
d13054c… drh 604 **
fd9b7bd… drh 605 ** A B-line gives the artifact hash for the baseline of a delta-manifest.
d13054c… drh 606 */
d13054c… drh 607 case 'B': {
aab9e66… drh 608 if( p->zBaseline ) SYNTAX("more than one B-card");
d13054c… drh 609 p->zBaseline = next_token(&x, &sz);
fd9b7bd… drh 610 if( p->zBaseline==0 ) SYNTAX("missing hash on B-card");
fd9b7bd… drh 611 if( !hname_validate(p->zBaseline,sz) ){
fd9b7bd… drh 612 SYNTAX("invalid hash on B-card");
aab9e66… drh 613 }
99fcc43… drh 614 p->type = CFTYPE_MANIFEST;
d0305b3… aku 615 break;
d0305b3… aku 616 }
d13054c… drh 617
d0305b3… aku 618
d0305b3… aku 619 /*
d0305b3… aku 620 ** C <comment>
d0305b3… aku 621 **
d0305b3… aku 622 ** Comment text is fossil-encoded. There may be no more than
b5f2ebd… stephan 623 ** one C line. C lines are required for manifests, are optional
b5f2ebd… stephan 624 ** for Events and Attachments, and are disallowed on all other
b5f2ebd… stephan 625 ** control files.
d0305b3… aku 626 */
d0305b3… aku 627 case 'C': {
aab9e66… drh 628 if( p->zComment!=0 ) SYNTAX("more than one C-card");
d13054c… drh 629 p->zComment = next_token(&x, 0);
aab9e66… drh 630 if( p->zComment==0 ) SYNTAX("missing comment text on C-card");
d0305b3… aku 631 defossilize(p->zComment);
d0305b3… aku 632 break;
d0305b3… aku 633 }
d0305b3… aku 634
d0305b3… aku 635 /*
d0305b3… aku 636 ** D <timestamp>
d0305b3… aku 637 **
d0305b3… aku 638 ** The timestamp should be ISO 8601. YYYY-MM-DDtHH:MM:SS
d0305b3… aku 639 ** There can be no more than 1 D line. D lines are required
d0305b3… aku 640 ** for all control files except for clusters.
d0305b3… aku 641 */
d0305b3… aku 642 case 'D': {
aab9e66… drh 643 if( p->rDate>0.0 ) SYNTAX("more than one D-card");
d13054c… drh 644 p->rDate = db_double(0.0, "SELECT julianday(%Q)", next_token(&x,0));
aab9e66… drh 645 if( p->rDate<=0.0 ) SYNTAX("cannot parse date on D-card");
d96c4a4… drh 646 break;
d96c4a4… drh 647 }
d96c4a4… drh 648
d96c4a4… drh 649 /*
d96c4a4… drh 650 ** E <timestamp> <uuid>
d96c4a4… drh 651 **
52b35c8… jan.nijtmans 652 ** An "event" card that contains the timestamp of the event in the
d96c4a4… drh 653 ** format YYYY-MM-DDtHH:MM:SS and a unique identifier for the event.
d96c4a4… drh 654 ** The event timestamp is distinct from the D timestamp. The D
d96c4a4… drh 655 ** timestamp is when the artifact was created whereas the E timestamp
d96c4a4… drh 656 ** is when the specific event is said to occur.
d96c4a4… drh 657 */
d96c4a4… drh 658 case 'E': {
aab9e66… drh 659 if( p->rEventDate>0.0 ) SYNTAX("more than one E-card");
d13054c… drh 660 p->rEventDate = db_double(0.0,"SELECT julianday(%Q)", next_token(&x,0));
aab9e66… drh 661 if( p->rEventDate<=0.0 ) SYNTAX("malformed date on E-card");
d13054c… drh 662 p->zEventId = next_token(&x, &sz);
f741baa… drh 663 if( p->zEventId==0 ) SYNTAX("missing hash on E-card");
fd9b7bd… drh 664 if( !hname_validate(p->zEventId, sz) ){
fd9b7bd… drh 665 SYNTAX("malformed hash on E-card");
aab9e66… drh 666 }
99fcc43… drh 667 p->type = CFTYPE_EVENT;
d0305b3… aku 668 break;
d0305b3… aku 669 }
d0305b3… aku 670
d0305b3… aku 671 /*
d13054c… drh 672 ** F <filename> ?<uuid>? ?<permissions>? ?<old-name>?
d0305b3… aku 673 **
d0305b3… aku 674 ** Identifies a file in a manifest. Multiple F lines are
d0305b3… aku 675 ** allowed in a manifest. F lines are not allowed in any
2bde9f9… drh 676 ** other control file. The filename and old-name are fossil-encoded.
d0305b3… aku 677 */
d0305b3… aku 678 case 'F': {
d13054c… drh 679 char *zName, *zPerm, *zPriorName;
d13054c… drh 680 zName = next_token(&x,0);
aab9e66… drh 681 if( zName==0 ) SYNTAX("missing filename on F-card");
d0305b3… aku 682 defossilize(zName);
b4aadf2… drh 683 if( !file_is_simple_pathname_nonstrict(zName) ){
aab9e66… drh 684 SYNTAX("F-card filename is not a simple path");
d13054c… drh 685 }
d13054c… drh 686 zUuid = next_token(&x, &sz);
d13054c… drh 687 if( p->zBaseline==0 || zUuid!=0 ){
f741baa… drh 688 if( zUuid==0 ) SYNTAX("missing hash on F-card");
fd9b7bd… drh 689 if( !hname_validate(zUuid,sz) ){
fd9b7bd… drh 690 SYNTAX("F-card hash invalid");
fd9b7bd… drh 691 }
2bde9f9… drh 692 }
d13054c… drh 693 zPerm = next_token(&x,0);
d13054c… drh 694 zPriorName = next_token(&x,0);
d13054c… drh 695 if( zPriorName ){
2bde9f9… drh 696 defossilize(zPriorName);
b4aadf2… drh 697 if( !file_is_simple_pathname_nonstrict(zPriorName) ){
aab9e66… drh 698 SYNTAX("F-card old filename is not a simple path");
2bde9f9… drh 699 }
d0305b3… aku 700 }
d0305b3… aku 701 if( p->nFile>=p->nFileAlloc ){
d0305b3… aku 702 p->nFileAlloc = p->nFileAlloc*2 + 10;
52b35c8… jan.nijtmans 703 p->aFile = fossil_realloc(p->aFile,
8f41b2f… drh 704 p->nFileAlloc*sizeof(p->aFile[0]) );
d0305b3… aku 705 }
d0305b3… aku 706 i = p->nFile++;
f741baa… drh 707 if( i>0 && fossil_strcmp(p->aFile[i-1].zName, zName)>=0 ){
f741baa… drh 708 SYNTAX("incorrect F-card sort order");
f741baa… drh 709 }
f741baa… drh 710 if( file_is_reserved_name(zName,-1) ){
f741baa… drh 711 /* If reserved names leaked into historical manifests due to
f741baa… drh 712 ** slack oversight by older versions of Fossil, simply ignore
f741baa… drh 713 ** those files */
f741baa… drh 714 p->nFile--;
f741baa… drh 715 break;
f741baa… drh 716 }
d0305b3… aku 717 p->aFile[i].zName = zName;
d0305b3… aku 718 p->aFile[i].zUuid = zUuid;
588bb7c… aku 719 p->aFile[i].zPerm = zPerm;
9fd8009… drh 720 p->aFile[i].zPrior = zPriorName;
99fcc43… drh 721 p->type = CFTYPE_MANIFEST;
99fcc43… drh 722 break;
99fcc43… drh 723 }
99fcc43… drh 724
99fcc43… drh 725 /*
99fcc43… drh 726 ** G <hash>
99fcc43… drh 727 **
99fcc43… drh 728 ** A G-card identifies the initial root forum post for the thread
99fcc43… drh 729 ** of which this post is a part. Forum posts only.
99fcc43… drh 730 */
99fcc43… drh 731 case 'G': {
99fcc43… drh 732 if( p->zThreadRoot!=0 ) SYNTAX("more than one G-card");
99fcc43… drh 733 p->zThreadRoot = next_token(&x, &sz);
99fcc43… drh 734 if( p->zThreadRoot==0 ) SYNTAX("missing hash on G-card");
99fcc43… drh 735 if( !hname_validate(p->zThreadRoot,sz) ){
99fcc43… drh 736 SYNTAX("Invalid hash on G-card");
99fcc43… drh 737 }
99fcc43… drh 738 p->type = CFTYPE_FORUM;
99fcc43… drh 739 break;
99fcc43… drh 740 }
99fcc43… drh 741
99fcc43… drh 742 /*
99fcc43… drh 743 ** H <threadtitle>
99fcc43… drh 744 **
99fcc43… drh 745 ** The title for a forum thread.
99fcc43… drh 746 */
99fcc43… drh 747 case 'H': {
99fcc43… drh 748 if( p->zThreadTitle!=0 ) SYNTAX("more than one H-card");
99fcc43… drh 749 p->zThreadTitle = next_token(&x,0);
99fcc43… drh 750 if( p->zThreadTitle==0 ) SYNTAX("missing title on H-card");
99fcc43… drh 751 defossilize(p->zThreadTitle);
99fcc43… drh 752 p->type = CFTYPE_FORUM;
99fcc43… drh 753 break;
99fcc43… drh 754 }
99fcc43… drh 755
99fcc43… drh 756 /*
99fcc43… drh 757 ** I <hash>
99fcc43… drh 758 **
99fcc43… drh 759 ** A I-card identifies another forum post that the current forum post
99fcc43… drh 760 ** is in reply to.
99fcc43… drh 761 */
99fcc43… drh 762 case 'I': {
99fcc43… drh 763 if( p->zInReplyTo!=0 ) SYNTAX("more than one I-card");
99fcc43… drh 764 p->zInReplyTo = next_token(&x, &sz);
99fcc43… drh 765 if( p->zInReplyTo==0 ) SYNTAX("missing hash on I-card");
99fcc43… drh 766 if( !hname_validate(p->zInReplyTo,sz) ){
99fcc43… drh 767 SYNTAX("Invalid hash on I-card");
d0305b3… aku 768 }
99fcc43… drh 769 p->type = CFTYPE_FORUM;
d0305b3… aku 770 break;
d0305b3… aku 771 }
d0305b3… aku 772
d0305b3… aku 773 /*
31e94c0… drh 774 ** J <name> ?<value>?
d0305b3… aku 775 **
d0305b3… aku 776 ** Specifies a name value pair for ticket. If the first character
31e94c0… drh 777 ** of <name> is "+" then the <value> is appended to any preexisting
31e94c0… drh 778 ** value. If <value> is omitted then it is understood to be an
31e94c0… drh 779 ** empty string.
d0305b3… aku 780 */
d0305b3… aku 781 case 'J': {
d0305b3… aku 782 char *zName, *zValue;
d13054c… drh 783 zName = next_token(&x,0);
d13054c… drh 784 zValue = next_token(&x,0);
aab9e66… drh 785 if( zName==0 ) SYNTAX("name missing from J-card");
d13054c… drh 786 if( zValue==0 ) zValue = "";
d0305b3… aku 787 defossilize(zValue);
d0305b3… aku 788 if( p->nField>=p->nFieldAlloc ){
d0305b3… aku 789 p->nFieldAlloc = p->nFieldAlloc*2 + 10;
8f41b2f… drh 790 p->aField = fossil_realloc(p->aField,
d0305b3… aku 791 p->nFieldAlloc*sizeof(p->aField[0]) );
d0305b3… aku 792 }
d0305b3… aku 793 i = p->nField++;
d0305b3… aku 794 p->aField[i].zName = zName;
d0305b3… aku 795 p->aField[i].zValue = zValue;
31c52c7… drh 796 if( i>0 && fossil_strcmp(p->aField[i-1].zName, zName)>=0 ){
aab9e66… drh 797 SYNTAX("incorrect J-card sort order");
d0305b3… aku 798 }
99fcc43… drh 799 p->type = CFTYPE_TICKET;
d0305b3… aku 800 break;
d0305b3… aku 801 }
d0305b3… aku 802
d0305b3… aku 803
d0305b3… aku 804 /*
d0305b3… aku 805 ** K <uuid>
d0305b3… aku 806 **
d0305b3… aku 807 ** A K-line gives the UUID for the ticket which this control file
d0305b3… aku 808 ** is amending.
d0305b3… aku 809 */
d0305b3… aku 810 case 'K': {
aab9e66… drh 811 if( p->zTicketUuid!=0 ) SYNTAX("more than one K-card");
d13054c… drh 812 p->zTicketUuid = next_token(&x, &sz);
323299c… drh 813 if( sz!=HNAME_LEN_SHA1 ) SYNTAX("K-card UUID is the wrong size");
323299c… drh 814 if( !validate16(p->zTicketUuid, sz) ){
aab9e66… drh 815 SYNTAX("invalid K-card UUID");
aab9e66… drh 816 }
99fcc43… drh 817 p->type = CFTYPE_TICKET;
d0305b3… aku 818 break;
d0305b3… aku 819 }
d0305b3… aku 820
d0305b3… aku 821 /*
d0305b3… aku 822 ** L <wikititle>
d0305b3… aku 823 **
d0305b3… aku 824 ** The wiki page title is fossil-encoded. There may be no more than
d0305b3… aku 825 ** one L line.
d0305b3… aku 826 */
d0305b3… aku 827 case 'L': {
aab9e66… drh 828 if( p->zWikiTitle!=0 ) SYNTAX("more than one L-card");
d13054c… drh 829 p->zWikiTitle = next_token(&x,0);
aab9e66… drh 830 if( p->zWikiTitle==0 ) SYNTAX("missing title on L-card");
d0305b3… aku 831 defossilize(p->zWikiTitle);
1942d58… drh 832 if( !wiki_name_is_wellformed((const unsigned char *)p->zWikiTitle) ){
aab9e66… drh 833 SYNTAX("L-card has malformed wiki name");
d0305b3… aku 834 }
99fcc43… drh 835 p->type = CFTYPE_WIKI;
d0305b3… aku 836 break;
d0305b3… aku 837 }
d0305b3… aku 838
d0305b3… aku 839 /*
fd9b7bd… drh 840 ** M <hash>
d0305b3… aku 841 **
fd9b7bd… drh 842 ** An M-line identifies another artifact by its hash. M-lines
d0305b3… aku 843 ** occur in clusters only.
d0305b3… aku 844 */
d0305b3… aku 845 case 'M': {
d13054c… drh 846 zUuid = next_token(&x, &sz);
fd9b7bd… drh 847 if( zUuid==0 ) SYNTAX("missing hash on M-card");
fd9b7bd… drh 848 if( !hname_validate(zUuid,sz) ){
fd9b7bd… drh 849 SYNTAX("Invalid hash on M-card");
fd9b7bd… drh 850 }
d0305b3… aku 851 if( p->nCChild>=p->nCChildAlloc ){
d0305b3… aku 852 p->nCChildAlloc = p->nCChildAlloc*2 + 10;
8f41b2f… drh 853 p->azCChild = fossil_realloc(p->azCChild
8f41b2f… drh 854 , p->nCChildAlloc*sizeof(p->azCChild[0]) );
d0305b3… aku 855 }
d0305b3… aku 856 i = p->nCChild++;
d0305b3… aku 857 p->azCChild[i] = zUuid;
31c52c7… drh 858 if( i>0 && fossil_strcmp(p->azCChild[i-1], zUuid)>=0 ){
aab9e66… drh 859 SYNTAX("M-card in the wrong order");
d0305b3… aku 860 }
99fcc43… drh 861 p->type = CFTYPE_CLUSTER;
4dcea80… drh 862 break;
4dcea80… drh 863 }
4dcea80… drh 864
4dcea80… drh 865 /*
4dcea80… drh 866 ** N <uuid>
4dcea80… drh 867 **
4dcea80… drh 868 ** An N-line identifies the mimetype of wiki or comment text.
4dcea80… drh 869 */
4dcea80… drh 870 case 'N': {
4dcea80… drh 871 if( p->zMimetype!=0 ) SYNTAX("more than one N-card");
4dcea80… drh 872 p->zMimetype = next_token(&x,0);
4dcea80… drh 873 if( p->zMimetype==0 ) SYNTAX("missing mimetype on N-card");
4dcea80… drh 874 defossilize(p->zMimetype);
d0305b3… aku 875 break;
d0305b3… aku 876 }
d0305b3… aku 877
d0305b3… aku 878 /*
d0305b3… aku 879 ** P <uuid> ...
d0305b3… aku 880 **
88e94eb… stephan 881 ** Specify one or more other artifacts which are the parents of
d0305b3… aku 882 ** this artifact. The first parent is the primary parent. All
b5f2ebd… stephan 883 ** others are parents by merge. Note that the initial empty
c49030f… drh 884 ** check-in historically has an empty P-card, so empty P-cards
b5f2ebd… stephan 885 ** must be accepted.
d0305b3… aku 886 */
d0305b3… aku 887 case 'P': {
d13054c… drh 888 while( (zUuid = next_token(&x, &sz))!=0 ){
fd9b7bd… drh 889 if( !hname_validate(zUuid, sz) ){
fd9b7bd… drh 890 SYNTAX("invalid hash on P-card");
fd9b7bd… drh 891 }
d0305b3… aku 892 if( p->nParent>=p->nParentAlloc ){
d0305b3… aku 893 p->nParentAlloc = p->nParentAlloc*2 + 5;
8f41b2f… drh 894 p->azParent = fossil_realloc(p->azParent,
8f41b2f… drh 895 p->nParentAlloc*sizeof(char*));
d0305b3… aku 896 }
d0305b3… aku 897 i = p->nParent++;
d0305b3… aku 898 p->azParent[i] = zUuid;
d0305b3… aku 899 }
8f41b2f… drh 900 break;
8f41b2f… drh 901 }
8f41b2f… drh 902
8f41b2f… drh 903 /*
7fcbbb1… drh 904 ** Q (+|-)<uuid> ?<uuid>?
7fcbbb1… drh 905 **
6ec2c2e… mistachkin 906 ** Specify one or a range of check-ins that are cherrypicked into
c49030f… drh 907 ** this check-in ("+") or backed out of this check-in ("-").
7fcbbb1… drh 908 */
7fcbbb1… drh 909 case 'Q': {
fd9b7bd… drh 910 if( (zUuid=next_token(&x, &sz))==0 ) SYNTAX("missing hash on Q-card");
aab9e66… drh 911 if( zUuid[0]!='+' && zUuid[0]!='-' ){
aab9e66… drh 912 SYNTAX("Q-card does not begin with '+' or '-'");
aab9e66… drh 913 }
fd9b7bd… drh 914 if( !hname_validate(&zUuid[1], sz-1) ){
fd9b7bd… drh 915 SYNTAX("invalid hash on Q-card");
aab9e66… drh 916 }
7fcbbb1… drh 917 n = p->nCherrypick;
7fcbbb1… drh 918 p->nCherrypick++;
7fcbbb1… drh 919 p->aCherrypick = fossil_realloc(p->aCherrypick,
7fcbbb1… drh 920 p->nCherrypick*sizeof(p->aCherrypick[0]));
7fcbbb1… drh 921 p->aCherrypick[n].zCPTarget = zUuid;
7fcbbb1… drh 922 p->aCherrypick[n].zCPBase = zUuid = next_token(&x, &sz);
fd9b7bd… drh 923 if( zUuid && !hname_validate(zUuid,sz) ){
fd9b7bd… drh 924 SYNTAX("invalid second hash on Q-card");
7fcbbb1… drh 925 }
99fcc43… drh 926 p->type = CFTYPE_MANIFEST;
7fcbbb1… drh 927 break;
7fcbbb1… drh 928 }
7fcbbb1… drh 929
7fcbbb1… drh 930 /*
d0305b3… aku 931 ** R <md5sum>
d0305b3… aku 932 **
aab38ef… drh 933 ** Specify the MD5 checksum over the name and content of all files
aab38ef… drh 934 ** in the manifest.
d0305b3… aku 935 */
d0305b3… aku 936 case 'R': {
4e092e0… jan.nijtmans 937 if( p->zRepoCksum!=0 ) SYNTAX("more than one R-card");
d13054c… drh 938 p->zRepoCksum = next_token(&x, &sz);
aab9e66… drh 939 if( sz!=32 ) SYNTAX("wrong size cksum on R-card");
aab9e66… drh 940 if( !validate16(p->zRepoCksum, 32) ) SYNTAX("malformed R-card cksum");
99fcc43… drh 941 p->type = CFTYPE_MANIFEST;
d0305b3… aku 942 break;
d0305b3… aku 943 }
d0305b3… aku 944
d0305b3… aku 945 /*
d0305b3… aku 946 ** T (+|*|-)<tagname> <uuid> ?<value>?
d0305b3… aku 947 **
d0305b3… aku 948 ** Create or cancel a tag or property. The tagname is fossil-encoded.
d0305b3… aku 949 ** The first character of the name must be either "+" to create a
d0305b3… aku 950 ** singleton tag, "*" to create a propagating tag, or "-" to create
33d3bf3… km 951 ** anti-tag that undoes a prior "+" or blocks propagation of a "*".
d0305b3… aku 952 **
d0305b3… aku 953 ** The tag is applied to <uuid>. If <uuid> is "*" then the tag is
52b35c8… jan.nijtmans 954 ** applied to the current manifest. If <value> is provided then
d0305b3… aku 955 ** the tag is really a property with the given value.
d0305b3… aku 956 **
d0305b3… aku 957 ** Tags are not allowed in clusters. Multiple T lines are allowed.
d0305b3… aku 958 */
d0305b3… aku 959 case 'T': {
d13054c… drh 960 char *zName, *zValue;
d13054c… drh 961 zName = next_token(&x, 0);
aab9e66… drh 962 if( zName==0 ) SYNTAX("missing name on T-card");
d13054c… drh 963 zUuid = next_token(&x, &sz);
fd9b7bd… drh 964 if( zUuid==0 ) SYNTAX("missing artifact hash on T-card");
d13054c… drh 965 zValue = next_token(&x, 0);
d13054c… drh 966 if( zValue ) defossilize(zValue);
fd9b7bd… drh 967 if( hname_validate(zUuid, sz) ){
fd9b7bd… drh 968 /* A valid artifact hash */
d13054c… drh 969 }else if( sz==1 && zUuid[0]=='*' ){
0afb5e8… drh 970 zUuid = 0;
99fcc43… drh 971 nSelfTag++;
0afb5e8… drh 972 }else{
fd9b7bd… drh 973 SYNTAX("malformed artifact hash on T-card");
d0305b3… aku 974 }
d0305b3… aku 975 defossilize(zName);
08db9e1… drh 976 if( zName[0]!='-' && zName[0]!='+' && zName[0]!='*' ){
aab9e66… drh 977 SYNTAX("T-card name does not begin with '-', '+', or '*'");
d0305b3… aku 978 }
99fcc43… drh 979 if( zName[0]=='+' ) nSimpleTag++;
d0305b3… aku 980 if( validate16(&zName[1], strlen(&zName[1])) ){
fd9b7bd… drh 981 /* Do not allow tags whose names look like a hash */
fd9b7bd… drh 982 SYNTAX("T-card name looks like a hexadecimal hash");
d0305b3… aku 983 }
d0305b3… aku 984 if( p->nTag>=p->nTagAlloc ){
d0305b3… aku 985 p->nTagAlloc = p->nTagAlloc*2 + 10;
8f41b2f… drh 986 p->aTag = fossil_realloc(p->aTag, p->nTagAlloc*sizeof(p->aTag[0]) );
d0305b3… aku 987 }
d0305b3… aku 988 i = p->nTag++;
d0305b3… aku 989 p->aTag[i].zName = zName;
d0305b3… aku 990 p->aTag[i].zUuid = zUuid;
d0305b3… aku 991 p->aTag[i].zValue = zValue;
2979c08… drh 992 if( i>0 ){
2979c08… drh 993 int c = fossil_strcmp(p->aTag[i-1].zName, zName);
2979c08… drh 994 if( c>0 || (c==0 && fossil_strcmp(p->aTag[i-1].zUuid, zUuid)>=0) ){
2979c08… drh 995 SYNTAX("T-card in the wrong order");
2979c08… drh 996 }
d0305b3… aku 997 }
d0305b3… aku 998 break;
d0305b3… aku 999 }
d0305b3… aku 1000
d0305b3… aku 1001 /*
6eddf50… drh 1002 ** U ?<login>?
d0305b3… aku 1003 **
d0305b3… aku 1004 ** Identify the user who created this control file by their
d0305b3… aku 1005 ** login. Only one U line is allowed. Prohibited in clusters.
6eddf50… drh 1006 ** If the user name is omitted, take that to be "anonymous".
d0305b3… aku 1007 */
d0305b3… aku 1008 case 'U': {
902135f… stephan 1009 if( p->zUser!=0 ) SYNTAX("more than one U-card");
d13054c… drh 1010 p->zUser = next_token(&x, 0);
23d45ff… drh 1011 if( p->zUser==0 || p->zUser[0]==0 ){
6eddf50… drh 1012 p->zUser = "anonymous";
6eddf50… drh 1013 }else{
6eddf50… drh 1014 defossilize(p->zUser);
6eddf50… drh 1015 }
d0305b3… aku 1016 break;
d0305b3… aku 1017 }
d0305b3… aku 1018
d0305b3… aku 1019 /*
d0305b3… aku 1020 ** W <size>
d0305b3… aku 1021 **
d0305b3… aku 1022 ** The next <size> bytes of the file contain the text of the wiki
d0305b3… aku 1023 ** page. There is always an extra \n before the start of the next
d0305b3… aku 1024 ** record.
d0305b3… aku 1025 */
d0305b3… aku 1026 case 'W': {
d13054c… drh 1027 char *zSize;
aab9e66… drh 1028 unsigned size, oldsize, c;
d0305b3… aku 1029 Blob wiki;
d13054c… drh 1030 zSize = next_token(&x, 0);
aab9e66… drh 1031 if( zSize==0 ) SYNTAX("missing size on W-card");
aab9e66… drh 1032 if( x.atEol==0 ) SYNTAX("no content after W-card");
aab9e66… drh 1033 for(oldsize=size=0; (c = zSize[0])>='0' && c<='9'; zSize++){
aab9e66… drh 1034 size = oldsize*10 + c - '0';
aab9e66… drh 1035 if( size<oldsize ) SYNTAX("size overflow on W-card");
aab9e66… drh 1036 oldsize = size;
d13054c… drh 1037 }
aab9e66… drh 1038 if( p->zWiki!=0 ) SYNTAX("more than one W-card");
d0305b3… aku 1039 blob_zero(&wiki);
aab9e66… drh 1040 if( (&x.z[size+1])>=x.zEnd )SYNTAX("not enough content after W-card");
d13054c… drh 1041 p->zWiki = x.z;
d13054c… drh 1042 x.z += size;
aab9e66… drh 1043 if( x.z[0]!='\n' ) SYNTAX("W-card content no \\n terminated");
d13054c… drh 1044 x.z[0] = 0;
d13054c… drh 1045 x.z++;
d0305b3… aku 1046 break;
d0305b3… aku 1047 }
d0305b3… aku 1048
d0305b3… aku 1049
d0305b3… aku 1050 /*
d0305b3… aku 1051 ** Z <md5sum>
d0305b3… aku 1052 **
d0305b3… aku 1053 ** MD5 checksum on this control file. The checksum is over all
d0305b3… aku 1054 ** lines (other than PGP-signature lines) prior to the current
d0305b3… aku 1055 ** line. This must be the last record.
d0305b3… aku 1056 **
d0305b3… aku 1057 ** This card is required for all control file types except for
d0305b3… aku 1058 ** Manifest. It is not required for manifest only for historical
d0305b3… aku 1059 ** compatibility reasons.
d0305b3… aku 1060 */
d0305b3… aku 1061 case 'Z': {
d13054c… drh 1062 zUuid = next_token(&x, &sz);
aab9e66… drh 1063 if( sz!=32 ) SYNTAX("wrong size for Z-card cksum");
aab9e66… drh 1064 if( !validate16(zUuid, 32) ) SYNTAX("malformed Z-card cksum");
d0305b3… aku 1065 break;
d0305b3… aku 1066 }
d0305b3… aku 1067 default: {
aab9e66… drh 1068 SYNTAX("unrecognized card");
aab9e66… drh 1069 }
aab9e66… drh 1070 }
aab9e66… drh 1071 }
7b8e475… drh 1072 if( x.z<x.zEnd ) SYNTAX("extra characters at end of card");
7b8e475… drh 1073
99fcc43… drh 1074 /* If the artifact type has not yet been determined, then compute
99fcc43… drh 1075 ** it now. */
99fcc43… drh 1076 if( p->type==0 ){
b3ccc4b… drh 1077 if( p->zComment!=0 || p->nFile>0 || p->nParent>0 ){
b3ccc4b… drh 1078 p->type = CFTYPE_MANIFEST;
b3ccc4b… drh 1079 }else{
b3ccc4b… drh 1080 p->type = CFTYPE_CONTROL;
b3ccc4b… drh 1081 }
99fcc43… drh 1082 }
99fcc43… drh 1083
99fcc43… drh 1084 /* Verify that no disallowed cards are present for this artifact type */
99fcc43… drh 1085 m = manifest_card_mask(manifestCardTypes[p->type-1].zAllowed);
99fcc43… drh 1086 if( seenCard & ~m ){
99fcc43… drh 1087 sqlite3_snprintf(sizeof(zErrBuf), zErrBuf, "%c-card in %s",
99fcc43… drh 1088 maskToType(seenCard & ~m),
99fcc43… drh 1089 azNameOfMType[p->type-1]);
99fcc43… drh 1090 zErr = zErrBuf;
99fcc43… drh 1091 goto manifest_syntax_error;
99fcc43… drh 1092 }
99fcc43… drh 1093
99fcc43… drh 1094 /* Verify that all required cards are present for this artifact type */
99fcc43… drh 1095 m = manifest_card_mask(manifestCardTypes[p->type-1].zRequired);
99fcc43… drh 1096 if( ~seenCard & m ){
99fcc43… drh 1097 sqlite3_snprintf(sizeof(zErrBuf), zErrBuf, "%c-card missing in %s",
99fcc43… drh 1098 maskToType(~seenCard & m),
99fcc43… drh 1099 azNameOfMType[p->type-1]);
99fcc43… drh 1100 zErr = zErrBuf;
99fcc43… drh 1101 goto manifest_syntax_error;
99fcc43… drh 1102 }
99fcc43… drh 1103
99fcc43… drh 1104 /* Additional checks based on artifact type */
99fcc43… drh 1105 switch( p->type ){
99fcc43… drh 1106 case CFTYPE_CONTROL: {
99fcc43… drh 1107 if( nSelfTag ) SYNTAX("self-referential T-card in control artifact");
99fcc43… drh 1108 break;
99fcc43… drh 1109 }
99fcc43… drh 1110 case CFTYPE_EVENT: {
99fcc43… drh 1111 if( p->nTag!=nSelfTag ){
99fcc43… drh 1112 SYNTAX("non-self-referential T-card in technote");
99fcc43… drh 1113 }
99fcc43… drh 1114 if( p->nTag!=nSimpleTag ){
99fcc43… drh 1115 SYNTAX("T-card with '*' or '-' in technote");
99fcc43… drh 1116 }
99fcc43… drh 1117 break;
99fcc43… drh 1118 }
99fcc43… drh 1119 case CFTYPE_FORUM: {
99fcc43… drh 1120 if( p->zThreadTitle && p->zInReplyTo ){
99fcc43… drh 1121 SYNTAX("cannot have I-card and H-card in a forum post");
99fcc43… drh 1122 }
99fcc43… drh 1123 if( p->nParent>1 ) SYNTAX("too many arguments to P-card");
99fcc43… drh 1124 break;
99fcc43… drh 1125 }
99fcc43… drh 1126 }
99fcc43… drh 1127
d13054c… drh 1128 md5sum_init();
f25e5e5… drh 1129 if( !isRepeat ) g.parseCnt[p->type]++;
d13054c… drh 1130 return p;
d13054c… drh 1131
d13054c… drh 1132 manifest_syntax_error:
0759200… stephan 1133 if(pErr!=0){
0759200… stephan 1134 char *zUuid = rid>0 ? rid_to_uuid(rid) : 0;
fd9b7bd… drh 1135 if( zUuid ){
0759200… stephan 1136 blob_appendf(pErr, "artifact [%s] ", zUuid);
fd9b7bd… drh 1137 fossil_free(zUuid);
fd9b7bd… drh 1138 }
992b62f… stephan 1139 if( zErr ){
992b62f… stephan 1140 blob_appendf(pErr, "line %d: %s", lineNo, zErr);
992b62f… stephan 1141 }else{
992b62f… stephan 1142 blob_appendf(pErr, "unknown error on line %d", lineNo);
992b62f… stephan 1143 }
aab9e66… drh 1144 }
d13054c… drh 1145 md5sum_init();
d13054c… drh 1146 manifest_destroy(p);
d13054c… drh 1147 return 0;
d13054c… drh 1148 }
d13054c… drh 1149
d13054c… drh 1150 /*
d13054c… drh 1151 ** Get a manifest given the rid for the control artifact. Return
d13054c… drh 1152 ** a pointer to the manifest on success or NULL if there is a failure.
d13054c… drh 1153 */
ec81aee… jan.nijtmans 1154 Manifest *manifest_get(int rid, int cfType, Blob *pErr){
d13054c… drh 1155 Blob content;
d13054c… drh 1156 Manifest *p;
195073f… drh 1157 if( !rid ) return 0;
d13054c… drh 1158 p = manifest_cache_find(rid);
d13054c… drh 1159 if( p ){
d13054c… drh 1160 if( cfType!=CFTYPE_ANY && cfType!=p->type ){
d13054c… drh 1161 manifest_cache_insert(p);
d13054c… drh 1162 p = 0;
d13054c… drh 1163 }
d13054c… drh 1164 return p;
d13054c… drh 1165 }
d13054c… drh 1166 content_get(rid, &content);
ec81aee… jan.nijtmans 1167 p = manifest_parse(&content, rid, pErr);
d13054c… drh 1168 if( p && cfType!=CFTYPE_ANY && cfType!=p->type ){
d13054c… drh 1169 manifest_destroy(p);
d13054c… drh 1170 p = 0;
d13054c… drh 1171 }
d13054c… drh 1172 return p;
d13054c… drh 1173 }
d13054c… drh 1174
d13054c… drh 1175 /*
c49030f… drh 1176 ** Given a check-in name, load and parse the manifest for that check-in.
d13054c… drh 1177 ** Throw a fatal error if anything goes wrong.
d13054c… drh 1178 */
d13054c… drh 1179 Manifest *manifest_get_by_name(const char *zName, int *pRid){
d13054c… drh 1180 int rid;
d13054c… drh 1181 Manifest *p;
d13054c… drh 1182
2a013f0… drh 1183 rid = name_to_typed_rid(zName, "ci");
d13054c… drh 1184 if( !is_a_version(rid) ){
c49030f… drh 1185 fossil_fatal("no such check-in: %s", zName);
d13054c… drh 1186 }
d13054c… drh 1187 if( pRid ) *pRid = rid;
ec81aee… jan.nijtmans 1188 p = manifest_get(rid, CFTYPE_MANIFEST, 0);
d13054c… drh 1189 if( p==0 ){
c49030f… drh 1190 fossil_fatal("cannot parse manifest for check-in: %s", zName);
d13054c… drh 1191 }
d13054c… drh 1192 return p;
c49030f… drh 1193 }
c49030f… drh 1194
c49030f… drh 1195 /*
0f7eff9… drh 1196 ** The input blob is text that may or may not be a valid Fossil
0f7eff9… drh 1197 ** control artifact of some kind. This routine returns true if
0f7eff9… drh 1198 ** the input is a well-formed control artifact and false if it
0f7eff9… drh 1199 ** is not.
0f7eff9… drh 1200 **
0f7eff9… drh 1201 ** This routine is optimized to return false quickly and with minimal
0f7eff9… drh 1202 ** work in the common case where the input is some random file.
0f7eff9… drh 1203 */
0f7eff9… drh 1204 int manifest_is_well_formed(const char *zIn, int nIn){
0f7eff9… drh 1205 int i;
0f7eff9… drh 1206 int iRes;
0f7eff9… drh 1207 Manifest *pManifest;
0f7eff9… drh 1208 Blob copy, errmsg;
0f7eff9… drh 1209 remove_pgp_signature(&zIn, &nIn);
0f7eff9… drh 1210
0f7eff9… drh 1211 /* Check to see that the file begins with a "card" */
0f7eff9… drh 1212 if( nIn<3 ) return 0;
0f7eff9… drh 1213 if( zIn[0]<'A' || zIn[0]>'M' || zIn[1]!=' ' ) return 0;
0f7eff9… drh 1214
0f7eff9… drh 1215 /* Check to see that the first card is followed by one more card */
0f7eff9… drh 1216 for(i=2; i<nIn && zIn[i]!='\n'; i++){}
0f7eff9… drh 1217 if( i>=nIn-3 ) return 0;
0f7eff9… drh 1218 i++;
0f7eff9… drh 1219 if( !fossil_isupper(zIn[i]) || zIn[i]<zIn[0] || zIn[i+1]!=' ' ) return 0;
0f7eff9… drh 1220
0f7eff9… drh 1221 /* The checks above will eliminate most random inputs. If these
0f7eff9… drh 1222 ** quick checks pass, then we could be dealing with a well-formed
0f7eff9… drh 1223 ** control artifact. Make a copy, and run it through the official
0f7eff9… drh 1224 ** artifact parser. This is the slow path, but it is rarely taken.
0f7eff9… drh 1225 */
0f7eff9… drh 1226 blob_init(&copy, 0, 0);
0f7eff9… drh 1227 blob_init(&errmsg, 0, 0);
0f7eff9… drh 1228 blob_append(&copy, zIn, nIn);
0f7eff9… drh 1229 pManifest = manifest_parse(&copy, 0, &errmsg);
275da70… danield 1230 iRes = pManifest!=0;
0f7eff9… drh 1231 manifest_destroy(pManifest);
0f7eff9… drh 1232 blob_reset(&errmsg);
0f7eff9… drh 1233 return iRes;
0f7eff9… drh 1234 }
0f7eff9… drh 1235
0f7eff9… drh 1236 /*
55fc643… drh 1237 ** COMMAND: test-parse-manifest
55fc643… drh 1238 **
7129882… drh 1239 ** Usage: %fossil test-parse-manifest FILENAME ?N?
55fc643… drh 1240 **
99fcc43… drh 1241 ** Parse the manifest(s) given on the command-line and report any
99fcc43… drh 1242 ** errors. If the N argument is given, run the parsing N times.
55fc643… drh 1243 */
55fc643… drh 1244 void manifest_test_parse_cmd(void){
d13054c… drh 1245 Manifest *p;
55fc643… drh 1246 Blob b;
7129882… drh 1247 int i;
7129882… drh 1248 int n = 1;
7616964… drh 1249 int isWF;
0f7eff9… drh 1250 db_find_and_open_repository(OPEN_SUBSTITUTE|OPEN_OK_NOT_FOUND,0);
b3ccc4b… drh 1251 verify_all_options();
7129882… drh 1252 if( g.argc!=3 && g.argc!=4 ){
55fc643… drh 1253 usage("FILENAME");
55fc643… drh 1254 }
1772357… drh 1255 blob_read_from_file(&b, g.argv[2], ExtFILE);
7129882… drh 1256 if( g.argc>3 ) n = atoi(g.argv[3]);
7616964… drh 1257 isWF = manifest_is_well_formed(blob_buffer(&b), blob_size(&b));
7616964… drh 1258 fossil_print("manifest_is_well_formed() reports the input %s\n",
7616964… drh 1259 isWF ? "is ok" : "contains errors");
7129882… drh 1260 for(i=0; i<n; i++){
7129882… drh 1261 Blob b2;
aab9e66… drh 1262 Blob err;
7129882… drh 1263 blob_copy(&b2, &b);
aab9e66… drh 1264 blob_zero(&err);
aab9e66… drh 1265 p = manifest_parse(&b2, 0, &err);
7616964… drh 1266 if( p==0 ){
7616964… drh 1267 fossil_print("ERROR: %s\n", blob_str(&err));
7616964… drh 1268 }else if( i==0 || (n==2 && i==1) ){
7616964… drh 1269 fossil_print("manifest_parse() worked\n");
7616964… drh 1270 }else if( i==n-1 ){
7616964… drh 1271 fossil_print("manifest_parse() worked %d more times\n", n-1);
7616964… drh 1272 }
7616964… drh 1273 if( (p==0 && isWF) || (p!=0 && !isWF) ){
7616964… drh 1274 fossil_print("ERROR: manifest_is_well_formed() and "
7616964… drh 1275 "manifest_parse() disagree!\n");
7616964… drh 1276 }
99fcc43… drh 1277 blob_reset(&err);
99fcc43… drh 1278 manifest_destroy(p);
99fcc43… drh 1279 }
cad57db… drh 1280 blob_reset(&b);
99fcc43… drh 1281 }
99fcc43… drh 1282
99fcc43… drh 1283 /*
99fcc43… drh 1284 ** COMMAND: test-parse-all-blobs
99fcc43… drh 1285 **
0f7eff9… drh 1286 ** Usage: %fossil test-parse-all-blobs ?OPTIONS?
99fcc43… drh 1287 **
99fcc43… drh 1288 ** Parse all entries in the BLOB table that are believed to be non-data
99fcc43… drh 1289 ** artifacts and report any errors. Run this test command on historical
99fcc43… drh 1290 ** repositories after making any changes to the manifest_parse()
99fcc43… drh 1291 ** implementation to confirm that the changes did not break anything.
52211cc… drh 1292 **
0f7eff9… drh 1293 ** Options:
4cb50c4… stephan 1294 ** --limit N Parse no more than N artifacts before stopping
0f7eff9… drh 1295 ** --wellformed Use all BLOB table entries as input, not just
0f7eff9… drh 1296 ** those entries that are believed to be valid
0f7eff9… drh 1297 ** artifacts, and verify that the result the
0f7eff9… drh 1298 ** manifest_is_well_formed() agrees with the
0f7eff9… drh 1299 ** result of manifest_parse().
99fcc43… drh 1300 */
99fcc43… drh 1301 void manifest_test_parse_all_blobs_cmd(void){
99fcc43… drh 1302 Manifest *p;
99fcc43… drh 1303 Blob err;
99fcc43… drh 1304 Stmt q;
99fcc43… drh 1305 int nTest = 0;
99fcc43… drh 1306 int nErr = 0;
52211cc… drh 1307 int N = 1000000000;
0f7eff9… drh 1308 int bWellFormed;
52211cc… drh 1309 const char *z;
99fcc43… drh 1310 db_find_and_open_repository(0, 0);
52211cc… drh 1311 z = find_option("limit", 0, 1);
52211cc… drh 1312 if( z ) N = atoi(z);
0f7eff9… drh 1313 bWellFormed = find_option("wellformed",0,0)!=0;
99fcc43… drh 1314 verify_all_options();
0f7eff9… drh 1315 if( bWellFormed ){
0f7eff9… drh 1316 db_prepare(&q, "SELECT rid FROM blob ORDER BY rid");
0f7eff9… drh 1317 }else{
0f7eff9… drh 1318 db_prepare(&q, "SELECT DISTINCT objid FROM EVENT ORDER BY objid");
0f7eff9… drh 1319 }
52211cc… drh 1320 while( (N--)>0 && db_step(&q)==SQLITE_ROW ){
99fcc43… drh 1321 int id = db_column_int(&q,0);
99fcc43… drh 1322 fossil_print("Checking %d \r", id);
99fcc43… drh 1323 nTest++;
99fcc43… drh 1324 fflush(stdout);
99fcc43… drh 1325 blob_init(&err, 0, 0);
0f7eff9… drh 1326 if( bWellFormed ){
0f7eff9… drh 1327 Blob content;
0f7eff9… drh 1328 int isWF;
0f7eff9… drh 1329 content_get(id, &content);
0f7eff9… drh 1330 isWF = manifest_is_well_formed(blob_buffer(&content),blob_size(&content));
0f7eff9… drh 1331 p = manifest_parse(&content, id, &err);
0f7eff9… drh 1332 if( isWF && p==0 ){
0f7eff9… drh 1333 fossil_print("%d ERROR: manifest_is_well_formed() reported true "
0f7eff9… drh 1334 "but manifest_parse() reports an error: %s\n",
0f7eff9… drh 1335 id, blob_str(&err));
0f7eff9… drh 1336 nErr++;
0f7eff9… drh 1337 }else if( !isWF && p!=0 ){
0f7eff9… drh 1338 fossil_print("%d ERROR: manifest_is_well_formed() reported false "
0f7eff9… drh 1339 "but manifest_parse() found nothing wrong.\n", id);
0f7eff9… drh 1340 nErr++;
0f7eff9… drh 1341 }
275da70… danield 1342 }else{
0f7eff9… drh 1343 p = manifest_get(id, CFTYPE_ANY, &err);
0f7eff9… drh 1344 if( p==0 ){
0f7eff9… drh 1345 fossil_print("%d ERROR: %s\n", id, blob_str(&err));
0f7eff9… drh 1346 nErr++;
0f7eff9… drh 1347 }
99fcc43… drh 1348 }
aab9e66… drh 1349 blob_reset(&err);
d13054c… drh 1350 manifest_destroy(p);
d13054c… drh 1351 }
99fcc43… drh 1352 db_finalize(&q);
99fcc43… drh 1353 fossil_print("%d tests with %d errors\n", nTest, nErr);
d13054c… drh 1354 }
d13054c… drh 1355
d13054c… drh 1356 /*
d13054c… drh 1357 ** Fetch the baseline associated with the delta-manifest p.
fda9b15… drh 1358 ** Return 0 on success. If unable to parse the baseline,
fda9b15… drh 1359 ** throw an error. If the baseline is a manifest, throw an
fda9b15… drh 1360 ** error if throwError is true, or record that p is an orphan
195073f… drh 1361 ** and return 1 if throwError is false.
d13054c… drh 1362 */
fda9b15… drh 1363 static int fetch_baseline(Manifest *p, int throwError){
d13054c… drh 1364 if( p->zBaseline!=0 && p->pBaseline==0 ){
195073f… drh 1365 int rid = uuid_to_rid(p->zBaseline, 1);
ec81aee… jan.nijtmans 1366 p->pBaseline = manifest_get(rid, CFTYPE_MANIFEST, 0);
8837795… drh 1367 if( p->pBaseline==0 ){
8837795… drh 1368 if( !throwError ){
8837795… drh 1369 db_multi_exec(
8837795… drh 1370 "INSERT OR IGNORE INTO orphan(rid, baseline) VALUES(%d,%d)",
195073f… drh 1371 p->rid, rid
8837795… drh 1372 );
8837795… drh 1373 return 1;
52b35c8… jan.nijtmans 1374 }
99fcc43… drh 1375 fossil_fatal("cannot access baseline manifest %S", p->zBaseline);
d13054c… drh 1376 }
d13054c… drh 1377 }
fda9b15… drh 1378 return 0;
d13054c… drh 1379 }
d13054c… drh 1380
d13054c… drh 1381 /*
d13054c… drh 1382 ** Rewind a manifest-file iterator back to the beginning of the manifest.
d13054c… drh 1383 */
d13054c… drh 1384 void manifest_file_rewind(Manifest *p){
d13054c… drh 1385 p->iFile = 0;
fda9b15… drh 1386 fetch_baseline(p, 1);
d13054c… drh 1387 if( p->pBaseline ){
d13054c… drh 1388 p->pBaseline->iFile = 0;
d13054c… drh 1389 }
d13054c… drh 1390 }
d13054c… drh 1391
d13054c… drh 1392 /*
d13054c… drh 1393 ** Advance to the next manifest-file.
d13054c… drh 1394 **
d13054c… drh 1395 ** Return NULL for end-of-records or if there is an error. If an error
d13054c… drh 1396 ** occurs and pErr!=0 then store 1 in *pErr.
d13054c… drh 1397 */
d13054c… drh 1398 ManifestFile *manifest_file_next(
52b35c8… jan.nijtmans 1399 Manifest *p,
d13054c… drh 1400 int *pErr
d13054c… drh 1401 ){
d13054c… drh 1402 ManifestFile *pOut = 0;
d13054c… drh 1403 if( pErr ) *pErr = 0;
d13054c… drh 1404 if( p->pBaseline==0 ){
d13054c… drh 1405 /* Manifest p is a baseline-manifest. Just scan down the list
d13054c… drh 1406 ** of files. */
d13054c… drh 1407 if( p->iFile<p->nFile ) pOut = &p->aFile[p->iFile++];
d13054c… drh 1408 }else{
d13054c… drh 1409 /* Manifest p is a delta-manifest. Scan the baseline but amend the
d13054c… drh 1410 ** file list in the baseline with changes described by p.
d13054c… drh 1411 */
d13054c… drh 1412 Manifest *pB = p->pBaseline;
d13054c… drh 1413 int cmp;
d13054c… drh 1414 while(1){
d13054c… drh 1415 if( pB->iFile>=pB->nFile ){
d13054c… drh 1416 /* We have used all entries out of the baseline. Return the next
d13054c… drh 1417 ** entry from the delta. */
d13054c… drh 1418 if( p->iFile<p->nFile ) pOut = &p->aFile[p->iFile++];
d13054c… drh 1419 break;
d13054c… drh 1420 }else if( p->iFile>=p->nFile ){
d13054c… drh 1421 /* We have used all entries from the delta. Return the next
d13054c… drh 1422 ** entry from the baseline. */
d13054c… drh 1423 if( pB->iFile<pB->nFile ) pOut = &pB->aFile[pB->iFile++];
d13054c… drh 1424 break;
31c52c7… drh 1425 }else if( (cmp = fossil_strcmp(pB->aFile[pB->iFile].zName,
d13054c… drh 1426 p->aFile[p->iFile].zName)) < 0 ){
d13054c… drh 1427 /* The next baseline entry comes before the next delta entry.
d13054c… drh 1428 ** So return the baseline entry. */
d13054c… drh 1429 pOut = &pB->aFile[pB->iFile++];
d13054c… drh 1430 break;
d13054c… drh 1431 }else if( cmp>0 ){
d13054c… drh 1432 /* The next delta entry comes before the next baseline
d13054c… drh 1433 ** entry so return the delta entry */
d13054c… drh 1434 pOut = &p->aFile[p->iFile++];
d13054c… drh 1435 break;
d13054c… drh 1436 }else if( p->aFile[p->iFile].zUuid ){
d13054c… drh 1437 /* The next delta entry is a replacement for the next baseline
d13054c… drh 1438 ** entry. Skip the baseline entry and return the delta entry */
d13054c… drh 1439 pB->iFile++;
d13054c… drh 1440 pOut = &p->aFile[p->iFile++];
d13054c… drh 1441 break;
d13054c… drh 1442 }else{
d13054c… drh 1443 /* The next delta entry is a delete of the next baseline
d13054c… drh 1444 ** entry. Skip them both. Repeat the loop to find the next
d13054c… drh 1445 ** non-delete entry. */
d13054c… drh 1446 pB->iFile++;
d13054c… drh 1447 p->iFile++;
d13054c… drh 1448 continue;
d13054c… drh 1449 }
d13054c… drh 1450 }
d13054c… drh 1451 }
d13054c… drh 1452 return pOut;
a9a27f8… drh 1453 }
a9a27f8… drh 1454
a9a27f8… drh 1455 /*
a9a27f8… drh 1456 ** Translate a filename into a filename-id (fnid). Create a new fnid
a9a27f8… drh 1457 ** if no previously exists.
a9a27f8… drh 1458 */
a9a27f8… drh 1459 static int filename_to_fnid(const char *zFilename){
a9a27f8… drh 1460 static Stmt q1, s1;
a9a27f8… drh 1461 int fnid;
a9a27f8… drh 1462 db_static_prepare(&q1, "SELECT fnid FROM filename WHERE name=:fn");
a9a27f8… drh 1463 db_bind_text(&q1, ":fn", zFilename);
a9a27f8… drh 1464 fnid = 0;
a9a27f8… drh 1465 if( db_step(&q1)==SQLITE_ROW ){
a9a27f8… drh 1466 fnid = db_column_int(&q1, 0);
a9a27f8… drh 1467 }
a9a27f8… drh 1468 db_reset(&q1);
a9a27f8… drh 1469 if( fnid==0 ){
a9a27f8… drh 1470 db_static_prepare(&s1, "INSERT INTO filename(name) VALUES(:fn)");
a9a27f8… drh 1471 db_bind_text(&s1, ":fn", zFilename);
a9a27f8… drh 1472 db_exec(&s1);
a9a27f8… drh 1473 fnid = db_last_insert_rowid();
a9a27f8… drh 1474 }
a9a27f8… drh 1475 return fnid;
a9a27f8… drh 1476 }
a9a27f8… drh 1477
a9a27f8… drh 1478 /*
7f11789… drh 1479 ** Compute an appropriate mlink.mperm integer for the permission string
7f11789… drh 1480 ** of a file.
7f11789… drh 1481 */
1243bf3… stephan 1482 int manifest_file_mperm(const ManifestFile *pFile){
e4f1c1f… drh 1483 int mperm = PERM_REG;
e4f1c1f… drh 1484 if( pFile && pFile->zPerm){
4bf670e… drh 1485 if( strstr(pFile->zPerm,"x")!=0 ){
e4f1c1f… drh 1486 mperm = PERM_EXE;
4bf670e… drh 1487 }else if( strstr(pFile->zPerm,"l")!=0 ){
e4f1c1f… drh 1488 mperm = PERM_LNK;
4bf670e… drh 1489 }
7f11789… drh 1490 }
7f11789… drh 1491 return mperm;
7f11789… drh 1492 }
7f11789… drh 1493
7f11789… drh 1494 /*
dbda8d6… drh 1495 ** Add a single entry to the mlink table. Also add the filename to
dbda8d6… drh 1496 ** the filename table if it is not there already.
e2c5353… drh 1497 **
e2c5353… drh 1498 ** An mlink entry is always created if isPrimary is true. But if
e2c5353… drh 1499 ** isPrimary is false (meaning that pmid is a merge parent of mid)
e2c5353… drh 1500 ** then the mlink entry is only created if there is already an mlink
e2c5353… drh 1501 ** from primary parent for the same file.
dbda8d6… drh 1502 */
dbda8d6… drh 1503 static void add_one_mlink(
65aa10f… drh 1504 int pmid, /* The parent manifest */
fd9b7bd… drh 1505 const char *zFromUuid, /* Artifact hash for content in parent */
dbda8d6… drh 1506 int mid, /* The record ID of the manifest */
fd9b7bd… drh 1507 const char *zToUuid, /* artifact hash for content in child */
9fd8009… drh 1508 const char *zFilename, /* Filename */
7f11789… drh 1509 const char *zPrior, /* Previous filename. NULL if unchanged */
2985120… drh 1510 int isPublic, /* True if mid is not a private manifest */
65aa10f… drh 1511 int isPrimary, /* pmid is the primary parent of mid */
e4f1c1f… drh 1512 int mperm /* 1: exec, 2: symlink */
dbda8d6… drh 1513 ){
9fd8009… drh 1514 int fnid, pfnid, pid, fid;
e2c5353… drh 1515 int doInsert;
e2c5353… drh 1516 static Stmt s1, s2;
9fd8009… drh 1517
a9a27f8… drh 1518 fnid = filename_to_fnid(zFilename);
9fd8009… drh 1519 if( zPrior==0 ){
9fd8009… drh 1520 pfnid = 0;
9fd8009… drh 1521 }else{
a9a27f8… drh 1522 pfnid = filename_to_fnid(zPrior);
9fd8009… drh 1523 }
d13054c… drh 1524 if( zFromUuid==0 || zFromUuid[0]==0 ){
dbda8d6… drh 1525 pid = 0;
dbda8d6… drh 1526 }else{
dbda8d6… drh 1527 pid = uuid_to_rid(zFromUuid, 1);
dbda8d6… drh 1528 }
d13054c… drh 1529 if( zToUuid==0 || zToUuid[0]==0 ){
d0305b3… aku 1530 fid = 0;
d0305b3… aku 1531 }else{
d0305b3… aku 1532 fid = uuid_to_rid(zToUuid, 1);
2985120… drh 1533 if( isPublic ) content_make_public(fid);
2985120… drh 1534 }
e2c5353… drh 1535 if( isPrimary ){
e2c5353… drh 1536 doInsert = 1;
e2c5353… drh 1537 }else{
e2c5353… drh 1538 db_static_prepare(&s2,
e2c5353… drh 1539 "SELECT 1 FROM mlink WHERE mid=:m AND fnid=:n AND NOT isaux"
e2c5353… drh 1540 );
e2c5353… drh 1541 db_bind_int(&s2, ":m", mid);
e2c5353… drh 1542 db_bind_int(&s2, ":n", fnid);
e2c5353… drh 1543 doInsert = db_step(&s2)==SQLITE_ROW;
e2c5353… drh 1544 db_reset(&s2);
e2c5353… drh 1545 }
e2c5353… drh 1546 if( doInsert ){
e2c5353… drh 1547 db_static_prepare(&s1,
e2c5353… drh 1548 "INSERT INTO mlink(mid,fid,pmid,pid,fnid,pfnid,mperm,isaux)"
e2c5353… drh 1549 "VALUES(:m,:f,:pm,:p,:n,:pfn,:mp,:isaux)"
e2c5353… drh 1550 );
e2c5353… drh 1551 db_bind_int(&s1, ":m", mid);
e2c5353… drh 1552 db_bind_int(&s1, ":f", fid);
e2c5353… drh 1553 db_bind_int(&s1, ":pm", pmid);
e2c5353… drh 1554 db_bind_int(&s1, ":p", pid);
e2c5353… drh 1555 db_bind_int(&s1, ":n", fnid);
e2c5353… drh 1556 db_bind_int(&s1, ":pfn", pfnid);
e2c5353… drh 1557 db_bind_int(&s1, ":mp", mperm);
e2c5353… drh 1558 db_bind_int(&s1, ":isaux", isPrimary==0);
e2c5353… drh 1559 db_exec(&s1);
e2c5353… drh 1560 }
d0305b3… aku 1561 if( pid && fid ){
a4047a9… drh 1562 content_deltify(pid, &fid, 1, 0);
9fd8009… drh 1563 }
9fd8009… drh 1564 }
9fd8009… drh 1565
9fd8009… drh 1566 /*
52b35c8… jan.nijtmans 1567 ** Do a binary search to find a file in the p->aFile[] array.
d13054c… drh 1568 **
d13054c… drh 1569 ** As an optimization, guess that the file we seek is at index p->iFile.
d13054c… drh 1570 ** That will usually be the case. If it is not found there, then do the
d13054c… drh 1571 ** actual binary search.
d13054c… drh 1572 **
d13054c… drh 1573 ** Update p->iFile to be the index of the file that is found.
9fd8009… drh 1574 */
4bf670e… drh 1575 static ManifestFile *manifest_file_seek_base(
4bf670e… drh 1576 Manifest *p, /* Manifest to search */
4bf670e… drh 1577 const char *zName, /* Name of the file we are looking for */
4bf670e… drh 1578 int bBest /* 0: exact match only. 1: closest match */
4bf670e… drh 1579 ){
9fd8009… drh 1580 int lwr, upr;
9fd8009… drh 1581 int c;
9fd8009… drh 1582 int i;
55f6c76… drh 1583 if( p->aFile==0 ){
55f6c76… drh 1584 return 0;
55f6c76… drh 1585 }
9fd8009… drh 1586 lwr = 0;
9fd8009… drh 1587 upr = p->nFile - 1;
d13054c… drh 1588 if( p->iFile>=lwr && p->iFile<upr ){
31c52c7… drh 1589 c = fossil_strcmp(p->aFile[p->iFile+1].zName, zName);
d13054c… drh 1590 if( c==0 ){
d13054c… drh 1591 return &p->aFile[++p->iFile];
d13054c… drh 1592 }else if( c>0 ){
d13054c… drh 1593 upr = p->iFile;
d13054c… drh 1594 }else{
d13054c… drh 1595 lwr = p->iFile+1;
d13054c… drh 1596 }
d13054c… drh 1597 }
9fd8009… drh 1598 while( lwr<=upr ){
9fd8009… drh 1599 i = (lwr+upr)/2;
31c52c7… drh 1600 c = fossil_strcmp(p->aFile[i].zName, zName);
9fd8009… drh 1601 if( c<0 ){
9fd8009… drh 1602 lwr = i+1;
9fd8009… drh 1603 }else if( c>0 ){
9fd8009… drh 1604 upr = i-1;
9fd8009… drh 1605 }else{
d13054c… drh 1606 p->iFile = i;
d13054c… drh 1607 return &p->aFile[i];
d13054c… drh 1608 }
31c52c7… drh 1609 }
1b4403c… drh 1610 if( bBest ){
2e51be8… drh 1611 if( lwr>=p->nFile ) lwr = p->nFile-1;
1b4403c… drh 1612 i = (int)strlen(zName);
1b4403c… drh 1613 if( strncmp(zName, p->aFile[lwr].zName, i)==0 ) return &p->aFile[lwr];
1b4403c… drh 1614 }
1b4403c… drh 1615 return 0;
d13054c… drh 1616 }
d13054c… drh 1617
d13054c… drh 1618 /*
d13054c… drh 1619 ** Locate a file named zName in the aFile[] array of the given manifest.
d13054c… drh 1620 ** Return a pointer to the appropriate ManifestFile object. Return NULL
d13054c… drh 1621 ** if not found.
d13054c… drh 1622 **
52b35c8… jan.nijtmans 1623 ** This routine works even if p is a delta-manifest. The pointer
d13054c… drh 1624 ** returned might be to the baseline.
d13054c… drh 1625 **
d13054c… drh 1626 ** We assume that filenames are in sorted order and use a binary search.
d13054c… drh 1627 */
4bf670e… drh 1628 ManifestFile *manifest_file_seek(Manifest *p, const char *zName, int bBest){
d13054c… drh 1629 ManifestFile *pFile;
52b35c8… jan.nijtmans 1630
4bf670e… drh 1631 pFile = manifest_file_seek_base(p, zName, p->zBaseline ? 0 : bBest);
d13054c… drh 1632 if( pFile && pFile->zUuid==0 ) return 0;
d13054c… drh 1633 if( pFile==0 && p->zBaseline ){
fda9b15… drh 1634 fetch_baseline(p, 1);
4bf670e… drh 1635 pFile = manifest_file_seek_base(p->pBaseline, zName,bBest);
d13054c… drh 1636 }
d13054c… drh 1637 return pFile;
9c90b0f… drh 1638 }
9c90b0f… drh 1639
9c90b0f… drh 1640 /*
9c90b0f… drh 1641 ** Look for a file in a manifest, taking the case-sensitive option
9c90b0f… drh 1642 ** into account. If case-sensitive is off, then files in any case
9c90b0f… drh 1643 ** will match.
9c90b0f… drh 1644 */
9c90b0f… drh 1645 ManifestFile *manifest_file_find(Manifest *p, const char *zName){
aa55cf3… mistachkin 1646 int i;
9c90b0f… drh 1647 Manifest *pBase;
9c90b0f… drh 1648 if( filenames_are_case_sensitive() ){
4bf670e… drh 1649 return manifest_file_seek(p, zName, 0);
9c90b0f… drh 1650 }
9c90b0f… drh 1651 for(i=0; i<p->nFile; i++){
9c90b0f… drh 1652 if( fossil_stricmp(zName, p->aFile[i].zName)==0 ){
9c90b0f… drh 1653 return &p->aFile[i];
9c90b0f… drh 1654 }
9c90b0f… drh 1655 }
9c90b0f… drh 1656 if( p->zBaseline==0 ) return 0;
9c90b0f… drh 1657 fetch_baseline(p, 1);
9c90b0f… drh 1658 pBase = p->pBaseline;
9c90b0f… drh 1659 if( pBase==0 ) return 0;
9c90b0f… drh 1660 for(i=0; i<pBase->nFile; i++){
72c0183… drh 1661 if( fossil_stricmp(zName, pBase->aFile[i].zName)==0 ){
72c0183… drh 1662 return &pBase->aFile[i];
9c90b0f… drh 1663 }
9c90b0f… drh 1664 }
9c90b0f… drh 1665 return 0;
7f11789… drh 1666 }
7f11789… drh 1667
7f11789… drh 1668 /*
7f11789… drh 1669 ** Add mlink table entries associated with manifest cid, pChild. The
7f11789… drh 1670 ** parent manifest is pid, pParent. One of either pChild or pParent
7f11789… drh 1671 ** will be NULL and it will be computed based on cid/pid.
9fd8009… drh 1672 **
7f11789… drh 1673 ** A single mlink entry is added for every file that changed content,
7f11789… drh 1674 ** name, and/or permissions going from pid to cid.
d0305b3… aku 1675 **
d0305b3… aku 1676 ** Deleted files have mlink.fid=0.
d0305b3… aku 1677 ** Added files have mlink.pid=0.
8e44cf6… drh 1678 ** File added by merge have mlink.pid=-1
d0305b3… aku 1679 ** Edited files have both mlink.pid!=0 and mlink.fid!=0
e2c5353… drh 1680 **
e2c5353… drh 1681 ** Many mlink entries for merge parents will only be added if another mlink
e2c5353… drh 1682 ** entry already exists for the same file from the primary parent. Therefore,
e2c5353… drh 1683 ** to ensure that all merge-parent mlink entries are properly created:
e2c5353… drh 1684 **
e2c5353… drh 1685 ** (1) Make this routine a no-op if pParent is a merge parent and the
e2c5353… drh 1686 ** primary parent is a phantom.
e2c5353… drh 1687 ** (2) Invoke this routine recursively for merge-parents if pParent is the
e2c5353… drh 1688 ** primary parent.
d0305b3… aku 1689 */
65aa10f… drh 1690 static void add_mlink(
9a8d082… drh 1691 int pmid, Manifest *pParent, /* Parent check-in */
9a8d082… drh 1692 int mid, Manifest *pChild, /* The child check-in */
9a8d082… drh 1693 int isPrim /* TRUE if pmid is the primary parent of mid */
65aa10f… drh 1694 ){
d0305b3… aku 1695 Blob otherContent;
0a55d16… drh 1696 int otherRid;
d13054c… drh 1697 int i, rc;
d13054c… drh 1698 ManifestFile *pChildFile, *pParentFile;
d13054c… drh 1699 Manifest **ppOther;
d13054c… drh 1700 static Stmt eq;
2985120… drh 1701 int isPublic; /* True if pChild is non-private */
d13054c… drh 1702
65aa10f… drh 1703 /* If mlink table entires are already exist for the pmid-to-mid transition,
65aa10f… drh 1704 ** then abort early doing no work.
7f11789… drh 1705 */
65aa10f… drh 1706 db_static_prepare(&eq, "SELECT 1 FROM mlink WHERE mid=:mid AND pmid=:pmid");
65aa10f… drh 1707 db_bind_int(&eq, ":mid", mid);
65aa10f… drh 1708 db_bind_int(&eq, ":pmid", pmid);
d13054c… drh 1709 rc = db_step(&eq);
d13054c… drh 1710 db_reset(&eq);
d13054c… drh 1711 if( rc==SQLITE_ROW ) return;
d0305b3… aku 1712
7f11789… drh 1713 /* Compute the value of the missing pParent or pChild parameter.
6ec2c2e… mistachkin 1714 ** Fetch the baseline check-ins for both.
7f11789… drh 1715 */
d0305b3… aku 1716 assert( pParent==0 || pChild==0 );
d0305b3… aku 1717 if( pParent==0 ){
d13054c… drh 1718 ppOther = &pParent;
65aa10f… drh 1719 otherRid = pmid;
0a55d16… drh 1720 }else{
d13054c… drh 1721 ppOther = &pChild;
65aa10f… drh 1722 otherRid = mid;
0a55d16… drh 1723 }
d13054c… drh 1724 if( (*ppOther = manifest_cache_find(otherRid))==0 ){
0a55d16… drh 1725 content_get(otherRid, &otherContent);
0a55d16… drh 1726 if( blob_size(&otherContent)==0 ) return;
aab9e66… drh 1727 *ppOther = manifest_parse(&otherContent, otherRid, 0);
d13054c… drh 1728 if( *ppOther==0 ) return;
d13054c… drh 1729 }
fda9b15… drh 1730 if( fetch_baseline(pParent, 0) || fetch_baseline(pChild, 0) ){
fda9b15… drh 1731 manifest_destroy(*ppOther);
fda9b15… drh 1732 return;
fda9b15… drh 1733 }
65aa10f… drh 1734 isPublic = !content_is_private(mid);
6ef54df… jan.nijtmans 1735
e2c5353… drh 1736 /* If pParent is not the primary parent of pChild, and the primary
e2c5353… drh 1737 ** parent of pChild is a phantom, then abort this routine without
e2c5353… drh 1738 ** doing any work. The mlink entries will be computed when the
e2c5353… drh 1739 ** primary parent dephantomizes.
e2c5353… drh 1740 */
e2c5353… drh 1741 if( !isPrim && otherRid==mid
e2c5353… drh 1742 && !db_exists("SELECT 1 FROM blob WHERE uuid=%Q AND size>0",
e2c5353… drh 1743 pChild->azParent[0])
e2c5353… drh 1744 ){
e2c5353… drh 1745 manifest_cache_insert(*ppOther);
e2c5353… drh 1746 return;
e2c5353… drh 1747 }
e2c5353… drh 1748
7f11789… drh 1749 /* Try to make the parent manifest a delta from the child, if that
52b35c8… jan.nijtmans 1750 ** is an appropriate thing to do. For a new baseline, make the
db0c512… drh 1751 ** previous baseline a delta from the current baseline.
7f11789… drh 1752 */
d13054c… drh 1753 if( (pParent->zBaseline==0)==(pChild->zBaseline==0) ){
a4047a9… drh 1754 content_deltify(pmid, &mid, 1, 0);
e21ca81… drh 1755 }else if( pChild->zBaseline==0 && pParent->zBaseline!=0 ){
a4047a9… drh 1756 content_deltify(pParent->pBaseline->rid, &mid, 1, 0);
0e87f42… drh 1757 }
0e87f42… drh 1758
feaab7b… drh 1759 /* Remember all children less than a few seconds younger than their parent,
0e87f42… drh 1760 ** as we might want to fudge the times for those children.
0e87f42… drh 1761 */
feaab7b… drh 1762 if( pChild->rDate<pParent->rDate+AGE_FUDGE_WINDOW
feaab7b… drh 1763 && manifest_crosslink_busy
feaab7b… drh 1764 ){
0e87f42… drh 1765 db_multi_exec(
0e87f42… drh 1766 "INSERT OR REPLACE INTO time_fudge VALUES(%d, %.17g, %d, %.17g);",
0e87f42… drh 1767 pParent->rid, pParent->rDate, pChild->rid, pChild->rDate
0e87f42… drh 1768 );
d13054c… drh 1769 }
7f11789… drh 1770
7f11789… drh 1771 /* First look at all files in pChild, ignoring its baseline. This
7f11789… drh 1772 ** is where most of the changes will be found.
52b35c8… jan.nijtmans 1773 */
d13054c… drh 1774 for(i=0, pChildFile=pChild->aFile; i<pChild->nFile; i++, pChildFile++){
7f11789… drh 1775 int mperm = manifest_file_mperm(pChildFile);
d13054c… drh 1776 if( pChildFile->zPrior ){
4bf670e… drh 1777 pParentFile = manifest_file_seek(pParent, pChildFile->zPrior, 0);
d13054c… drh 1778 if( pParentFile ){
7f11789… drh 1779 /* File with name change */
65aa10f… drh 1780 add_one_mlink(pmid, pParentFile->zUuid, mid, pChildFile->zUuid,
65aa10f… drh 1781 pChildFile->zName, pChildFile->zPrior,
65aa10f… drh 1782 isPublic, isPrim, mperm);
7f11789… drh 1783 }else{
7f11789… drh 1784 /* File name changed, but the old name is not found in the parent!
7f11789… drh 1785 ** Treat this like a new file. */
65aa10f… drh 1786 add_one_mlink(pmid, 0, mid, pChildFile->zUuid, pChildFile->zName, 0,
65aa10f… drh 1787 isPublic, isPrim, mperm);
d13054c… drh 1788 }
d13054c… drh 1789 }else{
4bf670e… drh 1790 pParentFile = manifest_file_seek(pParent, pChildFile->zName, 0);
d13054c… drh 1791 if( pParentFile==0 ){
d13054c… drh 1792 if( pChildFile->zUuid ){
7f11789… drh 1793 /* A new file */
65aa10f… drh 1794 add_one_mlink(pmid, 0, mid, pChildFile->zUuid, pChildFile->zName, 0,
65aa10f… drh 1795 isPublic, isPrim, mperm);
d13054c… drh 1796 }
7f11789… drh 1797 }else if( fossil_strcmp(pChildFile->zUuid, pParentFile->zUuid)!=0
7f11789… drh 1798 || manifest_file_mperm(pParentFile)!=mperm ){
7f11789… drh 1799 /* Changes in file content or permissions */
65aa10f… drh 1800 add_one_mlink(pmid, pParentFile->zUuid, mid, pChildFile->zUuid,
65aa10f… drh 1801 pChildFile->zName, 0, isPublic, isPrim, mperm);
d13054c… drh 1802 }
d13054c… drh 1803 }
d13054c… drh 1804 }
d13054c… drh 1805 if( pParent->zBaseline && pChild->zBaseline ){
7f11789… drh 1806 /* Both parent and child are delta manifests. Look for files that
d084043… drh 1807 ** are deleted or modified in the parent but which reappear or revert
d084043… drh 1808 ** to baseline in the child and show such files as being added or changed
d084043… drh 1809 ** in the child. */
d13054c… drh 1810 for(i=0, pParentFile=pParent->aFile; i<pParent->nFile; i++, pParentFile++){
d084043… drh 1811 if( pParentFile->zUuid ){
4bf670e… drh 1812 pChildFile = manifest_file_seek_base(pChild, pParentFile->zName, 0);
d084043… drh 1813 if( pChildFile==0 ){
d084043… drh 1814 /* The child file reverts to baseline. Show this as a change */
4bf670e… drh 1815 pChildFile = manifest_file_seek(pChild, pParentFile->zName, 0);
d084043… drh 1816 if( pChildFile ){
65aa10f… drh 1817 add_one_mlink(pmid, pParentFile->zUuid, mid, pChildFile->zUuid,
65aa10f… drh 1818 pChildFile->zName, 0, isPublic, isPrim,
d084043… drh 1819 manifest_file_mperm(pChildFile));
d084043… drh 1820 }
d084043… drh 1821 }
d084043… drh 1822 }else{
4bf670e… drh 1823 pChildFile = manifest_file_seek(pChild, pParentFile->zName, 0);
d084043… drh 1824 if( pChildFile ){
d084043… drh 1825 /* File resurrected in the child after having been deleted in
d084043… drh 1826 ** the parent. Show this as an added file. */
65aa10f… drh 1827 add_one_mlink(pmid, 0, mid, pChildFile->zUuid, pChildFile->zName, 0,
65aa10f… drh 1828 isPublic, isPrim, manifest_file_mperm(pChildFile));
d084043… drh 1829 }
0ba2c86… drh 1830 }
0ba2c86… drh 1831 }
ccc2c8f… drh 1832 }else if( pChild->zBaseline==0 ){
ccc2c8f… drh 1833 /* pChild is a baseline. Look for files that are present in pParent
ccc2c8f… drh 1834 ** but are missing from pChild and mark them as having been deleted. */
0ba2c86… drh 1835 manifest_file_rewind(pParent);
0ba2c86… drh 1836 while( (pParentFile = manifest_file_next(pParent,0))!=0 ){
4bf670e… drh 1837 pChildFile = manifest_file_seek(pChild, pParentFile->zName, 0);
9147261… joerg 1838 if( pChildFile==0 && pParentFile->zUuid!=0 ){
65aa10f… drh 1839 add_one_mlink(pmid, pParentFile->zUuid, mid, 0, pParentFile->zName, 0,
65aa10f… drh 1840 isPublic, isPrim, 0);
d13054c… drh 1841 }
d13054c… drh 1842 }
d13054c… drh 1843 }
d13054c… drh 1844 manifest_cache_insert(*ppOther);
6ef54df… jan.nijtmans 1845
e2c5353… drh 1846 /* If pParent is the primary parent of pChild, also run this analysis
e2c5353… drh 1847 ** for all merge parents of pChild
e2c5353… drh 1848 */
e2c5353… drh 1849 if( isPrim ){
e2c5353… drh 1850 for(i=1; i<pChild->nParent; i++){
e2c5353… drh 1851 pmid = uuid_to_rid(pChild->azParent[i], 0);
e2c5353… drh 1852 if( pmid<=0 ) continue;
e2c5353… drh 1853 add_mlink(pmid, 0, mid, pChild, 0);
e2c5353… drh 1854 }
e0d8f73… drh 1855 for(i=0; i<pChild->nCherrypick; i++){
e0d8f73… drh 1856 if( pChild->aCherrypick[i].zCPTarget[0]=='+'
e0d8f73… drh 1857 && (pmid = uuid_to_rid(pChild->aCherrypick[i].zCPTarget+1, 0))>0
e0d8f73… drh 1858 ){
e0d8f73… drh 1859 add_mlink(pmid, 0, mid, pChild, 0);
e0d8f73… drh 1860 }
e0d8f73… drh 1861 }
96164bb… drh 1862 }
96164bb… drh 1863 }
96164bb… drh 1864
96164bb… drh 1865 /*
96164bb… drh 1866 ** For a check-in with RID "rid" that has nParent parent check-ins given
fd9b7bd… drh 1867 ** by the hashes in azParent[], create all appropriate plink and mlink table
96164bb… drh 1868 ** entries.
96164bb… drh 1869 **
fd9b7bd… drh 1870 ** The primary parent is the first hash on the azParent[] list.
96164bb… drh 1871 **
96164bb… drh 1872 ** Return the RID of the primary parent.
96164bb… drh 1873 */
96164bb… drh 1874 static int manifest_add_checkin_linkages(
96164bb… drh 1875 int rid, /* The RID of the check-in */
96164bb… drh 1876 Manifest *p, /* Manifest for this check-in */
96164bb… drh 1877 int nParent, /* Number of parents for this check-in */
4cf8dbe… stephan 1878 char * const * azParent /* hashes for each parent */
96164bb… drh 1879 ){
96164bb… drh 1880 int i;
96164bb… drh 1881 int parentid = 0;
96164bb… drh 1882 char zBaseId[30]; /* Baseline manifest RID for deltas. "NULL" otherwise */
96164bb… drh 1883 Stmt q;
e0d8f73… drh 1884 int nLink;
96164bb… drh 1885
96164bb… drh 1886 if( p->zBaseline ){
6ef54df… jan.nijtmans 1887 sqlite3_snprintf(sizeof(zBaseId), zBaseId, "%d",
96164bb… drh 1888 uuid_to_rid(p->zBaseline,1));
96164bb… drh 1889 }else{
96164bb… drh 1890 sqlite3_snprintf(sizeof(zBaseId), zBaseId, "NULL");
96164bb… drh 1891 }
96164bb… drh 1892 for(i=0; i<nParent; i++){
96164bb… drh 1893 int pid = uuid_to_rid(azParent[i], 1);
96164bb… drh 1894 db_multi_exec(
96164bb… drh 1895 "INSERT OR IGNORE INTO plink(pid, cid, isprim, mtime, baseid)"
96164bb… drh 1896 "VALUES(%d, %d, %d, %.17g, %s)",
96164bb… drh 1897 pid, rid, i==0, p->rDate, zBaseId/*safe-for-%s*/);
96164bb… drh 1898 if( i==0 ) parentid = pid;
96164bb… drh 1899 }
96164bb… drh 1900 add_mlink(parentid, 0, rid, p, 1);
e0d8f73… drh 1901 nLink = nParent;
e0d8f73… drh 1902 for(i=0; i<p->nCherrypick; i++){
e0d8f73… drh 1903 if( p->aCherrypick[i].zCPTarget[0]=='+' ) nLink++;
e0d8f73… drh 1904 }
e0d8f73… drh 1905 if( nLink>1 ){
96164bb… drh 1906 /* Change MLINK.PID from 0 to -1 for files that are added by merge. */
96164bb… drh 1907 db_multi_exec(
96164bb… drh 1908 "UPDATE mlink SET pid=-1"
96164bb… drh 1909 " WHERE mid=%d"
96164bb… drh 1910 " AND pid=0"
96164bb… drh 1911 " AND fnid IN "
96164bb… drh 1912 " (SELECT fnid FROM mlink WHERE mid=%d GROUP BY fnid"
96164bb… drh 1913 " HAVING count(*)<%d)",
e0d8f73… drh 1914 rid, rid, nLink
96164bb… drh 1915 );
96164bb… drh 1916 }
96164bb… drh 1917 db_prepare(&q, "SELECT cid, isprim FROM plink WHERE pid=%d", rid);
96164bb… drh 1918 while( db_step(&q)==SQLITE_ROW ){
96164bb… drh 1919 int cid = db_column_int(&q, 0);
96164bb… drh 1920 int isprim = db_column_int(&q, 1);
96164bb… drh 1921 add_mlink(rid, p, cid, 0, isprim);
96164bb… drh 1922 }
96164bb… drh 1923 db_finalize(&q);
96164bb… drh 1924 if( nParent==0 ){
96164bb… drh 1925 /* For root files (files without parents) add mlink entries
96164bb… drh 1926 ** showing all content as new. */
96164bb… drh 1927 int isPublic = !content_is_private(rid);
96164bb… drh 1928 for(i=0; i<p->nFile; i++){
96164bb… drh 1929 add_one_mlink(0, 0, rid, p->aFile[i].zUuid, p->aFile[i].zName, 0,
96164bb… drh 1930 isPublic, 1, manifest_file_mperm(&p->aFile[i]));
96164bb… drh 1931 }
96164bb… drh 1932 }
96164bb… drh 1933 return parentid;
96164bb… drh 1934 }
96164bb… drh 1935
96164bb… drh 1936 /*
bc36fdc… danield 1937 ** There exists a "parent" tag against check-in rid that has value zValue.
fd9b7bd… drh 1938 ** If value is well-formed (meaning that it is a list of hashes), then use
96164bb… drh 1939 ** zValue to reparent check-in rid.
96164bb… drh 1940 */
96164bb… drh 1941 void manifest_reparent_checkin(int rid, const char *zValue){
fd9b7bd… drh 1942 int nParent = 0;
96164bb… drh 1943 char *zCopy = 0;
96164bb… drh 1944 char **azParent = 0;
96164bb… drh 1945 Manifest *p = 0;
fd9b7bd… drh 1946 int i, j;
96164bb… drh 1947 int n = (int)strlen(zValue);
fd9b7bd… drh 1948 int mxParent = (n+1)/(HNAME_MIN+1);
fd9b7bd… drh 1949
fd9b7bd… drh 1950 if( mxParent<1 ) return;
96164bb… drh 1951 zCopy = fossil_strdup(zValue);
fd9b7bd… drh 1952 azParent = fossil_malloc( sizeof(azParent[0])*mxParent );
fd9b7bd… drh 1953 for(nParent=0, i=0; zCopy[i]; i++){
fd9b7bd… drh 1954 char *z = &zCopy[i];
fd9b7bd… drh 1955 azParent[nParent++] = z;
fd9b7bd… drh 1956 if( nParent>mxParent ) goto reparent_abort;
fd9b7bd… drh 1957 for(j=HNAME_MIN; z[j]>' '; j++){}
fd9b7bd… drh 1958 if( !hname_validate(z, j) ) goto reparent_abort;
fd9b7bd… drh 1959 if( z[j]==0 ) break;
fd9b7bd… drh 1960 z[j] = 0;
fd9b7bd… drh 1961 i += j;
fd9b7bd… drh 1962 }
2bdbbc8… drh 1963 p = manifest_get(rid, CFTYPE_MANIFEST, 0);
96164bb… drh 1964 if( p!=0 ){
96164bb… drh 1965 db_multi_exec(
96164bb… drh 1966 "DELETE FROM plink WHERE cid=%d;"
96164bb… drh 1967 "DELETE FROM mlink WHERE mid=%d;",
96164bb… drh 1968 rid, rid
96164bb… drh 1969 );
96164bb… drh 1970 manifest_add_checkin_linkages(rid,p,nParent,azParent);
2bdbbc8… drh 1971 manifest_destroy(p);
e2c5353… drh 1972 }
fd9b7bd… drh 1973 reparent_abort:
96164bb… drh 1974 fossil_free(azParent);
96164bb… drh 1975 fossil_free(zCopy);
80c42a3… drh 1976 }
95f5520… drh 1977
95f5520… drh 1978 /*
95f5520… drh 1979 ** Setup to do multiple manifest_crosslink() calls.
96164bb… drh 1980 **
96164bb… drh 1981 ** This routine creates TEMP tables for holding information for
96164bb… drh 1982 ** processing that must be deferred until all artifacts have been
96164bb… drh 1983 ** seen at least once. The deferred processing is accomplished
96164bb… drh 1984 ** by the call to manifest_crosslink_end().
95f5520… drh 1985 */
95f5520… drh 1986 void manifest_crosslink_begin(void){
95f5520… drh 1987 assert( manifest_crosslink_busy==0 );
95f5520… drh 1988 manifest_crosslink_busy = 1;
169ba8d… drh 1989 manifest_create_event_triggers();
e1962ef… drh 1990 db_begin_transaction();
0e87f42… drh 1991 db_multi_exec(
ae1dac8… drh 1992 "CREATE TEMP TABLE pending_xlink(id TEXT PRIMARY KEY)WITHOUT ROWID;"
0e87f42… drh 1993 "CREATE TEMP TABLE time_fudge("
feaab7b… drh 1994 " mid INTEGER PRIMARY KEY," /* The rid of a manifest */
feaab7b… drh 1995 " m1 REAL," /* The timestamp on mid */
feaab7b… drh 1996 " cid INTEGER," /* A child or mid */
feaab7b… drh 1997 " m2 REAL" /* Timestamp on the child */
0e87f42… drh 1998 ");"
ae1dac8… drh 1999 );
ae1dac8… drh 2000 }
ae1dac8… drh 2001
ae1dac8… drh 2002 /*
ae1dac8… drh 2003 ** Add a new entry to the pending_xlink table.
ae1dac8… drh 2004 */
ae1dac8… drh 2005 static void add_pending_crosslink(char cType, const char *zId){
ae1dac8… drh 2006 assert( manifest_crosslink_busy==1 );
ae1dac8… drh 2007 db_multi_exec(
ae1dac8… drh 2008 "INSERT OR IGNORE INTO pending_xlink VALUES('%c%q')",
ae1dac8… drh 2009 cType, zId
6ec2c2e… mistachkin 2010 );
6ec2c2e… mistachkin 2011 }
6ec2c2e… mistachkin 2012
feaab7b… drh 2013 #if INTERFACE
6ec2c2e… mistachkin 2014 /* Timestamps might be adjusted slightly to ensure that check-ins appear
feaab7b… drh 2015 ** on the timeline in chronological order. This is the maximum amount
feaab7b… drh 2016 ** of the adjustment window, in days.
feaab7b… drh 2017 */
feaab7b… drh 2018 #define AGE_FUDGE_WINDOW (2.0/86400.0) /* 2 seconds */
feaab7b… drh 2019
feaab7b… drh 2020 /* This is increment (in days) by which timestamps are adjusted for
feaab7b… drh 2021 ** use on the timeline.
feaab7b… drh 2022 */
feaab7b… drh 2023 #define AGE_ADJUST_INCREMENT (25.0/86400000.0) /* 25 milliseconds */
feaab7b… drh 2024
feaab7b… drh 2025 #endif /* LOCAL_INTERFACE */
feaab7b… drh 2026
feaab7b… drh 2027 /*
95f5520… drh 2028 ** Finish up a sequence of manifest_crosslink calls.
95f5520… drh 2029 */
42316a1… mistachkin 2030 int manifest_crosslink_end(int flags){
0e87f42… drh 2031 Stmt q, u;
0e87f42… drh 2032 int i;
42316a1… mistachkin 2033 int rc = TH_OK;
42316a1… mistachkin 2034 int permitHooks = (flags & MC_PERMIT_HOOKS);
42316a1… mistachkin 2035 const char *zScript = 0;
95f5520… drh 2036 assert( manifest_crosslink_busy==1 );
42316a1… mistachkin 2037 if( permitHooks ){
42316a1… mistachkin 2038 rc = xfer_run_common_script();
42316a1… mistachkin 2039 if( rc==TH_OK ){
42316a1… mistachkin 2040 zScript = xfer_ticket_code();
42316a1… mistachkin 2041 }
42316a1… mistachkin 2042 }
6ef54df… jan.nijtmans 2043 db_prepare(&q,
96164bb… drh 2044 "SELECT rid, value FROM tagxref"
96164bb… drh 2045 " WHERE tagid=%d AND tagtype=1",
96164bb… drh 2046 TAG_PARENT
96164bb… drh 2047 );
96164bb… drh 2048 while( db_step(&q)==SQLITE_ROW ){
96164bb… drh 2049 int rid = db_column_int(&q,0);
96164bb… drh 2050 const char *zValue = db_column_text(&q,1);
96164bb… drh 2051 manifest_reparent_checkin(rid, zValue);
96164bb… drh 2052 }
96164bb… drh 2053 db_finalize(&q);
ae1dac8… drh 2054 db_prepare(&q, "SELECT id FROM pending_xlink");
95f5520… drh 2055 while( db_step(&q)==SQLITE_ROW ){
ae1dac8… drh 2056 const char *zId = db_column_text(&q, 0);
ae1dac8… drh 2057 char cType;
ae1dac8… drh 2058 if( zId==0 || zId[0]==0 ) continue;
ae1dac8… drh 2059 cType = zId[0];
ae1dac8… drh 2060 zId++;
ae1dac8… drh 2061 if( cType=='t' ){
ae1dac8… drh 2062 ticket_rebuild_entry(zId);
ae1dac8… drh 2063 if( permitHooks && rc==TH_OK ){
ae1dac8… drh 2064 rc = xfer_run_script(zScript, zId, 0);
ae1dac8… drh 2065 }
ae1dac8… drh 2066 }else if( cType=='w' ){
ae1dac8… drh 2067 backlink_wiki_refresh(zId);
42316a1… mistachkin 2068 }
0e87f42… drh 2069 }
0e87f42… drh 2070 db_finalize(&q);
ae1dac8… drh 2071 db_multi_exec("DROP TABLE pending_xlink");
0e87f42… drh 2072
feaab7b… drh 2073 /* If multiple check-ins happen close together in time, adjust their
feaab7b… drh 2074 ** times by a few milliseconds to make sure they appear in chronological
feaab7b… drh 2075 ** order.
feaab7b… drh 2076 */
feaab7b… drh 2077 db_prepare(&q,
feaab7b… drh 2078 "UPDATE time_fudge SET m1=m2-:incr WHERE m1>=m2 AND m1<m2+:window"
feaab7b… drh 2079 );
feaab7b… drh 2080 db_bind_double(&q, ":incr", AGE_ADJUST_INCREMENT);
feaab7b… drh 2081 db_bind_double(&q, ":window", AGE_FUDGE_WINDOW);
feaab7b… drh 2082 db_prepare(&u,
feaab7b… drh 2083 "UPDATE time_fudge SET m2="
feaab7b… drh 2084 "(SELECT x.m1 FROM time_fudge AS x WHERE x.mid=time_fudge.cid)"
feaab7b… drh 2085 );
0e87f42… drh 2086 for(i=0; i<30; i++){
0e87f42… drh 2087 db_step(&q);
0e87f42… drh 2088 db_reset(&q);
0e87f42… drh 2089 if( sqlite3_changes(g.db)==0 ) break;
0e87f42… drh 2090 db_step(&u);
0e87f42… drh 2091 db_reset(&u);
95f5520… drh 2092 }
95f5520… drh 2093 db_finalize(&q);
0e87f42… drh 2094 db_finalize(&u);
b4dffda… drh 2095 if( db_exists("SELECT 1 FROM time_fudge") ){
b4dffda… drh 2096 db_multi_exec(
b4dffda… drh 2097 "UPDATE event SET mtime=(SELECT m1 FROM time_fudge WHERE mid=objid)"
e08f9c0… drh 2098 " WHERE objid IN (SELECT mid FROM time_fudge)"
e08f9c0… drh 2099 " AND (mtime=omtime OR omtime IS NULL)"
b4dffda… drh 2100 );
b4dffda… drh 2101 }
b4dffda… drh 2102 db_multi_exec("DROP TABLE time_fudge;");
0e87f42… drh 2103
95f5520… drh 2104 db_end_transaction(0);
95f5520… drh 2105 manifest_crosslink_busy = 0;
42316a1… mistachkin 2106 return ( rc!=TH_ERROR );
42316a1… mistachkin 2107 }
ae1dac8… drh 2108
ae1dac8… drh 2109 /*
169ba8d… drh 2110 ** Activate EVENT triggers if they do not already exist.
169ba8d… drh 2111 */
169ba8d… drh 2112 void manifest_create_event_triggers(void){
169ba8d… drh 2113 if( manifest_event_triggers_are_enabled ){
169ba8d… drh 2114 return; /* Triggers already exists. No-op. */
169ba8d… drh 2115 }
169ba8d… drh 2116 alert_create_trigger();
275da70… danield 2117 manifest_event_triggers_are_enabled = 1;
169ba8d… drh 2118 }
169ba8d… drh 2119
169ba8d… drh 2120 /*
169ba8d… drh 2121 ** Disable manifest event triggers. Drop them if they exist, but mark
169ba8d… drh 2122 ** them has having been created so that they won't be recreated. This
169ba8d… drh 2123 ** is used during "rebuild" to prevent triggers from firing then.
169ba8d… drh 2124 */
169ba8d… drh 2125 void manifest_disable_event_triggers(void){
169ba8d… drh 2126 alert_drop_trigger();
169ba8d… drh 2127 manifest_event_triggers_are_enabled = 1;
169ba8d… drh 2128 }
169ba8d… drh 2129
169ba8d… drh 2130
169ba8d… drh 2131 /*
95f5520… drh 2132 ** Make an entry in the event table for a ticket change artifact.
95f5520… drh 2133 */
95f5520… drh 2134 void manifest_ticket_event(
95f5520… drh 2135 int rid, /* Artifact ID of the change ticket artifact */
95f5520… drh 2136 const Manifest *pManifest, /* Parsed content of the artifact */
df3e34c… drh 2137 int isNew, /* True if this is the first event */
df3e34c… drh 2138 int tktTagId /* Ticket tag ID */
95f5520… drh 2139 ){
95f5520… drh 2140 int i;
95f5520… drh 2141 char *zTitle;
95f5520… drh 2142 Blob comment;
df3e34c… drh 2143 Blob brief;
95f5520… drh 2144 char *zNewStatus = 0;
95f5520… drh 2145 static char *zTitleExpr = 0;
95f5520… drh 2146 static char *zStatusColumn = 0;
95f5520… drh 2147 static int once = 1;
95f5520… drh 2148
95f5520… drh 2149 blob_zero(&comment);
df3e34c… drh 2150 blob_zero(&brief);
95f5520… drh 2151 if( once ){
95f5520… drh 2152 once = 0;
95f5520… drh 2153 zTitleExpr = db_get("ticket-title-expr", "title");
95f5520… drh 2154 zStatusColumn = db_get("ticket-status-column", "status");
95f5520… drh 2155 }
52b35c8… jan.nijtmans 2156 zTitle = db_text("unknown",
49b0ff1… drh 2157 "SELECT \"%w\" FROM ticket WHERE tkt_uuid=%Q",
95f5520… drh 2158 zTitleExpr, pManifest->zTicketUuid
95f5520… drh 2159 );
95f5520… drh 2160 if( !isNew ){
95f5520… drh 2161 for(i=0; i<pManifest->nField; i++){
31c52c7… drh 2162 if( fossil_strcmp(pManifest->aField[i].zName, zStatusColumn)==0 ){
95f5520… drh 2163 zNewStatus = pManifest->aField[i].zValue;
95f5520… drh 2164 }
95f5520… drh 2165 }
95f5520… drh 2166 if( zNewStatus ){
d93f5ee… jan.nijtmans 2167 blob_appendf(&comment, "%h ticket [%!S|%S]: <i>%h</i>",
a16236e… jan.nijtmans 2168 zNewStatus, pManifest->zTicketUuid, pManifest->zTicketUuid, zTitle
95f5520… drh 2169 );
95f5520… drh 2170 if( pManifest->nField>1 ){
95f5520… drh 2171 blob_appendf(&comment, " plus %d other change%s",
95f5520… drh 2172 pManifest->nField-1, pManifest->nField==2 ? "" : "s");
95f5520… drh 2173 }
d93f5ee… jan.nijtmans 2174 blob_appendf(&brief, "%h ticket [%!S|%S].",
a16236e… jan.nijtmans 2175 zNewStatus, pManifest->zTicketUuid, pManifest->zTicketUuid);
a16236e… jan.nijtmans 2176 }else{
a16236e… jan.nijtmans 2177 zNewStatus = db_text("unknown",
49b0ff1… drh 2178 "SELECT \"%w\" FROM ticket WHERE tkt_uuid=%Q",
a16236e… jan.nijtmans 2179 zStatusColumn, pManifest->zTicketUuid
a16236e… jan.nijtmans 2180 );
d93f5ee… jan.nijtmans 2181 blob_appendf(&comment, "Ticket [%!S|%S] <i>%h</i> status still %h with "
a16236e… jan.nijtmans 2182 "%d other change%s",
a16236e… jan.nijtmans 2183 pManifest->zTicketUuid, pManifest->zTicketUuid, zTitle, zNewStatus,
a16236e… jan.nijtmans 2184 pManifest->nField, pManifest->nField==1 ? "" : "s"
a16236e… jan.nijtmans 2185 );
b7ff537… stephan 2186 fossil_free(zNewStatus);
d93f5ee… jan.nijtmans 2187 blob_appendf(&brief, "Ticket [%!S|%S]: %d change%s",
a16236e… jan.nijtmans 2188 pManifest->zTicketUuid, pManifest->zTicketUuid, pManifest->nField,
a16236e… jan.nijtmans 2189 pManifest->nField==1 ? "" : "s"
a16236e… jan.nijtmans 2190 );
a16236e… jan.nijtmans 2191 }
a16236e… jan.nijtmans 2192 }else{
d93f5ee… jan.nijtmans 2193 blob_appendf(&comment, "New ticket [%!S|%S] <i>%h</i>.",
a16236e… jan.nijtmans 2194 pManifest->zTicketUuid, pManifest->zTicketUuid, zTitle
a16236e… jan.nijtmans 2195 );
d93f5ee… jan.nijtmans 2196 blob_appendf(&brief, "New ticket [%!S|%S].", pManifest->zTicketUuid,
a16236e… jan.nijtmans 2197 pManifest->zTicketUuid);
a16236e… jan.nijtmans 2198 }
b7ff537… stephan 2199 fossil_free(zTitle);
169ba8d… drh 2200 manifest_create_event_triggers();
fb040bb… drh 2201 if( db_exists("SELECT 1 FROM event WHERE type='t' AND objid=%d", rid) ){
fb040bb… drh 2202 /* The ticket_rebuild_entry() function redoes all of the event entries
fb040bb… drh 2203 ** for a ticket whenever a new event appears. Be careful to only UPDATE
fb040bb… drh 2204 ** existing events, so that they do not get turned into alerts by
fb040bb… drh 2205 ** the alert trigger. */
fb040bb… drh 2206 db_multi_exec(
fb040bb… drh 2207 "UPDATE event SET tagid=%d, mtime=%.17g, user=%Q, comment=%Q, brief=%Q"
fb040bb… drh 2208 " WHERE objid=%d",
fb040bb… drh 2209 tktTagId, pManifest->rDate, pManifest->zUser,
fb040bb… drh 2210 blob_str(&comment), blob_str(&brief), rid
fb040bb… drh 2211 );
fb040bb… drh 2212 }else{
fb040bb… drh 2213 db_multi_exec(
fb040bb… drh 2214 "REPLACE INTO event(type,tagid,mtime,objid,user,comment,brief)"
fb040bb… drh 2215 "VALUES('t',%d,%.17g,%d,%Q,%Q,%Q)",
fb040bb… drh 2216 tktTagId, pManifest->rDate, rid, pManifest->zUser,
fb040bb… drh 2217 blob_str(&comment), blob_str(&brief)
fb040bb… drh 2218 );
fb040bb… drh 2219 }
a16236e… jan.nijtmans 2220 blob_reset(&comment);
a16236e… jan.nijtmans 2221 blob_reset(&brief);
1dd4be8… drh 2222 }
1dd4be8… drh 2223
1dd4be8… drh 2224 /*
1dd4be8… drh 2225 ** Add an extra line of text to the end of a manifest to prevent it being
1dd4be8… drh 2226 ** recognized as a valid manifest.
1dd4be8… drh 2227 **
1dd4be8… drh 2228 ** This routine is called prior to writing out the text of a manifest as
1dd4be8… drh 2229 ** the "manifest" file in the root of a repository when
1dd4be8… drh 2230 ** "fossil setting manifest on" is enabled. That way, if the files of
1dd4be8… drh 2231 ** the project are imported into a different Fossil project, the manifest
1dd4be8… drh 2232 ** file will not be interpreted as a control artifact in that other project.
1dd4be8… drh 2233 **
1dd4be8… drh 2234 ** Normally it is sufficient to simply append the extra line of text.
1dd4be8… drh 2235 ** However, if the manifest is PGP signed then the extra line has to be
1dd4be8… drh 2236 ** inserted before the PGP signature (thus invalidating the signature).
1dd4be8… drh 2237 */
2777682… drh 2238 void sterilize_manifest(Blob *p, int eType){
1dd4be8… drh 2239 char *z, *zOrig;
1dd4be8… drh 2240 int n, nOrig;
1dd4be8… drh 2241 static const char zExtraLine[] =
2777682… drh 2242 "# Remove this line to create a well-formed Fossil %s.\n";
2777682… drh 2243 const char *zType = eType==CFTYPE_MANIFEST ? "manifest" : "control artifact";
1dd4be8… drh 2244
1dd4be8… drh 2245 z = zOrig = blob_materialize(p);
1dd4be8… drh 2246 n = nOrig = blob_size(p);
0f7eff9… drh 2247 remove_pgp_signature((const char **)&z, &n);
1dd4be8… drh 2248 if( z==zOrig ){
2777682… drh 2249 blob_appendf(p, zExtraLine/*works-like:"%s"*/, zType);
1dd4be8… drh 2250 }else{
1dd4be8… drh 2251 int iEnd;
1dd4be8… drh 2252 Blob copy;
1dd4be8… drh 2253 memcpy(&copy, p, sizeof(copy));
1dd4be8… drh 2254 blob_init(p, 0, 0);
1dd4be8… drh 2255 iEnd = (int)(&z[n] - zOrig);
1dd4be8… drh 2256 blob_append(p, zOrig, iEnd);
2777682… drh 2257 blob_appendf(p, zExtraLine/*works-like:"%s"*/, zType);
1dd4be8… drh 2258 blob_append(p, &zOrig[iEnd], -1);
1dd4be8… drh 2259 blob_zero(&copy);
1dd4be8… drh 2260 }
5b1a685… jan.nijtmans 2261 }
5b1a685… jan.nijtmans 2262
5b1a685… jan.nijtmans 2263 /*
5b1a685… jan.nijtmans 2264 ** This is the comparison function used to sort the tag array.
5b1a685… jan.nijtmans 2265 */
5b1a685… jan.nijtmans 2266 static int tag_compare(const void *a, const void *b){
5b1a685… jan.nijtmans 2267 struct TagType *pA = (struct TagType*)a;
5b1a685… jan.nijtmans 2268 struct TagType *pB = (struct TagType*)b;
5b1a685… jan.nijtmans 2269 int c;
5b1a685… jan.nijtmans 2270 c = fossil_strcmp(pA->zUuid, pB->zUuid);
5b1a685… jan.nijtmans 2271 if( c==0 ){
5b1a685… jan.nijtmans 2272 c = fossil_strcmp(pA->zName, pB->zName);
5b1a685… jan.nijtmans 2273 }
5b1a685… jan.nijtmans 2274 return c;
2777682… drh 2275 }
2777682… drh 2276
2777682… drh 2277 /*
7c64fe0… drh 2278 ** Inserts plink entries for FORUM, WIKI, and TECHNOTE manifests. May
7c64fe0… drh 2279 ** assert for other manifest types. If a parent entry exists, it also
7c64fe0… drh 2280 ** propagates any tags for that parent. This is a no-op if
7c64fe0… drh 2281 ** p->nParent==0.
7c64fe0… drh 2282 */
7c64fe0… drh 2283 static void manifest_add_fwt_plink(int rid, Manifest *p){
7c64fe0… drh 2284 int i;
7c64fe0… drh 2285 int parentId = 0;
7c64fe0… drh 2286 assert(p->type==CFTYPE_WIKI ||
7c64fe0… drh 2287 p->type==CFTYPE_FORUM ||
7c64fe0… drh 2288 p->type==CFTYPE_EVENT);
7c64fe0… drh 2289 for(i=0; i<p->nParent; ++i){
7c64fe0… drh 2290 int const pid = uuid_to_rid(p->azParent[i], 1);
7c64fe0… drh 2291 if(0==i){
7c64fe0… drh 2292 parentId = pid;
7c64fe0… drh 2293 }
7c64fe0… drh 2294 db_multi_exec(
7c64fe0… drh 2295 "INSERT OR IGNORE INTO plink"
7c64fe0… drh 2296 "(pid, cid, isprim, mtime, baseid)"
7c64fe0… drh 2297 "VALUES(%d, %d, %d, %.17g, NULL)",
7c64fe0… drh 2298 pid, rid, i==0, p->rDate);
7c64fe0… drh 2299 }
7c64fe0… drh 2300 if(parentId){
7c64fe0… drh 2301 tag_propagate_all(parentId);
7c64fe0… drh 2302 }
7c64fe0… drh 2303 }
7c64fe0… drh 2304
7c64fe0… drh 2305 /*
243e02b… drh 2306 ** Scan artifact rid/pContent to see if it is a control artifact of
5e28feb… drh 2307 ** any type:
243e02b… drh 2308 **
243e02b… drh 2309 ** * Manifest
243e02b… drh 2310 ** * Control
243e02b… drh 2311 ** * Wiki Page
243e02b… drh 2312 ** * Ticket Change
243e02b… drh 2313 ** * Cluster
d96c4a4… drh 2314 ** * Attachment
d96c4a4… drh 2315 ** * Event
5e28feb… drh 2316 ** * Forum post
243e02b… drh 2317 **
243e02b… drh 2318 ** If the input is a control artifact, then make appropriate entries
243e02b… drh 2319 ** in the auxiliary tables of the database in order to crosslink the
243e02b… drh 2320 ** artifact.
243e02b… drh 2321 **
52b35c8… jan.nijtmans 2322 ** If global variable g.xlinkClusterOnly is true, then ignore all
243e02b… drh 2323 ** control artifacts other than clusters.
d22519e… drh 2324 **
d22519e… drh 2325 ** This routine always resets the pContent blob before returning.
243e02b… drh 2326 **
243e02b… drh 2327 ** Historical note: This routine original processed manifests only.
243e02b… drh 2328 ** Processing for other control artifacts was added later. The name
243e02b… drh 2329 ** of the routine, "manifest_crosslink", and the name of this source
243e02b… drh 2330 ** file, is a legacy of its original use.
243e02b… drh 2331 */
1311841… jan.nijtmans 2332 int manifest_crosslink(int rid, Blob *pContent, int flags){
42316a1… mistachkin 2333 int i, rc = TH_OK;
d13054c… drh 2334 Manifest *p;
d0305b3… aku 2335 int parentid = 0;
42316a1… mistachkin 2336 int permitHooks = (flags & MC_PERMIT_HOOKS);
1311841… jan.nijtmans 2337 const char *zScript = 0;
5fabb7c… stephan 2338 char *zUuid = 0;
d0305b3… aku 2339
e08f9c0… drh 2340 if( g.fSqlTrace ){
e08f9c0… drh 2341 fossil_trace("-- manifest_crosslink(%d)\n", rid);
e08f9c0… drh 2342 }
169ba8d… drh 2343 manifest_create_event_triggers();
d13054c… drh 2344 if( (p = manifest_cache_find(rid))!=0 ){
0a55d16… drh 2345 blob_reset(pContent);
aab9e66… drh 2346 }else if( (p = manifest_parse(pContent, rid, 0))==0 ){
d22519e… drh 2347 assert( blob_is_reset(pContent) || pContent==0 );
e9bccd1… mistachkin 2348 if( (flags & MC_NO_ERRORS)==0 ){
4cf8dbe… stephan 2349 char * zErrUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d",rid);
4cf8dbe… stephan 2350 fossil_error(1, "syntax error in manifest [%S]", zErrUuid);
4cf8dbe… stephan 2351 fossil_free(zErrUuid);
e9bccd1… mistachkin 2352 }
d13054c… drh 2353 return 0;
d13054c… drh 2354 }
d13054c… drh 2355 if( g.xlinkClusterOnly && p->type!=CFTYPE_CLUSTER ){
d13054c… drh 2356 manifest_destroy(p);
d22519e… drh 2357 assert( blob_is_reset(pContent) );
bf3db3d… drh 2358 if( (flags & MC_NO_ERRORS)==0 ) fossil_error(1, "no manifest");
fda9b15… drh 2359 return 0;
fda9b15… drh 2360 }
fda9b15… drh 2361 if( p->type==CFTYPE_MANIFEST && fetch_baseline(p, 0) ){
fda9b15… drh 2362 manifest_destroy(p);
d22519e… drh 2363 assert( blob_is_reset(pContent) );
bf3db3d… drh 2364 if( (flags & MC_NO_ERRORS)==0 ){
d93f5ee… jan.nijtmans 2365 fossil_error(1, "cannot fetch baseline for manifest [%S]",
e9bccd1… mistachkin 2366 db_text(0, "SELECT uuid FROM blob WHERE rid=%d",rid));
bf3db3d… drh 2367 }
d0305b3… aku 2368 return 0;
d0305b3… aku 2369 }
e1962ef… drh 2370 db_begin_transaction();
d13054c… drh 2371 if( p->type==CFTYPE_MANIFEST ){
42316a1… mistachkin 2372 if( permitHooks ){
42316a1… mistachkin 2373 zScript = xfer_commit_code();
5fabb7c… stephan 2374 zUuid = rid_to_uuid(rid);
1c74e91… drh 2375 }
1c74e91… drh 2376 if( p->nCherrypick && db_table_exists("repository","cherrypick") ){
1c74e91… drh 2377 int i;
1c74e91… drh 2378 for(i=0; i<p->nCherrypick; i++){
1c74e91… drh 2379 db_multi_exec(
1c74e91… drh 2380 "REPLACE INTO cherrypick(parentid,childid,isExclude)"
1c74e91… drh 2381 " SELECT rid, %d, %d FROM blob WHERE uuid=%Q",
1c74e91… drh 2382 rid, p->aCherrypick[i].zCPTarget[0]=='-',
1c74e91… drh 2383 p->aCherrypick[i].zCPTarget+1
1c74e91… drh 2384 );
1c74e91… drh 2385 }
cdab54e… mistachkin 2386 }
d0305b3… aku 2387 if( !db_exists("SELECT 1 FROM mlink WHERE mid=%d", rid) ){
7c2577b… drh 2388 char *zCom;
96164bb… drh 2389 parentid = manifest_add_checkin_linkages(rid,p,p->nParent,p->azParent);
a00a140… drh 2390 search_doc_touch('c', rid, 0);
169ba8d… drh 2391 assert( manifest_event_triggers_are_enabled );
8a3dc1a… drh 2392 zCom = db_text(0,
7aa9a2e… drh 2393 "REPLACE INTO event(type,mtime,objid,user,comment,"
1d83ecc… drh 2394 "bgcolor,euser,ecomment,omtime)"
ac03d43… drh 2395 "VALUES('ci',"
ac03d43… drh 2396 " coalesce("
ac03d43… drh 2397 " (SELECT julianday(value) FROM tagxref WHERE tagid=%d AND rid=%d),"
ac03d43… drh 2398 " %.17g"
ac03d43… drh 2399 " ),"
ac03d43… drh 2400 " %d,%Q,%Q,"
ac03d43… drh 2401 " (SELECT value FROM tagxref WHERE tagid=%d AND rid=%d AND tagtype>0),"
d0305b3… aku 2402 " (SELECT value FROM tagxref WHERE tagid=%d AND rid=%d),"
8a3dc1a… drh 2403 " (SELECT value FROM tagxref WHERE tagid=%d AND rid=%d),%.17g)"
8a3dc1a… drh 2404 "RETURNING coalesce(ecomment,comment);",
d13054c… drh 2405 TAG_DATE, rid, p->rDate,
52b35c8… jan.nijtmans 2406 rid, p->zUser, p->zComment,
d0305b3… aku 2407 TAG_BGCOLOR, rid,
d0305b3… aku 2408 TAG_USER, rid,
1d83ecc… drh 2409 TAG_COMMENT, rid, p->rDate
d0305b3… aku 2410 );
6fb642c… george 2411 backlink_extract(zCom, MT_NONE, rid, BKLNK_COMMENT, p->rDate, 1);
b7ff537… stephan 2412 fossil_free(zCom);
d13054c… drh 2413
d13054c… drh 2414 /* If this is a delta-manifest, record the fact that this repository
d13054c… drh 2415 ** contains delta manifests, to free the "commit" logic to generate
d13054c… drh 2416 ** new delta manifests.
d13054c… drh 2417 */
d13054c… drh 2418 if( p->zBaseline!=0 ){
18d762a… jan.nijtmans 2419 static int once = 1;
18d762a… jan.nijtmans 2420 if( once ){
d13054c… drh 2421 db_set_int("seen-delta-manifest", 1, 0);
d13054c… drh 2422 once = 0;
d13054c… drh 2423 }
d13054c… drh 2424 }
d0305b3… aku 2425 }
d0305b3… aku 2426 }
d13054c… drh 2427 if( p->type==CFTYPE_CLUSTER ){
fda9b15… drh 2428 static Stmt del1;
d13054c… drh 2429 tag_insert("cluster", 1, 0, rid, p->rDate, rid);
fda9b15… drh 2430 db_static_prepare(&del1, "DELETE FROM unclustered WHERE rid=:rid");
d13054c… drh 2431 for(i=0; i<p->nCChild; i++){
d0305b3… aku 2432 int mid;
d13054c… drh 2433 mid = uuid_to_rid(p->azCChild[i], 1);
d0305b3… aku 2434 if( mid>0 ){
fda9b15… drh 2435 db_bind_int(&del1, ":rid", mid);
fda9b15… drh 2436 db_step(&del1);
fda9b15… drh 2437 db_reset(&del1);
d0305b3… aku 2438 }
d0305b3… aku 2439 }
d0305b3… aku 2440 }
d13054c… drh 2441 if( p->type==CFTYPE_CONTROL
d13054c… drh 2442 || p->type==CFTYPE_MANIFEST
d13054c… drh 2443 || p->type==CFTYPE_EVENT
d96c4a4… drh 2444 ){
d13054c… drh 2445 for(i=0; i<p->nTag; i++){
d0305b3… aku 2446 int tid;
d0305b3… aku 2447 int type;
d13054c… drh 2448 if( p->aTag[i].zUuid ){
d13054c… drh 2449 tid = uuid_to_rid(p->aTag[i].zUuid, 1);
0afb5e8… drh 2450 }else{
0afb5e8… drh 2451 tid = rid;
0afb5e8… drh 2452 }
a48474b… drh 2453 if( tid ){
d13054c… drh 2454 switch( p->aTag[i].zName[0] ){
db0c512… drh 2455 case '-': type = 0; break; /* Cancel prior occurrences */
08db9e1… drh 2456 case '+': type = 1; break; /* Apply to target only */
08db9e1… drh 2457 case '*': type = 2; break; /* Propagate to descendants */
a48474b… drh 2458 default:
1311841… jan.nijtmans 2459 fossil_error(1, "unknown tag type in manifest: %s", p->aTag);
4cf8dbe… stephan 2460 manifest_destroy(p);
a48474b… drh 2461 return 0;
a48474b… drh 2462 }
52b35c8… jan.nijtmans 2463 tag_insert(&p->aTag[i].zName[1], type, p->aTag[i].zValue,
d13054c… drh 2464 rid, p->rDate, tid);
a48474b… drh 2465 }
d0305b3… aku 2466 }
d0305b3… aku 2467 if( parentid ){
d0305b3… aku 2468 tag_propagate_all(parentid);
d0305b3… aku 2469 }
7c64fe0… drh 2470 }
7c64fe0… drh 2471 if(p->type==CFTYPE_WIKI || p->type==CFTYPE_FORUM
7c64fe0… drh 2472 || p->type==CFTYPE_EVENT){
7c64fe0… drh 2473 manifest_add_fwt_plink(rid, p);
b8c1d42… stephan 2474 }
045deb2… drh 2475 if( p->type==CFTYPE_WIKI ){
045deb2… drh 2476 char *zTag = mprintf("wiki-%s", p->zWikiTitle);
ecb7053… stephan 2477 int prior = 0;
1a9b1c4… drh 2478 char cPrefix;
045deb2… drh 2479 int nWiki;
045deb2… drh 2480 char zLength[40];
1a9b1c4… drh 2481
045deb2… drh 2482 while( fossil_isspace(p->zWiki[0]) ) p->zWiki++;
045deb2… drh 2483 nWiki = strlen(p->zWiki);
045deb2… drh 2484 sqlite3_snprintf(sizeof(zLength), zLength, "%d", nWiki);
045deb2… drh 2485 tag_insert(zTag, 1, zLength, rid, p->rDate, rid);
045deb2… drh 2486 fossil_free(zTag);
ecb7053… stephan 2487 if(p->nParent){
ecb7053… stephan 2488 prior = fast_uuid_to_rid(p->azParent[0]);
ecb7053… stephan 2489 }
045deb2… drh 2490 if( prior ){
a4047a9… drh 2491 content_deltify(prior, &rid, 1, 0);
045deb2… drh 2492 }
ae1dac8… drh 2493 if( nWiki<=0 ){
1a9b1c4… drh 2494 cPrefix = '-';
ae1dac8… drh 2495 }else if( !prior ){
1a9b1c4… drh 2496 cPrefix = '+';
045deb2… drh 2497 }else{
1a9b1c4… drh 2498 cPrefix = ':';
045deb2… drh 2499 }
045deb2… drh 2500 search_doc_touch('w',rid,p->zWikiTitle);
c40863a… drh 2501 if( manifest_crosslink_busy ){
c40863a… drh 2502 add_pending_crosslink('w',p->zWikiTitle);
c40863a… drh 2503 }else{
c40863a… drh 2504 backlink_wiki_refresh(p->zWikiTitle);
c40863a… drh 2505 }
169ba8d… drh 2506 assert( manifest_event_triggers_are_enabled );
045deb2… drh 2507 db_multi_exec(
1a9b1c4… drh 2508 "REPLACE INTO event(type,mtime,objid,user,comment)"
1a9b1c4… drh 2509 "VALUES('w',%.17g,%d,%Q,'%c%q');",
1a9b1c4… drh 2510 p->rDate, rid, p->zUser, cPrefix, p->zWikiTitle
045deb2… drh 2511 );
045deb2… drh 2512 }
045deb2… drh 2513 if( p->type==CFTYPE_EVENT ){
045deb2… drh 2514 char *zTag = mprintf("event-%s", p->zEventId);
045deb2… drh 2515 int tagid = tag_findid(zTag, 1);
b8c1d42… stephan 2516 int prior = 0, subsequent;
045deb2… drh 2517 int nWiki;
045deb2… drh 2518 char zLength[40];
045deb2… drh 2519 Stmt qatt;
045deb2… drh 2520 while( fossil_isspace(p->zWiki[0]) ) p->zWiki++;
045deb2… drh 2521 nWiki = strlen(p->zWiki);
045deb2… drh 2522 sqlite3_snprintf(sizeof(zLength), zLength, "%d", nWiki);
045deb2… drh 2523 tag_insert(zTag, 1, zLength, rid, p->rDate, rid);
045deb2… drh 2524 fossil_free(zTag);
b8c1d42… stephan 2525 if(p->nParent){
b8c1d42… stephan 2526 prior = fast_uuid_to_rid(p->azParent[0]);
b8c1d42… stephan 2527 }
045deb2… drh 2528 subsequent = db_int(0,
b8c1d42… stephan 2529 /* BUG: this check is only correct if subsequent
b8c1d42… stephan 2530 version has already been crosslinked. */
bc37d1c… drh 2531 "SELECT rid FROM tagxref"
bc37d1c… drh 2532 " WHERE tagid=%d AND mtime>=%.17g AND rid!=%d"
bc37d1c… drh 2533 " ORDER BY mtime",
bc37d1c… drh 2534 tagid, p->rDate, rid
bc37d1c… drh 2535 );
9b26d1e… drh 2536 if( prior ){
a4047a9… drh 2537 content_deltify(prior, &rid, 1, 0);
9b26d1e… drh 2538 if( !subsequent ){
9b26d1e… drh 2539 db_multi_exec(
9b26d1e… drh 2540 "DELETE FROM event"
9b26d1e… drh 2541 " WHERE type='e'"
9b26d1e… drh 2542 " AND tagid=%d"
9b26d1e… drh 2543 " AND objid IN (SELECT rid FROM tagxref WHERE tagid=%d)",
9b26d1e… drh 2544 tagid, tagid
9b26d1e… drh 2545 );
9b26d1e… drh 2546 }
9b26d1e… drh 2547 }
bc37d1c… drh 2548 if( subsequent ){
a4047a9… drh 2549 content_deltify(rid, &subsequent, 1, 0);
bc37d1c… drh 2550 }else{
a00a140… drh 2551 search_doc_touch('e',rid,0);
169ba8d… drh 2552 assert( manifest_event_triggers_are_enabled );
bc37d1c… drh 2553 db_multi_exec(
bc37d1c… drh 2554 "REPLACE INTO event(type,mtime,objid,tagid,user,comment,bgcolor)"
bc37d1c… drh 2555 "VALUES('e',%.17g,%d,%d,%Q,%Q,"
bc37d1c… drh 2556 " (SELECT value FROM tagxref WHERE tagid=%d AND rid=%d));",
52b35c8… jan.nijtmans 2557 p->rEventDate, rid, tagid, p->zUser, p->zComment,
bc37d1c… drh 2558 TAG_BGCOLOR, rid
bc37d1c… drh 2559 );
bc37d1c… drh 2560 }
045deb2… drh 2561 /* Locate and update comment for any attachments */
045deb2… drh 2562 db_prepare(&qatt,
045deb2… drh 2563 "SELECT attachid, src, target, filename FROM attachment"
045deb2… drh 2564 " WHERE target=%Q",
045deb2… drh 2565 p->zEventId
045deb2… drh 2566 );
045deb2… drh 2567 while( db_step(&qatt)==SQLITE_ROW ){
045deb2… drh 2568 const char *zAttachId = db_column_text(&qatt, 0);
045deb2… drh 2569 const char *zSrc = db_column_text(&qatt, 1);
045deb2… drh 2570 const char *zTarget = db_column_text(&qatt, 2);
045deb2… drh 2571 const char *zName = db_column_text(&qatt, 3);
045deb2… drh 2572 const char isAdd = (zSrc && zSrc[0]) ? 1 : 0;
045deb2… drh 2573 char *zComment;
045deb2… drh 2574 if( isAdd ){
045deb2… drh 2575 zComment = mprintf(
045deb2… drh 2576 "Add attachment [/artifact/%!S|%h] to"
05cd9fa… rberteig 2577 " tech note [/technote/%!S|%S]",
6ef54df… jan.nijtmans 2578 zSrc, zName, zTarget, zTarget);
045deb2… drh 2579 }else{
05cd9fa… rberteig 2580 zComment = mprintf(
05cd9fa… rberteig 2581 "Delete attachment \"%h\" from"
05cd9fa… rberteig 2582 " tech note [/technote/%!S|%S]",
05cd9fa… rberteig 2583 zName, zTarget, zTarget);
045deb2… drh 2584 }
045deb2… drh 2585 db_multi_exec("UPDATE event SET comment=%Q, type='e'"
045deb2… drh 2586 " WHERE objid=%Q",
045deb2… drh 2587 zComment, zAttachId);
6ef54df… jan.nijtmans 2588 fossil_free(zComment);
045deb2… drh 2589 }
045deb2… drh 2590 db_finalize(&qatt);
bc37d1c… drh 2591 }
bc37d1c… drh 2592 if( p->type==CFTYPE_TICKET ){
bc37d1c… drh 2593 char *zTag;
045deb2… drh 2594 Stmt qatt;
bc37d1c… drh 2595 assert( manifest_crosslink_busy==1 );
bc37d1c… drh 2596 zTag = mprintf("tkt-%s", p->zTicketUuid);
bc37d1c… drh 2597 tag_insert(zTag, 1, 0, rid, p->rDate, rid);
b7ff537… stephan 2598 fossil_free(zTag);
ae1dac8… drh 2599 add_pending_crosslink('t',p->zTicketUuid);
045deb2… drh 2600 /* Locate and update comment for any attachments */
045deb2… drh 2601 db_prepare(&qatt,
045deb2… drh 2602 "SELECT attachid, src, target, filename FROM attachment"
045deb2… drh 2603 " WHERE target=%Q",
045deb2… drh 2604 p->zTicketUuid
045deb2… drh 2605 );
045deb2… drh 2606 while( db_step(&qatt)==SQLITE_ROW ){
045deb2… drh 2607 const char *zAttachId = db_column_text(&qatt, 0);
045deb2… drh 2608 const char *zSrc = db_column_text(&qatt, 1);
045deb2… drh 2609 const char *zTarget = db_column_text(&qatt, 2);
045deb2… drh 2610 const char *zName = db_column_text(&qatt, 3);
045deb2… drh 2611 const char isAdd = (zSrc && zSrc[0]) ? 1 : 0;
045deb2… drh 2612 char *zComment;
045deb2… drh 2613 if( isAdd ){
045deb2… drh 2614 zComment = mprintf(
045deb2… drh 2615 "Add attachment [/artifact/%!S|%h] to ticket [%!S|%S]",
045deb2… drh 2616 zSrc, zName, zTarget, zTarget);
045deb2… drh 2617 }else{
045deb2… drh 2618 zComment = mprintf("Delete attachment \"%h\" from ticket [%!S|%S]",
045deb2… drh 2619 zName, zTarget, zTarget);
045deb2… drh 2620 }
045deb2… drh 2621 db_multi_exec("UPDATE event SET comment=%Q, type='t'"
045deb2… drh 2622 " WHERE objid=%Q",
045deb2… drh 2623 zComment, zAttachId);
6ef54df… jan.nijtmans 2624 fossil_free(zComment);
045deb2… drh 2625 }
045deb2… drh 2626 db_finalize(&qatt);
bc37d1c… drh 2627 }
bc37d1c… drh 2628 if( p->type==CFTYPE_ATTACHMENT ){
b7ff537… stephan 2629 char *zComment = 0;
38421a9… jan.nijtmans 2630 const char isAdd = (p->zAttachSrc && p->zAttachSrc[0]) ? 1 : 0;
5079ffb… stephan 2631 char attachToType = 0;
bc37d1c… drh 2632 db_multi_exec(
bc37d1c… drh 2633 "INSERT INTO attachment(attachid, mtime, src, target,"
b7ff537… stephan 2634 "filename, comment, user)"
7c2577b… drh 2635 "VALUES(%d,%.17g,%Q,%Q,%Q,%Q,%Q);",
d13054c… drh 2636 rid, p->rDate, p->zAttachSrc, p->zAttachTarget, p->zAttachName,
d13054c… drh 2637 (p->zComment ? p->zComment : ""), p->zUser
7c2577b… drh 2638 );
7c2577b… drh 2639 db_multi_exec(
7c2577b… drh 2640 "UPDATE attachment SET isLatest = (mtime=="
7c2577b… drh 2641 "(SELECT max(mtime) FROM attachment"
7c2577b… drh 2642 " WHERE target=%Q AND filename=%Q))"
7c2577b… drh 2643 " WHERE target=%Q AND filename=%Q",
d13054c… drh 2644 p->zAttachTarget, p->zAttachName,
d13054c… drh 2645 p->zAttachTarget, p->zAttachName
d13054c… drh 2646 );
ca90659… stephan 2647 switch( attachment_target_type(p->zAttachTarget, 1) ){
5079ffb… stephan 2648 case 0:
5079ffb… stephan 2649 /* It is possible that p->zAttachTarget is not yet in this
5079ffb… stephan 2650 ** copy of the repository. If we cannot identify it yet,
5079ffb… stephan 2651 ** generate a generic /artifact link to it instead of a
5079ffb… stephan 2652 ** type-specific link or an error message. */
5079ffb… stephan 2653 attachToType = 'a';
5079ffb… stephan 2654 if( isAdd ){
5079ffb… stephan 2655 zComment = mprintf(
5079ffb… stephan 2656 "Add attachment [/artifact/%!S|%h] to [/artifact/%!S|%h]",
5079ffb… stephan 2657 p->zAttachSrc, p->zAttachName,
5079ffb… stephan 2658 p->zAttachTarget, p->zAttachTarget);
5079ffb… stephan 2659 }else{
5079ffb… stephan 2660 zComment = mprintf("Delete attachment \"%h\" from "
5079ffb… stephan 2661 "[/artifact/%!S|%h",
5079ffb… stephan 2662 p->zAttachName, p->zAttachTarget,
5079ffb… stephan 2663 p->zAttachTarget);
5079ffb… stephan 2664 }
5079ffb… stephan 2665 break;
5079ffb… stephan 2666 case CFTYPE_WIKI:
5079ffb… stephan 2667 attachToType = 'w';
5079ffb… stephan 2668 if( isAdd ){
5079ffb… stephan 2669 zComment = mprintf(
5079ffb… stephan 2670 "Add attachment [/artifact/%!S|%h] to wiki page [%h]",
5079ffb… stephan 2671 p->zAttachSrc, p->zAttachName, p->zAttachTarget);
5079ffb… stephan 2672 }else{
5079ffb… stephan 2673 zComment = mprintf("Delete attachment \"%h\" from "
5079ffb… stephan 2674 "wiki page [%h]",
5079ffb… stephan 2675 p->zAttachName, p->zAttachTarget);
5079ffb… stephan 2676 }
5079ffb… stephan 2677 break;
5079ffb… stephan 2678 case CFTYPE_EVENT:
5079ffb… stephan 2679 attachToType = 'e';
5079ffb… stephan 2680 if( isAdd ){
5079ffb… stephan 2681 zComment = mprintf(
5079ffb… stephan 2682 "Add attachment [/artifact/%!S|%h] to tech note "
5079ffb… stephan 2683 "[/technote/%!S|%S]",
5079ffb… stephan 2684 p->zAttachSrc, p->zAttachName, p->zAttachTarget, p->zAttachTarget);
5079ffb… stephan 2685 }else{
5079ffb… stephan 2686 zComment = mprintf(
5079ffb… stephan 2687 "Delete attachment \"/artifact/%!S|%h\" from"
5079ffb… stephan 2688 " tech note [/technote/%!S|%S]",
5079ffb… stephan 2689 p->zAttachName, p->zAttachName,
5079ffb… stephan 2690 p->zAttachTarget,p->zAttachTarget);
5079ffb… stephan 2691 }
5079ffb… stephan 2692 break;
5079ffb… stephan 2693 case CFTYPE_TICKET:
5079ffb… stephan 2694 attachToType = 't';
5079ffb… stephan 2695 if( isAdd ){
5079ffb… stephan 2696 zComment = mprintf(
5079ffb… stephan 2697 "Add attachment [/artifact/%!S|%h] to ticket [%!S|%S]",
5079ffb… stephan 2698 p->zAttachSrc, p->zAttachName, p->zAttachTarget, p->zAttachTarget);
5079ffb… stephan 2699 }else{
5079ffb… stephan 2700 zComment = mprintf(
5079ffb… stephan 2701 "Delete attachment \"%h\" from ticket [%!S|%S]",
5079ffb… stephan 2702 p->zAttachName, p->zAttachTarget, p->zAttachTarget);
5079ffb… stephan 2703 }
5079ffb… stephan 2704 break;
5079ffb… stephan 2705 case CFTYPE_FORUM:
5079ffb… stephan 2706 attachToType = 'f';
5079ffb… stephan 2707 if( isAdd ){
5079ffb… stephan 2708 zComment = mprintf(
5079ffb… stephan 2709 "Add attachment [/artifact/%!S|%h] to forum post "
5079ffb… stephan 2710 "[/forumpost/%!S|%S]",
5079ffb… stephan 2711 p->zAttachSrc, p->zAttachName, p->zAttachTarget, p->zAttachTarget);
5079ffb… stephan 2712 }else{
5079ffb… stephan 2713 zComment = mprintf(
5079ffb… stephan 2714 "Delete attachment \"%h\" from forum post "
5079ffb… stephan 2715 "[/forumpost/%!S|%S]",
5079ffb… stephan 2716 p->zAttachName, p->zAttachTarget, p->zAttachTarget);
5079ffb… stephan 2717 }
5079ffb… stephan 2718 break;
169ba8d… drh 2719 }
169ba8d… drh 2720 assert( manifest_event_triggers_are_enabled );
b7ff537… stephan 2721 db_multi_exec(
b7ff537… stephan 2722 "REPLACE INTO event(type,mtime,objid,user,comment)"
b7ff537… stephan 2723 "VALUES('%c',%.17g,%d,%Q,%Q)",
b7ff537… stephan 2724 attachToType, p->rDate, rid, p->zUser, zComment
b7ff537… stephan 2725 );
b7ff537… stephan 2726 fossil_free(zComment);
87540ed… drh 2727 }
87540ed… drh 2728 if( p->type==CFTYPE_CONTROL ){
87540ed… drh 2729 Blob comment;
87540ed… drh 2730 int i;
87540ed… drh 2731 const char *zName;
87540ed… drh 2732 const char *zValue;
1311841… jan.nijtmans 2733 const char *zTagUuid;
7367cec… drh 2734 int branchMove = 0;
87540ed… drh 2735 blob_zero(&comment);
9aca5f7… jan.nijtmans 2736 if( p->zComment ){
5b1a685… jan.nijtmans 2737 blob_appendf(&comment, " %s.", p->zComment);
9aca5f7… jan.nijtmans 2738 }
fd9b7bd… drh 2739 /* Next loop expects tags to be sorted on hash, so sort it. */
5b1a685… jan.nijtmans 2740 qsort(p->aTag, p->nTag, sizeof(p->aTag[0]), tag_compare);
87540ed… drh 2741 for(i=0; i<p->nTag; i++){
1311841… jan.nijtmans 2742 zTagUuid = p->aTag[i].zUuid;
1311841… jan.nijtmans 2743 if( !zTagUuid ) continue;
1311841… jan.nijtmans 2744 if( i==0 || fossil_strcmp(zTagUuid, p->aTag[i-1].zUuid)!=0 ){
7367cec… drh 2745 blob_appendf(&comment,
d93f5ee… jan.nijtmans 2746 " Edit [%!S|%S]:",
f46482a… jan.nijtmans 2747 zTagUuid, zTagUuid);
7367cec… drh 2748 branchMove = 0;
bed113c… jan.nijtmans 2749 if( permitHooks && db_exists("SELECT 1 FROM event, blob"
1311841… jan.nijtmans 2750 " WHERE event.type='ci' AND event.objid=blob.rid"
49b0ff1… drh 2751 " AND blob.uuid=%Q", zTagUuid) ){
bed113c… jan.nijtmans 2752 zScript = xfer_commit_code();
5fabb7c… stephan 2753 fossil_free(zUuid);
5fabb7c… stephan 2754 zUuid = fossil_strdup(zTagUuid);
1311841… jan.nijtmans 2755 }
87540ed… drh 2756 }
87540ed… drh 2757 zName = p->aTag[i].zName;
87540ed… drh 2758 zValue = p->aTag[i].zValue;
7367cec… drh 2759 if( strcmp(zName, "*branch")==0 ){
7367cec… drh 2760 blob_appendf(&comment,
55e9a2f… drh 2761 " Move to branch [/timeline?r=%t&nd&dp=%!S&unhide | %h].",
1311841… jan.nijtmans 2762 zValue, zTagUuid, zValue);
7367cec… drh 2763 branchMove = 1;
24bff45… jan.nijtmans 2764 continue;
7367cec… drh 2765 }else if( strcmp(zName, "*bgcolor")==0 ){
7367cec… drh 2766 blob_appendf(&comment,
7367cec… drh 2767 " Change branch background color to \"%h\".", zValue);
24bff45… jan.nijtmans 2768 continue;
7367cec… drh 2769 }else if( strcmp(zName, "+bgcolor")==0 ){
7367cec… drh 2770 blob_appendf(&comment,
7367cec… drh 2771 " Change background color to \"%h\".", zValue);
24bff45… jan.nijtmans 2772 continue;
7367cec… drh 2773 }else if( strcmp(zName, "-bgcolor")==0 ){
24bff45… jan.nijtmans 2774 blob_appendf(&comment, " Cancel background color");
7367cec… drh 2775 }else if( strcmp(zName, "+comment")==0 ){
7367cec… drh 2776 blob_appendf(&comment, " Edit check-in comment.");
24bff45… jan.nijtmans 2777 continue;
7367cec… drh 2778 }else if( strcmp(zName, "+user")==0 ){
7367cec… drh 2779 blob_appendf(&comment, " Change user to \"%h\".", zValue);
24bff45… jan.nijtmans 2780 continue;
7367cec… drh 2781 }else if( strcmp(zName, "+date")==0 ){
7367cec… drh 2782 blob_appendf(&comment, " Timestamp %h.", zValue);
24bff45… jan.nijtmans 2783 continue;
7367cec… drh 2784 }else if( memcmp(zName, "-sym-",5)==0 ){
9a8d082… drh 2785 if( !branchMove ){
9a8d082… drh 2786 blob_appendf(&comment, " Cancel tag \"%h\"", &zName[5]);
ef5f795… andygoth 2787 }else{
ef5f795… andygoth 2788 continue;
9a8d082… drh 2789 }
7367cec… drh 2790 }else if( memcmp(zName, "*sym-",5)==0 ){
7367cec… drh 2791 if( !branchMove ){
24bff45… jan.nijtmans 2792 blob_appendf(&comment, " Add propagating tag \"%h\"", &zName[5]);
ef5f795… andygoth 2793 }else{
ef5f795… andygoth 2794 continue;
7367cec… drh 2795 }
7367cec… drh 2796 }else if( memcmp(zName, "+sym-",5)==0 ){
24bff45… jan.nijtmans 2797 blob_appendf(&comment, " Add tag \"%h\"", &zName[5]);
7367cec… drh 2798 }else if( strcmp(zName, "+closed")==0 ){
ef5f795… andygoth 2799 blob_append(&comment, " Mark \"Closed\"", -1);
7367cec… drh 2800 }else if( strcmp(zName, "-closed")==0 ){
ef5f795… andygoth 2801 blob_append(&comment, " Remove the \"Closed\" mark", -1);
7367cec… drh 2802 }else {
7367cec… drh 2803 if( zName[0]=='-' ){
7367cec… drh 2804 blob_appendf(&comment, " Cancel \"%h\"", &zName[1]);
7367cec… drh 2805 }else if( zName[0]=='+' ){
7367cec… drh 2806 blob_appendf(&comment, " Add \"%h\"", &zName[1]);
7367cec… drh 2807 }else{
7367cec… drh 2808 blob_appendf(&comment, " Add propagating \"%h\"", &zName[1]);
7367cec… drh 2809 }
7367cec… drh 2810 if( zValue && zValue[0] ){
7367cec… drh 2811 blob_appendf(&comment, " with value \"%h\".", zValue);
7367cec… drh 2812 }else{
7367cec… drh 2813 blob_appendf(&comment, ".");
7367cec… drh 2814 }
24bff45… jan.nijtmans 2815 continue;
24bff45… jan.nijtmans 2816 }
24bff45… jan.nijtmans 2817 if( zValue && zValue[0] ){
24bff45… jan.nijtmans 2818 blob_appendf(&comment, " with note \"%h\".", zValue);
24bff45… jan.nijtmans 2819 }else{
24bff45… jan.nijtmans 2820 blob_appendf(&comment, ".");
7367cec… drh 2821 }
7367cec… drh 2822 }
7367cec… drh 2823 /*blob_appendf(&comment, " &#91;[/info/%S | details]&#93;");*/
5b1a685… jan.nijtmans 2824 if( blob_size(&comment)==0 ) blob_append(&comment, " ", 1);
169ba8d… drh 2825 assert( manifest_event_triggers_are_enabled );
87540ed… drh 2826 db_multi_exec(
87540ed… drh 2827 "REPLACE INTO event(type,mtime,objid,user,comment)"
87540ed… drh 2828 "VALUES('g',%.17g,%d,%Q,%Q)",
5b1a685… jan.nijtmans 2829 p->rDate, rid, p->zUser, blob_str(&comment)+1
87540ed… drh 2830 );
87540ed… drh 2831 blob_reset(&comment);
87540ed… drh 2832 }
99fcc43… drh 2833 if( p->type==CFTYPE_FORUM ){
99fcc43… drh 2834 int froot, fprev, firt;
99fcc43… drh 2835 char *zFType;
99fcc43… drh 2836 char *zTitle;
5fabb7c… stephan 2837
5fabb7c… stephan 2838 assert( 0==zUuid );
99fcc43… drh 2839 schema_forum();
5e28feb… drh 2840 search_doc_touch('f', rid, 0);
99fcc43… drh 2841 froot = p->zThreadRoot ? uuid_to_rid(p->zThreadRoot, 1) : rid;
99fcc43… drh 2842 fprev = p->nParent ? uuid_to_rid(p->azParent[0],1) : 0;
99fcc43… drh 2843 firt = p->zInReplyTo ? uuid_to_rid(p->zInReplyTo,1) : 0;
99fcc43… drh 2844 db_multi_exec(
8a980fd… drh 2845 "REPLACE INTO forumpost(fpid,froot,fprev,firt,fmtime)"
99fcc43… drh 2846 "VALUES(%d,%d,nullif(%d,0),nullif(%d,0),%.17g)",
99fcc43… drh 2847 p->rid, froot, fprev, firt, p->rDate
99fcc43… drh 2848 );
99fcc43… drh 2849 if( firt==0 ){
99fcc43… drh 2850 /* This is the start of a new thread, either the initial entry
99fcc43… drh 2851 ** or an edit of the initial entry. */
99fcc43… drh 2852 zTitle = p->zThreadTitle;
99fcc43… drh 2853 if( zTitle==0 || zTitle[0]==0 ){
99fcc43… drh 2854 zTitle = "(Deleted)";
99fcc43… drh 2855 }
99fcc43… drh 2856 zFType = fprev ? "Edit" : "Post";
169ba8d… drh 2857 assert( manifest_event_triggers_are_enabled );
99fcc43… drh 2858 db_multi_exec(
99fcc43… drh 2859 "REPLACE INTO event(type,mtime,objid,user,comment)"
99fcc43… drh 2860 "VALUES('f',%.17g,%d,%Q,'%q: %q')",
99fcc43… drh 2861 p->rDate, rid, p->zUser, zFType, zTitle
99fcc43… drh 2862 );
99fcc43… drh 2863 /*
99fcc43… drh 2864 ** If this edit is the most recent, then make it the title for
99fcc43… drh 2865 ** all other entries for the same thread
99fcc43… drh 2866 */
99fcc43… drh 2867 if( !db_exists("SELECT 1 FROM forumpost WHERE froot=%d AND firt=0"
99fcc43… drh 2868 " AND fpid!=%d AND fmtime>%.17g", froot, rid, p->rDate)
99fcc43… drh 2869 ){
99fcc43… drh 2870 /* This entry establishes a new title for all entries on the thread */
99fcc43… drh 2871 db_multi_exec(
99fcc43… drh 2872 "UPDATE event"
99fcc43… drh 2873 " SET comment=substr(comment,1,instr(comment,':')) || ' %q'"
99fcc43… drh 2874 " WHERE objid IN (SELECT fpid FROM forumpost WHERE froot=%d)",
99fcc43… drh 2875 zTitle, froot
99fcc43… drh 2876 );
99fcc43… drh 2877 }
99fcc43… drh 2878 }else{
99fcc43… drh 2879 /* This is a reply to a prior post. Take the title from the root. */
99fcc43… drh 2880 zTitle = db_text(0, "SELECT substr(comment,instr(comment,':')+2)"
99fcc43… drh 2881 " FROM event WHERE objid=%d", froot);
99fcc43… drh 2882 if( zTitle==0 ) zTitle = fossil_strdup("<i>Unknown</i>");
99fcc43… drh 2883 if( p->zWiki[0]==0 ){
99fcc43… drh 2884 zFType = "Delete reply";
99fcc43… drh 2885 }else if( fprev ){
99fcc43… drh 2886 zFType = "Edit reply";
99fcc43… drh 2887 }else{
99fcc43… drh 2888 zFType = "Reply";
99fcc43… drh 2889 }
169ba8d… drh 2890 assert( manifest_event_triggers_are_enabled );
99fcc43… drh 2891 db_multi_exec(
99fcc43… drh 2892 "REPLACE INTO event(type,mtime,objid,user,comment)"
99fcc43… drh 2893 "VALUES('f',%.17g,%d,%Q,'%q: %q')",
99fcc43… drh 2894 p->rDate, rid, p->zUser, zFType, zTitle
99fcc43… drh 2895 );
99fcc43… drh 2896 fossil_free(zTitle);
99fcc43… drh 2897 }
2df0e5c… drh 2898 if( p->zWiki[0] ){
6fb642c… george 2899 int mimetype = parse_mimetype(p->zMimetype);
6fb642c… george 2900 backlink_extract(p->zWiki, mimetype, rid, BKLNK_FORUM, p->rDate, 1);
2df0e5c… drh 2901 }
99fcc43… drh 2902 }
7c64fe0… drh 2903
87540ed… drh 2904 db_end_transaction(0);
42316a1… mistachkin 2905 if( permitHooks ){
42316a1… mistachkin 2906 rc = xfer_run_common_script();
42316a1… mistachkin 2907 if( rc==TH_OK ){
fc32fae… mistachkin 2908 rc = xfer_run_script(zScript, zUuid, 0);
1311841… jan.nijtmans 2909 }
1311841… jan.nijtmans 2910 }
5fabb7c… stephan 2911 fossil_free(zUuid);
d13054c… drh 2912 if( p->type==CFTYPE_MANIFEST ){
d13054c… drh 2913 manifest_cache_insert(p);
d13054c… drh 2914 }else{
d13054c… drh 2915 manifest_destroy(p);
d13054c… drh 2916 }
d22519e… drh 2917 assert( blob_is_reset(pContent) );
42316a1… mistachkin 2918 return ( rc!=TH_ERROR );
ebd36f9… drh 2919 }
ebd36f9… drh 2920
ebd36f9… drh 2921 /*
ebd36f9… drh 2922 ** COMMAND: test-crosslink
ebd36f9… drh 2923 **
ebd36f9… drh 2924 ** Usage: %fossil test-crosslink RECORDID
ebd36f9… drh 2925 **
ebd36f9… drh 2926 ** Run the manifest_crosslink() routine on the artifact with the given
ebd36f9… drh 2927 ** record ID. This is typically done in the debugger.
ebd36f9… drh 2928 */
ebd36f9… drh 2929 void test_crosslink_cmd(void){
ebd36f9… drh 2930 int rid;
ebd36f9… drh 2931 Blob content;
ebd36f9… drh 2932 db_find_and_open_repository(0, 0);
ebd36f9… drh 2933 if( g.argc!=3 ) usage("RECORDID");
ebd36f9… drh 2934 rid = name_to_rid(g.argv[2]);
ebd36f9… drh 2935 content_get(rid, &content);
1311841… jan.nijtmans 2936 manifest_crosslink(rid, &content, MC_NONE);
1e2d602… stephan 2937 }
1e2d602… stephan 2938
1e2d602… stephan 2939 /*
1e2d602… stephan 2940 ** For a given CATYPE_... value, returns a human-friendly name, or
94d5e32… stephan 2941 ** NULL if typeId is unknown or is CFTYPE_ANY. The names returned by
94d5e32… stephan 2942 ** this function are geared towards use with artifact_to_json(), and
94d5e32… stephan 2943 ** may differ from some historical uses. e.g. CFTYPE_CONTROL artifacts
94d5e32… stephan 2944 ** are called "tag" artifacts by this function.
1e2d602… stephan 2945 */
1e2d602… stephan 2946 const char * artifact_type_to_name(int typeId){
1e2d602… stephan 2947 switch(typeId){
1e2d602… stephan 2948 case CFTYPE_MANIFEST: return "checkin";
1e2d602… stephan 2949 case CFTYPE_CLUSTER: return "cluster";
1e2d602… stephan 2950 case CFTYPE_CONTROL: return "tag";
1e2d602… stephan 2951 case CFTYPE_WIKI: return "wiki";
1e2d602… stephan 2952 case CFTYPE_TICKET: return "ticket";
1e2d602… stephan 2953 case CFTYPE_ATTACHMENT: return "attachment";
b9fb403… stephan 2954 case CFTYPE_EVENT: return "technote";
1e2d602… stephan 2955 case CFTYPE_FORUM: return "forumpost";
1e2d602… stephan 2956 }
1e2d602… stephan 2957 return NULL;
1e2d602… stephan 2958 }
1e2d602… stephan 2959
1e2d602… stephan 2960 /*
1e2d602… stephan 2961 ** Creates a JSON representation of p, appending it to b.
1e2d602… stephan 2962 **
1e2d602… stephan 2963 ** b is not cleared before rendering, so the caller needs to do that
1e2d602… stephan 2964 ** if it's important for their use case.
1e2d602… stephan 2965 **
ca8d6ee… stephan 2966 ** Pedantic note: this routine traverses p->aFile directly, rather
ca8d6ee… stephan 2967 ** than using manifest_file_next(), so that delta manifests are
ca8d6ee… stephan 2968 ** rendered as-is instead of containing their derived F-cards. If that
ca8d6ee… stephan 2969 ** policy is ever changed, p will need to be non-const.
1e2d602… stephan 2970 */
1e2d602… stephan 2971 void artifact_to_json(Manifest const *p, Blob *b){
1e2d602… stephan 2972 int i;
1e2d602… stephan 2973
1e2d602… stephan 2974 blob_append_literal(b, "{");
f184784… stephan 2975 blob_appendf(b, "\"uuid\":\"%z\"", rid_to_uuid(p->rid));
1e2d602… stephan 2976 /*blob_appendf(b, ", \"rid\": %d", p->rid); not portable across repos*/
f184784… stephan 2977 blob_appendf(b, ",\"type\":%!j", artifact_type_to_name(p->type));
1e2d602… stephan 2978 #define ISA(TYPE) if( p->type==TYPE )
1e2d602… stephan 2979 #define CARD_LETTER(LETTER) \
f184784… stephan 2980 blob_append_literal(b, ",\"" #LETTER "\":")
1e2d602… stephan 2981 #define CARD_STR(LETTER, VAL) \
1e2d602… stephan 2982 assert( VAL ); CARD_LETTER(LETTER); blob_appendf(b, "%!j", VAL)
1e2d602… stephan 2983 #define CARD_STR2(LETTER, VAL) \
1e2d602… stephan 2984 if( VAL ) { CARD_STR(LETTER, VAL); } (void)0
1e2d602… stephan 2985 #define STR_OR_NULL(VAL) \
1e2d602… stephan 2986 if( VAL ) blob_appendf(b, "%!j", VAL); \
1e2d602… stephan 2987 else blob_append(b, "null", 4)
1e2d602… stephan 2988 #define KVP_STR(ADDCOMMA, KEY,VAL) \
1e2d602… stephan 2989 if(ADDCOMMA) blob_append_char(b, ','); \
f184784… stephan 2990 blob_appendf(b, "%!j:", #KEY); \
1e2d602… stephan 2991 STR_OR_NULL(VAL)
1e2d602… stephan 2992
1e2d602… stephan 2993 ISA( CFTYPE_ATTACHMENT ){
1e2d602… stephan 2994 CARD_LETTER(A);
1e2d602… stephan 2995 blob_append_char(b, '{');
1e2d602… stephan 2996 KVP_STR(0, filename, p->zAttachName);
1e2d602… stephan 2997 KVP_STR(1, target, p->zAttachTarget);
1e2d602… stephan 2998 KVP_STR(1, source, p->zAttachSrc);
1e2d602… stephan 2999 blob_append_char(b, '}');
1e2d602… stephan 3000 }
1e2d602… stephan 3001 CARD_STR2(B, p->zBaseline);
1e2d602… stephan 3002 CARD_STR2(C, p->zComment);
1e2d602… stephan 3003 CARD_LETTER(D); blob_appendf(b, "%f", p->rDate);
1e2d602… stephan 3004 ISA( CFTYPE_EVENT ){
f184784… stephan 3005 blob_appendf(b, ", \"E\":{\"time\":%f,\"id\":%!j}",
1e2d602… stephan 3006 p->rEventDate, p->zEventId);
1e2d602… stephan 3007 }
1e2d602… stephan 3008 ISA( CFTYPE_MANIFEST ){
1e2d602… stephan 3009 CARD_LETTER(F);
1e2d602… stephan 3010 blob_append_char(b, '[');
1e2d602… stephan 3011 for( i = 0; i < p->nFile; ++i ){
1e2d602… stephan 3012 ManifestFile const * const pF = &p->aFile[i];
1e2d602… stephan 3013 if( i>0 ) blob_append_char(b, ',');
1e2d602… stephan 3014 blob_append_char(b, '{');
1e2d602… stephan 3015 KVP_STR(0, name, pF->zName);
1e2d602… stephan 3016 KVP_STR(1, uuid, pF->zUuid);
1e2d602… stephan 3017 KVP_STR(1, perm, pF->zPerm);
d2db37a… stephan 3018 KVP_STR(1, rename, pF->zPrior);
1e2d602… stephan 3019 blob_append_char(b, '}');
1e2d602… stephan 3020 }
e2bdc10… danield 3021 /* Special case: model check-ins with no F-card as having an empty
1e2d602… stephan 3022 ** array, rather than no F-cards, to hypothetically simplify
1e2d602… stephan 3023 ** handling in JSON queries. */
1e2d602… stephan 3024 blob_append_char(b, ']');
1e2d602… stephan 3025 }
1e2d602… stephan 3026 CARD_STR2(G, p->zThreadRoot);
d2db37a… stephan 3027 ISA( CFTYPE_FORUM ){
87bdf7d… stephan 3028 CARD_LETTER(H);
87bdf7d… stephan 3029 STR_OR_NULL( (p->zThreadTitle && *p->zThreadTitle) ? p->zThreadTitle : NULL);
87bdf7d… stephan 3030 CARD_STR2(I, p->zInReplyTo);
87bdf7d… stephan 3031 }
1e2d602… stephan 3032 if( p->nField ){
1e2d602… stephan 3033 CARD_LETTER(J);
1e2d602… stephan 3034 blob_append_char(b, '[');
1e2d602… stephan 3035 for( i = 0; i < p->nField; ++i ){
98396f4… stephan 3036 const char * zName = p->aField[i].zName;
1e2d602… stephan 3037 if( i>0 ) blob_append_char(b, ',');
1e2d602… stephan 3038 blob_append_char(b, '{');
98396f4… stephan 3039 KVP_STR(0, name, '+'==*zName ? &zName[1] : zName);
1e2d602… stephan 3040 KVP_STR(1, value, p->aField[i].zValue);
f184784… stephan 3041 blob_appendf(b, ",\"append\":%s", '+'==*zName ? "true" : "false");
1e2d602… stephan 3042 blob_append_char(b, '}');
1e2d602… stephan 3043 }
1e2d602… stephan 3044 blob_append_char(b, ']');
1e2d602… stephan 3045 }
1e2d602… stephan 3046 CARD_STR2(K, p->zTicketUuid);
1e2d602… stephan 3047 CARD_STR2(L, p->zWikiTitle);
ca8d6ee… stephan 3048 ISA( CFTYPE_CLUSTER ){
1e2d602… stephan 3049 CARD_LETTER(M);
1e2d602… stephan 3050 blob_append_char(b, '[');
2ce32f7… florian 3051 for( i = 0; i < p->nCChild; ++i ){
1e2d602… stephan 3052 if( i>0 ) blob_append_char(b, ',');
1e2d602… stephan 3053 blob_appendf(b, "%!j", p->azCChild[i]);
1e2d602… stephan 3054 }
1e2d602… stephan 3055 blob_append_char(b, ']');
1e2d602… stephan 3056 }
1e2d602… stephan 3057 CARD_STR2(N, p->zMimetype);
0d8c913… stephan 3058 ISA( CFTYPE_MANIFEST || p->nParent>0 ){
1e2d602… stephan 3059 CARD_LETTER(P);
1e2d602… stephan 3060 blob_append_char(b, '[');
87bdf7d… stephan 3061 for( i = 0; i < p->nParent; ++i ){
87bdf7d… stephan 3062 if( i>0 ) blob_append_char(b, ',');
87bdf7d… stephan 3063 blob_appendf(b, "%!j", p->azParent[i]);
ca8d6ee… stephan 3064 }
e2bdc10… danield 3065 /* Special case: model check-ins with no P-card as having an empty
ca8d6ee… stephan 3066 ** array, as per F-cards. */
1e2d602… stephan 3067 blob_append_char(b, ']');
1e2d602… stephan 3068 }
1e2d602… stephan 3069 if( p->nCherrypick ){
1e2d602… stephan 3070 CARD_LETTER(Q);
1e2d602… stephan 3071 blob_append_char(b, '[');
1e2d602… stephan 3072 for( i = 0; i < p->nCherrypick; ++i ){
1e2d602… stephan 3073 if( i>0 ) blob_append_char(b, ',');
1e2d602… stephan 3074 blob_append_char(b, '{');
f184784… stephan 3075 blob_appendf(b, "\"type\":\"%c\"", p->aCherrypick[i].zCPTarget[0]);
1e2d602… stephan 3076 KVP_STR(1, target, &p->aCherrypick[i].zCPTarget[1]);
1e2d602… stephan 3077 KVP_STR(1, base, p->aCherrypick[i].zCPBase);
1e2d602… stephan 3078 blob_append_char(b, '}');
1e2d602… stephan 3079 }
1e2d602… stephan 3080 blob_append_char(b, ']');
1e2d602… stephan 3081 }
1e2d602… stephan 3082 CARD_STR2(R, p->zRepoCksum);
1e2d602… stephan 3083 if( p->nTag ){
1e2d602… stephan 3084 CARD_LETTER(T);
1e2d602… stephan 3085 blob_append_char(b, '[');
2ce32f7… florian 3086 for( i = 0; i < p->nTag; ++i ){
1e2d602… stephan 3087 const char *zName = p->aTag[i].zName;
1e2d602… stephan 3088 if( i>0 ) blob_append_char(b, ',');
1e2d602… stephan 3089 blob_append_char(b, '{');
f184784… stephan 3090 blob_appendf(b, "\"type\":\"%c\"", *zName);
ca8d6ee… stephan 3091 KVP_STR(1, name, &zName[1]);
1e2d602… stephan 3092 KVP_STR(1, target, p->aTag[i].zUuid ? p->aTag[i].zUuid : "*")
ca8d6ee… stephan 3093 /* We could arguably resolve the "*" as null or p's uuid. */;
1e2d602… stephan 3094 KVP_STR(1, value, p->aTag[i].zValue);
1e2d602… stephan 3095 blob_append_char(b, '}');
1e2d602… stephan 3096 }
1e2d602… stephan 3097 blob_append_char(b, ']');
1e2d602… stephan 3098 }
1e2d602… stephan 3099 CARD_STR2(U, p->zUser);
655d613… stephan 3100 if( p->zWiki || CFTYPE_WIKI==p->type || CFTYPE_FORUM==p->type
655d613… stephan 3101 || CFTYPE_EVENT==p->type ){
87bdf7d… stephan 3102 CARD_LETTER(W);
87bdf7d… stephan 3103 STR_OR_NULL((p->zWiki && *p->zWiki) ? p->zWiki : NULL);
87bdf7d… stephan 3104 }
1e2d602… stephan 3105 blob_append_literal(b, "}");
1e2d602… stephan 3106 #undef CARD_FMT
1e2d602… stephan 3107 #undef CARD_LETTER
1e2d602… stephan 3108 #undef CARD_STR
1e2d602… stephan 3109 #undef CARD_STR2
1e2d602… stephan 3110 #undef ISA
1e2d602… stephan 3111 #undef KVP_STR
1e2d602… stephan 3112 #undef STR_OR_NULL
1e2d602… stephan 3113 }
1e2d602… stephan 3114
1e2d602… stephan 3115 /*
1e2d602… stephan 3116 ** Convenience wrapper around artifact_to_json() which expects rid to
1e2d602… stephan 3117 ** be the blob.rid of any artifact type. If it can load a Manifest
1e2d602… stephan 3118 ** with that rid, it returns rid, else it returns 0.
1e2d602… stephan 3119 */
1e2d602… stephan 3120 int artifact_to_json_by_rid(int rid, Blob *pOut){
1e2d602… stephan 3121 Manifest * const p = manifest_get(rid, CFTYPE_ANY, 0);
1e2d602… stephan 3122 if( p ){
1e2d602… stephan 3123 artifact_to_json(p, pOut);
1e2d602… stephan 3124 manifest_destroy(p);
1e2d602… stephan 3125 }else{
1e2d602… stephan 3126 rid = 0;
1e2d602… stephan 3127 }
1e2d602… stephan 3128 return rid;
1e2d602… stephan 3129 }
1e2d602… stephan 3130
1e2d602… stephan 3131 /*
1e2d602… stephan 3132 ** Convenience wrapper around artifact_to_json() which accepts any
1e2d602… stephan 3133 ** artifact name which is legal for symbolic_name_to_rid(). On success
1e2d602… stephan 3134 ** it returns the rid of the artifact. Returns 0 if no such artifact
1e2d602… stephan 3135 ** exists and a negative value if the name is ambiguous.
1e2d602… stephan 3136 **
1e2d602… stephan 3137 ** pOut is not cleared before rendering, so the caller needs to do
1e2d602… stephan 3138 ** that if it's important for their use case.
1e2d602… stephan 3139 */
1e2d602… stephan 3140 int artifact_to_json_by_name(const char *zName, Blob *pOut){
1e2d602… stephan 3141 const int rid = symbolic_name_to_rid(zName, 0);
1e2d602… stephan 3142 return rid>0
1e2d602… stephan 3143 ? artifact_to_json_by_rid(rid, pOut)
1e2d602… stephan 3144 : rid;
1e2d602… stephan 3145 }
1e2d602… stephan 3146
1e2d602… stephan 3147 /*
1e2d602… stephan 3148 ** SQLite UDF for artifact_to_json(). Its single argument should be
1e2d602… stephan 3149 ** either an INTEGER (blob.rid value) or a TEXT symbolic artifact
1e2d602… stephan 3150 ** name, as per symbolic_name_to_rid(). If an artifact is found then
1e2d602… stephan 3151 ** the result of the UDF is that JSON as a string, else it evaluates
1e2d602… stephan 3152 ** to NULL.
1e2d602… stephan 3153 */
1e2d602… stephan 3154 void artifact_to_json_sql_func(
1e2d602… stephan 3155 sqlite3_context *context,
1e2d602… stephan 3156 int argc,
1e2d602… stephan 3157 sqlite3_value **argv
1e2d602… stephan 3158 ){
1e2d602… stephan 3159 int rid = 0;
1e2d602… stephan 3160 Blob b = empty_blob;
1e2d602… stephan 3161
1e2d602… stephan 3162 if(1 != argc){
1e2d602… stephan 3163 goto error_usage;
1e2d602… stephan 3164 }
1e2d602… stephan 3165 switch( sqlite3_value_type(argv[0]) ){
1e2d602… stephan 3166 case SQLITE_INTEGER:
1e2d602… stephan 3167 rid = artifact_to_json_by_rid(sqlite3_value_int(argv[0]), &b);
1e2d602… stephan 3168 break;
1e2d602… stephan 3169 case SQLITE_TEXT:{
1e2d602… stephan 3170 const char * z = (const char *)sqlite3_value_text(argv[0]);
1e2d602… stephan 3171 if( z ){
1e2d602… stephan 3172 rid = artifact_to_json_by_name(z, &b);
1e2d602… stephan 3173 }
1e2d602… stephan 3174 break;
1e2d602… stephan 3175 }
1e2d602… stephan 3176 default:
1e2d602… stephan 3177 goto error_usage;
1e2d602… stephan 3178 }
1e2d602… stephan 3179 if( rid>0 ){
1e2d602… stephan 3180 sqlite3_result_text(context, blob_str(&b), blob_size(&b),
1e2d602… stephan 3181 SQLITE_TRANSIENT);
1e2d602… stephan 3182 blob_reset(&b);
1e2d602… stephan 3183 }else{
1e2d602… stephan 3184 /* We should arguably error out if rid<0 (ambiguous name) */
1e2d602… stephan 3185 sqlite3_result_null(context);
1e2d602… stephan 3186 }
1e2d602… stephan 3187 return;
1e2d602… stephan 3188 error_usage:
1e2d602… stephan 3189 sqlite3_result_error(context, "Expecting one argument: blob.rid or "
1e2d602… stephan 3190 "artifact symbolic name", -1);
1e2d602… stephan 3191 }
1e2d602… stephan 3192
1e2d602… stephan 3193
1e2d602… stephan 3194
1e2d602… stephan 3195 /*
1e2d602… stephan 3196 ** COMMAND: test-artifact-to-json
1e2d602… stephan 3197 **
f184784… stephan 3198 ** Usage: %fossil test-artifact-to-json ?-pretty|-p? symbolic-name [...names]
1e2d602… stephan 3199 **
1e2d602… stephan 3200 ** Tests the artifact_to_json() and artifact_to_json_by_name() APIs.
1e2d602… stephan 3201 */
1e2d602… stephan 3202 void test_manifest_to_json(void){
1e2d602… stephan 3203 int i;
1e2d602… stephan 3204 Blob b = empty_blob;
1e2d602… stephan 3205 Stmt q;
f184784… stephan 3206 const int bPretty = find_option("pretty","p",0)!=0;
1e2d602… stephan 3207 int nErr = 0;
1e2d602… stephan 3208
1e2d602… stephan 3209 db_find_and_open_repository(0,0);
1e2d602… stephan 3210 db_prepare(&q, "select json_pretty(:json)");
1e2d602… stephan 3211 for( i=2; i<g.argc; ++i ){
1e2d602… stephan 3212 char const *zName = g.argv[i];
1e2d602… stephan 3213 const int rc = artifact_to_json_by_name(zName, &b);
1e2d602… stephan 3214 if( rc<=0 ){
1e2d602… stephan 3215 ++nErr;
1e2d602… stephan 3216 fossil_warning("Error reading artifact %Q", zName);
1e2d602… stephan 3217 continue;
1e2d602… stephan 3218 }else if( bPretty ){
1e2d602… stephan 3219 db_bind_blob(&q, ":json", &b);
1e2d602… stephan 3220 b.nUsed = 0;
1e2d602… stephan 3221 db_step(&q);
1e2d602… stephan 3222 db_column_blob(&q, 0, &b);
1e2d602… stephan 3223 db_reset(&q);
1e2d602… stephan 3224 }
1e2d602… stephan 3225 fossil_print("%b\n", &b);
1e2d602… stephan 3226 blob_reset(&b);
1e2d602… stephan 3227 }
1e2d602… stephan 3228 db_finalize(&q);
1e2d602… stephan 3229 if( nErr ){
1e2d602… stephan 3230 fossil_warning("Error count: %d", nErr);
1e2d602… stephan 3231 }
ab0f06a… stephan 3232 }
ab0f06a… stephan 3233
ab0f06a… stephan 3234 /*
ab0f06a… stephan 3235 ** Given the RID of an artifact, if that artifact type supports
ab0f06a… stephan 3236 ** P-cards then this returns the root-most RID of the P-card chain by
ab0f06a… stephan 3237 ** traversing the plink table, considering only primary parents. If
ab0f06a… stephan 3238 ** rid refers to anything other than such an artifact, or if the
ab0f06a… stephan 3239 ** artifact has no primary parent, rid is returned as-is.
ab0f06a… stephan 3240 */
ab0f06a… stephan 3241 int rid_root_parent(int rid){
ab0f06a… stephan 3242 Stmt q;
ab0f06a… stephan 3243 db_prepare(
ab0f06a… stephan 3244 &q, "SELECT pid FROM plink WHERE isprim AND cid=:cid"
ab0f06a… stephan 3245 );
ab0f06a… stephan 3246 db_bind_int(&q, ":cid", rid);
ab0f06a… stephan 3247 while( SQLITE_ROW==db_step(&q) ){
ab0f06a… stephan 3248 rid = db_column_int(&q, 0);
ab0f06a… stephan 3249 db_reset(&q);
ab0f06a… stephan 3250 db_bind_int(&q, ":cid", rid);
ab0f06a… stephan 3251 }
ab0f06a… stephan 3252 db_finalize(&q);
ab0f06a… stephan 3253 return rid;
dbda8d6… drh 3254 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button