Check-in [ba35aa3a57]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Removed fsl_deck::uuid and the step in fsl_deck_parse() which tries to map its input to an existing artifact. That step (which was there simply to proactively fetch the deck's rid and uuid) isn't strictly necessary and is a big performance hit. This reduces fsl_deck_parse() time by very roughly 2/3rds, per f-parseparty's mass-parsing tests on the libfossil and fossil repos.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ba35aa3a576fc060b129f46731f86a656e9c523b
User & Date: stephan 2021-10-20 14:07:51
Original Comment: Removed fsl_deck::uuid and the step in fsl_deck_parse() which tries to map its input to an existing artifact. That step (which was there simply to proactively fetch the deck's rid and uuid) aren't strictly necessary and are a big performance hit (reducing parse-time costs by roughly 2/3rds f-parseparty's mass-parsing tests).
References
2024-12-10
06:22 Wiki page "TODOs" artifact: 5c87f073a7 user: stephan
Context
2021-10-20
14:26
Doc updates related to the removal of fsl_deck::uuid. Fixed a mismatched transaction start/end in one particular error case in fsl_deck_crosslink() (when loading baseline of a delta manifest failed). check-in: 1742cabfde user: stephan tags: trunk
14:07
Removed fsl_deck::uuid and the step in fsl_deck_parse() which tries to map its input to an existing artifact. That step (which was there simply to proactively fetch the deck's rid and uuid) isn't strictly necessary and is a big performance hit. This reduces fsl_deck_parse() time by very roughly 2/3rds, per f-parseparty's mass-parsing tests on the libfossil and fossil repos. check-in: ba35aa3a57 user: stephan tags: trunk
13:16
f-parseparty: fixed an assert broken by the fsl_deck refactoring and added --dry-run flag. Closed-Leaf check-in: c1390fe2f8 user: stephan tags: remove-deck-uuid
11:30
Minor auto.def cleanup. check-in: 5a85356515 user: stephan tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to f-apps/f-aparse.c.
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Just for testing default crosslinker replacement */
#define MY_OVERRIDE_XLINK_CHECKIN 0

/**
    Just experimenting with fsl_xlink_listener() and friends.
 */
static int my_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.12s] (RID %"FSL_ID_T_PFMT")\n",
           fsl_satype_cstr(d->type), d->uuid, d->rid));
  if( *((char const *)state) ){
    return fsl_cx_err_set(d->f, FSL_RC_IO,
                          "Demonstrating what happens when crosslinking fails.");
  }
#if !MY_OVERRIDE_XLINK_CHECKIN
  return fsl_db_exec(fsl_cx_db_repo(d->f),
       "UPDATE event SET ecomment="







|
|







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Just for testing default crosslinker replacement */
#define MY_OVERRIDE_XLINK_CHECKIN 0

/**
    Just experimenting with fsl_xlink_listener() and friends.
 */
static int my_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact RID %" FSL_ID_T_PFMT "\n",
           fsl_satype_cstr(d->type), d->rid));
  if( *((char const *)state) ){
    return fsl_cx_err_set(d->f, FSL_RC_IO,
                          "Demonstrating what happens when crosslinking fails.");
  }
#if !MY_OVERRIDE_XLINK_CHECKIN
  return fsl_db_exec(fsl_cx_db_repo(d->f),
       "UPDATE event SET ecomment="
101
102
103
104
105
106
107
108
109
110
111
112
113
114















115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
}

static int test_parse_1( char const * mfile ){
  fsl_buffer buf = fsl_buffer_empty;
  fsl_buffer bout = fsl_buffer_empty;
  int rc;
  fsl_deck mf = fsl_deck_empty;
  fsl_cx * f = fcli_cx();
  char const * ofile = App.oFile;
  char seemsSafeEnough = 1;
  rc = fsl_buffer_fill_from_filename(&buf, mfile);
  assert(0==mf.rid);
  assert(!rc);
  assert(buf.used);















  f_out("Parsing this manifest: %s\n",mfile);
  mf.f = f /* this allows fsl_deck_parse() to populate mf with more
              data. */;
  rc = fsl_deck_parse(&mf, &buf);
  if(rc) goto end;
  assert(f == mf.f);
  f_out("Artifact type=%s, rid=%"FSL_ID_T_PFMT", uuid=%s\n",
        fsl_satype_cstr(mf.type), mf.rid, mf.uuid);

  if(App.saveDeck){
    if(!mf.uuid || !mf.rid){
      fsl_free(mf.uuid);
      mf.uuid = 0;
      mf.rid = 0;
    }
    rc = fsl_deck_save(&mf, false);
    MARKER(("save rc=%s\n",fsl_rc_cstr(rc)));
    if(rc) goto end;
  }
  
  if(!mf.rid){
    f_out("manifest rid is not set - "
          "assuming this comes from another repo?\n");
  }
  if(mf.B.uuid){
    if(mf.rid>0){
      f_out("Trying to fetch baseline manifest [%s]\n", mf.B.uuid);
      rc = fsl_deck_baseline_fetch(&mf);
      f_out("rc=%s, Baseline@%p\n", fsl_rc_cstr(rc), (void const *)mf.B.baseline);
      if(0){
        fsl_deck_output( mf.B.baseline, fsl_output_f_FILE, stdout);
      }
      /* assert(mf.B.baseline->F.list.used > 0); */
    }
  }
  if(mf.rid>0 && mf.R){
    fsl_card_F const * fc = NULL;
    int i;
    assert(!rc);
    /* f_out("File list:\n"); */
    for( i = 0; !(rc=fsl_deck_F_next(&mf, &fc)) && fc; ++i ){
      /* f_out("File: %.*s %s\n", 8, fc->uuid, fc->name); */
    }
    f_out("%d files seen in manifest(s).\n",i);
    assert(i>0 || 1==mf.rid /* special case! */);
  }

  if(App.checkRCard && mf.R){
    char _rCheck[FSL_STRLEN_MD5+1] = {0};
    char * rCheck = 0 ? NULL : _rCheck;
    assert(mf.R);
    f_out("Trying to re-calculate R-card: original=[%s]\n", mf.R);
    rc = fsl_deck_R_calc2(&mf, &rCheck);
    fcli_err_report(1);







|

<

<


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






<
|
<

<
<
<
<
<





<
<
<
<

<
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<







101
102
103
104
105
106
107
108
109

110

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

134

135





136
137
138
139
140




141

142
143
144
145
146
147
148
149













150
151
152
153
154
155
156
}

static int test_parse_1( char const * mfile ){
  fsl_buffer buf = fsl_buffer_empty;
  fsl_buffer bout = fsl_buffer_empty;
  int rc;
  fsl_deck mf = fsl_deck_empty;
  fsl_cx * const f = fcli_cx();
  char const * ofile = App.oFile;

  rc = fsl_buffer_fill_from_filename(&buf, mfile);

  assert(!rc);
  assert(buf.used);
  { /* See if we can find an in-repo match... */
    fsl_buffer hash = fsl_buffer_empty;
    fsl_sha1sum_buffer(&buf, &hash);
    mf.rid = fsl_uuid_to_rid(f, fsl_buffer_cstr(&hash));
    if(mf.rid<=0){
      fsl_buffer_reuse(&hash);
      fsl_sha3sum_buffer(&buf, &hash);
      mf.rid = fsl_uuid_to_rid(f, fsl_buffer_cstr(&hash));
      if(mf.rid<0){
        mf.rid = 0;
      }
    }
    fcli_err_reset();
  }

  f_out("Parsing this manifest: %s\n",mfile);
  mf.f = f /* this allows fsl_deck_parse() to populate mf with more
              data. */;
  rc = fsl_deck_parse(&mf, &buf);
  if(rc) goto end;
  assert(f == mf.f);

  f_out("Artifact type=%s\n", fsl_satype_cstr(mf.type));

  if(App.saveDeck){





    rc = fsl_deck_save(&mf, false);
    MARKER(("save rc=%s\n",fsl_rc_cstr(rc)));
    if(rc) goto end;
  }
  




  if(mf.B.uuid){

    f_out("Trying to fetch baseline manifest [%s]\n", mf.B.uuid);
    rc = fsl_deck_baseline_fetch(&mf);
    f_out("rc=%s, Baseline@%p\n", fsl_rc_cstr(rc), (void const *)mf.B.baseline);
    if(0){
      fsl_deck_output( mf.B.baseline, fsl_output_f_FILE, stdout);
    }
    fcli_err_reset(/*in case baseline was not in our repo*/);
  }













  if(App.checkRCard && mf.R){
    char _rCheck[FSL_STRLEN_MD5+1] = {0};
    char * rCheck = 0 ? NULL : _rCheck;
    assert(mf.R);
    f_out("Trying to re-calculate R-card: original=[%s]\n", mf.R);
    rc = fsl_deck_R_calc2(&mf, &rCheck);
    fcli_err_report(1);
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    rc = fsl_buffer_to_filename(&bout, ofile);
    assert(!rc);

    fsl_buffer sha = fsl_buffer_empty;
    rc = fsl_cx_hash_filename(f, 0, ofile, &sha);
    assert(!rc);
    f_out("SHA of [%s] = [%b]\n", ofile, &sha);
    seemsSafeEnough = (0==fsl_strcmp(fsl_buffer_cstr(&sha),
                                     mf.uuid));
    f_out("SHA match? %s: artifact.uuid=%s, rehashed file=%b\n",
          seemsSafeEnough ? "yes" : "NO!",
          mf.uuid, &sha);
    fsl_buffer_clear(&sha);
    if(!seemsSafeEnough){
      /* i would like to show a diff here, but parsing the manifest
         modifies buf, replacing spaces AND \n with \0 because doing
         so simplifies tokenization and provides a basis for a memory
         reuse case. Because it replaces both, we have no simple way
         of knowing which replacements we would need to make.
         So... we just load the modified buffer again.
      */
      rc = fsl_buffer_fill_from_filename(&buf, mfile);
      assert(!rc);
      if(buf.used){
        assert(bout.used==buf.used);
        assert(0!=fsl_buffer_compare(&buf,&bout));
        f_out("Diff of mismatched output (small timestamp "
              "diffs and PGP sigs are normal):\n");
        rc = fsl_diff_text( &buf, &bout, fsl_output_f_FILE,
                            stdout, 2, 0,
#if 1
                            0
#else
                            FSL_DIFF_SIDEBYSIDE
                            | FSL_DIFF_LINENO
#endif
                            );
        assert(!rc);
      }/*else it came from stdin and cannot be read again*/
    }
  }/*if ofile*/

  if(!mf.rid){
    f_out("No matching RID found: this is only an error if the artifact "
           "came from the current repo.\n");
  }

  if(App.doCrosslink){
    fsl_cx_flag_set(f, FSL_CX_F_SKIP_UNKNOWN_CROSSLINKS, 1);
    f_out("Disabling errors for currently-unhandled crosslink types.\n");
    if(mf.uuid){
      f_out("Crosslinking manifest #%d / %s ...\n", mf.rid, mf.uuid);
      rc = fsl_deck_crosslink_one( &mf );
      f_out("Crosslink says: %s\n", fsl_rc_cstr(rc));
      fcli_err_report(1);
    }
  }

  end:







<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


<
<
<
<
<



|
|







175
176
177
178
179
180
181





182



























183
184





185
186
187
188
189
190
191
192
193
194
195
196
    rc = fsl_buffer_to_filename(&bout, ofile);
    assert(!rc);

    fsl_buffer sha = fsl_buffer_empty;
    rc = fsl_cx_hash_filename(f, 0, ofile, &sha);
    assert(!rc);
    f_out("SHA of [%s] = [%b]\n", ofile, &sha);





    fsl_buffer_clear(&sha);



























  }/*if ofile*/






  if(App.doCrosslink){
    fsl_cx_flag_set(f, FSL_CX_F_SKIP_UNKNOWN_CROSSLINKS, 1);
    f_out("Disabling errors for currently-unhandled crosslink types.\n");
    if(mf.rid){
      f_out("Crosslinking manifest #%d ...\n", (int)mf.rid);
      rc = fsl_deck_crosslink_one( &mf );
      f_out("Crosslink says: %s\n", fsl_rc_cstr(rc));
      fcli_err_report(1);
    }
  }

  end:
Changes to f-apps/f-ci.c.
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  return rc ? rc : FCLI_RC_FLAG_AGAIN;
}

/**
    Just experimenting with fsl_xlink_listener() and friends.
*/
static int my_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.12s] (RID %"FSL_ID_T_PFMT")\n",
           fsl_satype_cstr(d->type), d->uuid, d->rid));
  return 0;
}

/** fsl_checkin_queue_f callback */
static int fsl_checkin_queue_f_my(const char * filename, void * state){
  ++*((fsl_size_t*)state);
  f_out("QUEUED: %s\n", filename);







|
|







84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  return rc ? rc : FCLI_RC_FLAG_AGAIN;
}

/**
    Just experimenting with fsl_xlink_listener() and friends.
*/
static int my_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact RID %" FSL_ID_T_PFMT "\n",
           fsl_satype_cstr(d->type), d->rid));
  return 0;
}

/** fsl_checkin_queue_f callback */
static int fsl_checkin_queue_f_my(const char * filename, void * state){
  ++*((fsl_size_t*)state);
  f_out("QUEUED: %s\n", filename);
Changes to f-apps/f-event.c.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* static */
int test_event_0(){
  fsl_cx * f = fcli_cx();
  fsl_db * db = fsl_cx_db_repo(f);
  fsl_deck DECK = fsl_deck_empty;
  fsl_deck * d = &DECK;
  fsl_buffer dout = fsl_buffer_empty;
  fsl_buffer hash = fsl_buffer_empty;
  int rc;
  double now = 0
    ? 2456525.3001276273 /* 2013-08-20T19:12:11.027 */
    : fsl_db_julian_now(db);
  if(!db){
    return fsl_cx_err_set(f, FSL_RC_MISUSE, "This app requires a repo.");
  }







<







28
29
30
31
32
33
34

35
36
37
38
39
40
41
/* static */
int test_event_0(){
  fsl_cx * f = fcli_cx();
  fsl_db * db = fsl_cx_db_repo(f);
  fsl_deck DECK = fsl_deck_empty;
  fsl_deck * d = &DECK;
  fsl_buffer dout = fsl_buffer_empty;

  int rc;
  double now = 0
    ? 2456525.3001276273 /* 2013-08-20T19:12:11.027 */
    : fsl_db_julian_now(db);
  if(!db){
    return fsl_cx_err_set(f, FSL_RC_MISUSE, "This app requires a repo.");
  }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    fsl_deck_save() existed.
  */
  rc = fsl_deck_output(d, fsl_output_f_buffer, &dout);
  fcli_err_report(1);
  assert(!rc);
  f_out("%b", &dout);

  fsl_sha1sum_buffer( &dout, &hash );
  d->uuid = fsl_buffer_str(&hash);
  hash = fsl_buffer_empty;
  FCLI_V(("SHA1=%s\n", d->uuid));
  
  if(App.crossLink){
    /* Write it! */
    fsl_db_transaction_begin(db);

    assert(d->rid<=0);
    assert(d->uuid);
    rc = fsl_content_put_ex(f, &dout, d->uuid,
                            0, 0, 0, &d->rid);
    fcli_err_report(1);
    assert(!rc);
    FCLI_V(("Event content record id: %"FSL_ID_T_PFMT"\n", d->rid));

    rc = fsl_deck_crosslink(d);
    fcli_err_report(0);
    assert(!rc);
    FCLI_V(("Crosslink of #%"FSL_ID_T_PFMT" [%.*s] finished.\n", d->rid, 12, d->uuid));
    if(!App.wetRun){
      FCLI_V(("dry-run mode: rolling back transaction.\n"));
    }
    fsl_db_transaction_end(db, rc || !App.wetRun);
  }

  fsl_buffer_clear(&dout);
  fsl_buffer_clear(&hash);
  fsl_deck_finalize(d);
  return rc;
}

int main(int argc, char const * const * argv ){
  int rc = 0;
  fcli_cliflag FCliFlags[] = {







<
<
<
<
<



>
|
|
<
<

<
|
<
<
<
<
<







<







87
88
89
90
91
92
93





94
95
96
97
98
99


100

101





102
103
104
105
106
107
108

109
110
111
112
113
114
115
    fsl_deck_save() existed.
  */
  rc = fsl_deck_output(d, fsl_output_f_buffer, &dout);
  fcli_err_report(1);
  assert(!rc);
  f_out("%b", &dout);






  if(App.crossLink){
    /* Write it! */
    fsl_db_transaction_begin(db);
    rc = fsl_deck_save(d, false);
    assert(!rc);
    assert(d->rid>0);


    fcli_err_report(1);

    FCLI_V(("Event content RID: %"FSL_ID_T_PFMT"\n", d->rid));





    if(!App.wetRun){
      FCLI_V(("dry-run mode: rolling back transaction.\n"));
    }
    fsl_db_transaction_end(db, rc || !App.wetRun);
  }

  fsl_buffer_clear(&dout);

  fsl_deck_finalize(d);
  return rc;
}

int main(int argc, char const * const * argv ){
  int rc = 0;
  fcli_cliflag FCliFlags[] = {
Changes to f-apps/f-ls.c.
106
107
108
109
110
111
112
113
114
115

116
117
118
119
120
121
122

  /* Find/load the checkin... */
  rc = fsl_deck_load_sym(f, d, lsVersion, FSL_SATYPE_CHECKIN);
  if(rc) goto end;


  /* Output the list...*/
  f_out("File list from manifest version '%s' [%.*s] "
        "(RID %"FSL_ID_T_PFMT")...\n",
        lsVersion, 12, d->uuid, (fsl_id_t)d->rid);

 if(d->B.uuid){
    f_out("This is a delta manifest from baseline [%.*s].\n",
          12, d->B.uuid);
  }
  if(fcli_is_verbose()) f_out("RID     ");
  f_out("%-12s P Name\n", "UUID");
  rc = fsl_deck_F_foreach(d, ls_F_card_v, NULL);







|

|
>







106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

  /* Find/load the checkin... */
  rc = fsl_deck_load_sym(f, d, lsVersion, FSL_SATYPE_CHECKIN);
  if(rc) goto end;


  /* Output the list...*/
  f_out("File list from manifest version '%s' [%.*z] "
        "(RID %"FSL_ID_T_PFMT")...\n",
        lsVersion, 12, fsl_rid_to_uuid(f, d->rid),
        d->rid);
 if(d->B.uuid){
    f_out("This is a delta manifest from baseline [%.*s].\n",
          12, d->B.uuid);
  }
  if(fcli_is_verbose()) f_out("RID     ");
  f_out("%-12s P Name\n", "UUID");
  rc = fsl_deck_F_foreach(d, ls_F_card_v, NULL);
Changes to f-apps/f-parseparty.c.
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
true/*clearLinks*/,
true/*rebuildLeaves*/,
NULL/*eventTypes*/
};

static int my_xlink_f(fsl_deck * d, void * state){
  if(fcli_is_verbose()>1){
    FCLI_VN(2,("Crosslinking rid %"FSL_ID_T_PFMT", uuid %s ...\n",
               d->rid, d->uuid));
  }else if(!App.quiet){
    f_out("x");
  }
  return 0;
}

static int test_parse_all(void){







|
|







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
true/*clearLinks*/,
true/*rebuildLeaves*/,
NULL/*eventTypes*/
};

static int my_xlink_f(fsl_deck * d, void * state){
  if(fcli_is_verbose()>1){
    FCLI_VN(2,("Crosslinking rid %"FSL_ID_T_PFMT" ...\n",
               d->rid));
  }else if(!App.quiet){
    f_out("x");
  }
  return 0;
}

static int test_parse_all(void){
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    runtimeC += fsl_timer_stop(&timer);
    RC;
    if(App.wellFormed && !fsl_might_be_artifact(&content)){
      wellFormedCheck = 0;
    }
    assert(mf.f);
    fsl_timer_start(&timer);
    rc = fsl_deck_parse(&mf, &content);
    runtimeP += fsl_timer_stop(&timer);
    if(fcli_is_verbose()<2 && !App.quiet){
      f_out(".");
      fflush(stdout);
    }
    if(rc){
      ++errCount;
      f_out("%sparse-offending artifact: %d / %s\n",
            lineBreak, (int)rid, zUuid);
      if(App.failFast){
        goto end;
      }
      fcli_err_report(1);
      continue;
    }
    assert(mf.rid);
    assert(mf.uuid);
    assert(mf.type>=0 && mf.type<FSL_SATYPE_count);
    assert(!content.mem && "Was handed off to mf.");
    if(!wellFormedCheck){
      f_out("\nWARNING: fsl_might_be_artifact() says that this "
            "is NOT an artifact: %s\n", mf.uuid);
    }
#if 0
    /* These assertions are wrong for phantom artifact cases. The
       libfossil tree contains some artifacts, for testing purposes,
       from the main fossil tree, which results in phantoms (the
       hashes those artifacts reference but which we don't have). */
    assert(mf.rid);
    assert(mf.uuid);
#endif
    FCLI_VN(2,("Parsed rid %d, uuid %s\n", (int)mf.rid, mf.uuid));
    if(App.crosslink){
      fsl_timer_start(&timer);
      rc = fsl_deck_crosslink(&mf);
      runtimeX += fsl_timer_stop(&timer);
      if(rc){
        if(FSL_RC_NOT_FOUND==rc){
          /* Assume this is an artifact, like 4b05c2c59fa61f1240d41949b305173c76d1395d,
             which exists as an artifact file but is not part of this project. */
          f_out("%sFAILED NON-FATALLY crosslinking rid %d, uuid %s\n",
                lineBreak, (int)mf.rid, mf.uuid, fsl_rc_cstr(rc));
          fcli_err_report(1);
          rc = 0;
        }else{
          f_out("%sFAILED crosslinking rid %d, uuid %s w/ rc=%s\n",
                lineBreak, (int)mf.rid, mf.uuid, fsl_rc_cstr(rc));
          fcli_err_report(1);
          if(App.failFast){
            break;
          }
        }
      }
    }







|
















<




|









|








|
|



|
|







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    runtimeC += fsl_timer_stop(&timer);
    RC;
    if(App.wellFormed && !fsl_might_be_artifact(&content)){
      wellFormedCheck = 0;
    }
    assert(mf.f);
    fsl_timer_start(&timer);
    rc = fsl_deck_parse2(&mf, &content, rid);
    runtimeP += fsl_timer_stop(&timer);
    if(fcli_is_verbose()<2 && !App.quiet){
      f_out(".");
      fflush(stdout);
    }
    if(rc){
      ++errCount;
      f_out("%sparse-offending artifact: %d / %s\n",
            lineBreak, (int)rid, zUuid);
      if(App.failFast){
        goto end;
      }
      fcli_err_report(1);
      continue;
    }
    assert(mf.rid);

    assert(mf.type>=0 && mf.type<FSL_SATYPE_count);
    assert(!content.mem && "Was handed off to mf.");
    if(!wellFormedCheck){
      f_out("\nWARNING: fsl_might_be_artifact() says that this "
            "is NOT an artifact: #%" FSL_ID_T_PFMT"\n", mf.rid);
    }
#if 0
    /* These assertions are wrong for phantom artifact cases. The
       libfossil tree contains some artifacts, for testing purposes,
       from the main fossil tree, which results in phantoms (the
       hashes those artifacts reference but which we don't have). */
    assert(mf.rid);
    assert(mf.uuid);
#endif
    FCLI_VN(2,("Parsed rid %d\n", (int)mf.rid));
    if(App.crosslink){
      fsl_timer_start(&timer);
      rc = fsl_deck_crosslink(&mf);
      runtimeX += fsl_timer_stop(&timer);
      if(rc){
        if(FSL_RC_NOT_FOUND==rc){
          /* Assume this is an artifact, like 4b05c2c59fa61f1240d41949b305173c76d1395d,
             which exists as an artifact file but is not part of this project. */
          f_out("%sFAILED NON-FATALLY crosslinking rid %d w/ rc=%s\n",
                lineBreak, (int)mf.rid, fsl_rc_cstr(rc));
          fcli_err_report(1);
          rc = 0;
        }else{
          f_out("%sFAILED crosslinking rid %d w/ rc=%s\n",
                lineBreak, (int)mf.rid, fsl_rc_cstr(rc));
          fcli_err_report(1);
          if(App.failFast){
            break;
          }
        }
      }
    }
331
332
333
334
335
336
337

338
339
340
341
342
343
344
345
346
347
348
349


350
351
352
353
354
355
356
  return rc;
}

int main(int argc, char const * const * argv ){
  int rc = 0;
  bool skipUnknown = false;
  bool useMCache = true;

  fsl_timer_state timer = fsl_timer_state_empty;
  fcli_cliflag FCliFlags[] = {
    FCLI_FLAG("t","types","list",&App.eventTypes,
              "List of letters of artifact types to parse: "
              "c=checkin, g=control (tags), w=wiki, "
              "t=ticket, n=technote, f=forum"),
    FCLI_FLAG_BOOL("F","fail-fast",&App.failFast,
                   "Stop processing at the first error."),
    FCLI_FLAG_BOOL("c","crosslink",&App.crosslink,
                   "Crosslink all parsed artifacts."),
    FCLI_FLAG_BOOL("q","quiet",&App.quiet,
                   "Disables certain output."),


    FCLI_FLAG_BOOL_INVERT("w","no-well-formed",&App.wellFormed,
                   "Disable comparing results of fsl_might_be_artifact() "
                   "with the parsing results to ensure that they "
                   "agree with each other."),
    FCLI_FLAG_BOOL_INVERT(0,"no-mcache",&useMCache,
                          "Disable use of the manifest cache."),
    FCLI_FLAG_BOOL(0,"random",&App.randomOrder,







>












>
>







330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  return rc;
}

int main(int argc, char const * const * argv ){
  int rc = 0;
  bool skipUnknown = false;
  bool useMCache = true;
  bool dryRun = false;
  fsl_timer_state timer = fsl_timer_state_empty;
  fcli_cliflag FCliFlags[] = {
    FCLI_FLAG("t","types","list",&App.eventTypes,
              "List of letters of artifact types to parse: "
              "c=checkin, g=control (tags), w=wiki, "
              "t=ticket, n=technote, f=forum"),
    FCLI_FLAG_BOOL("F","fail-fast",&App.failFast,
                   "Stop processing at the first error."),
    FCLI_FLAG_BOOL("c","crosslink",&App.crosslink,
                   "Crosslink all parsed artifacts."),
    FCLI_FLAG_BOOL("q","quiet",&App.quiet,
                   "Disables certain output."),
    FCLI_FLAG_BOOL(NULL,"dry-run",&dryRun,
                   "Rolls back any changes."),
    FCLI_FLAG_BOOL_INVERT("w","no-well-formed",&App.wellFormed,
                   "Disable comparing results of fsl_might_be_artifact() "
                   "with the parsing results to ensure that they "
                   "agree with each other."),
    FCLI_FLAG_BOOL_INVERT(0,"no-mcache",&useMCache,
                          "Disable use of the manifest cache."),
    FCLI_FLAG_BOOL(0,"random",&App.randomOrder,
379
380
381
382
383
384
385

386
387
388
389
390
391
392
393
394
395

396
397
398
  else if(fcli_has_unused_flags(0)) goto end;
  fsl_cx * const f = fcli_cx();
  fsl_cx_flag_set(f, FSL_CX_F_MANIFEST_CACHE, useMCache);
  if(skipUnknown){
    fsl_cx_flag_set(fcli_cx(), FSL_CX_F_SKIP_UNKNOWN_CROSSLINKS, 1);
  }
  fsl_timer_start(&timer);

  rc = test_parse_all();
  {
    f_out("Work took a total of %f ms of CPU time.\n",
          (double)(fsl_timer_reset(&timer) / 1000.0));
    if(useMCache){
      f_out("fsl_cx::cache::mcache hits = %u misses = %u\n",
            f->cache.mcache.hits,
            f->cache.mcache.misses);
    }
  }

  end:
  return fcli_end_of_main(rc);
}







>
|









>



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  else if(fcli_has_unused_flags(0)) goto end;
  fsl_cx * const f = fcli_cx();
  fsl_cx_flag_set(f, FSL_CX_F_MANIFEST_CACHE, useMCache);
  if(skipUnknown){
    fsl_cx_flag_set(fcli_cx(), FSL_CX_F_SKIP_UNKNOWN_CROSSLINKS, 1);
  }
  fsl_timer_start(&timer);
  if(dryRun) rc = fsl_cx_transaction_begin(f);
  if(0==rc) rc = test_parse_all();
  {
    f_out("Work took a total of %f ms of CPU time.\n",
          (double)(fsl_timer_reset(&timer) / 1000.0));
    if(useMCache){
      f_out("fsl_cx::cache::mcache hits = %u misses = %u\n",
            f->cache.mcache.hits,
            f->cache.mcache.misses);
    }
  }
  if(dryRun) fsl_cx_transaction_end(f, true);
  end:
  return fcli_end_of_main(rc);
}
Changes to f-apps/f-sanity.c.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  rc = fsl_sym_to_rid(f, "prev", FSL_SATYPE_CHECKIN, &rid);
  assert(!rc);
  assert(rid>0);
  rc = fsl_content_get(f, rid, &buf);
  assert(!rc);
  /* assert('B' == *fsl_buffer_cstr(&buf)); */
  d.f = f;
  rc = fsl_deck_parse(&d, &buf);
  assert(0==rc);
  fsl_buffer_clear(&buf);
  assert(FSL_SATYPE_CHECKIN==d.type);
  assert(rid==d.rid);
  uuid = fsl_rid_to_uuid(f, d.rid);
  assert(uuid);
  assert(0==fsl_uuidcmp(d.uuid, uuid));
  fsl_free(uuid);
  assert(d.D>0 && d.D<fsl_julian_now());
  fsl_deck_finalize(&d);
  assert(0==rc);

  slen = 0;
  str = fsl_db_g_text(db, &slen, "SELECT FSL_USER()");







|






<







77
78
79
80
81
82
83
84
85
86
87
88
89
90

91
92
93
94
95
96
97
  rc = fsl_sym_to_rid(f, "prev", FSL_SATYPE_CHECKIN, &rid);
  assert(!rc);
  assert(rid>0);
  rc = fsl_content_get(f, rid, &buf);
  assert(!rc);
  /* assert('B' == *fsl_buffer_cstr(&buf)); */
  d.f = f;
  rc = fsl_deck_parse2(&d, &buf, rid);
  assert(0==rc);
  fsl_buffer_clear(&buf);
  assert(FSL_SATYPE_CHECKIN==d.type);
  assert(rid==d.rid);
  uuid = fsl_rid_to_uuid(f, d.rid);
  assert(uuid);

  fsl_free(uuid);
  assert(d.D>0 && d.D<fsl_julian_now());
  fsl_deck_finalize(&d);
  assert(0==rc);

  slen = 0;
  str = fsl_db_g_text(db, &slen, "SELECT FSL_USER()");
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
  rc = fsl_deck_load_sym(f, &d, "trunk", FSL_SATYPE_CHECKIN);
  assert(0==rc);
  assert(d.f == f);
  fname = "auto.def";
  rc = fsl_deck_F_set(&d, fname, NULL, FSL_FILE_PERM_REGULAR, NULL);
  assert(FSL_RC_MISUSE==rc);
  rc = 0;
  fsl_free(d.uuid);
  d.uuid = NULL;
  d.rid = 0;
  rc = fsl_deck_F_set(&d, fname, NULL, FSL_FILE_PERM_REGULAR, NULL);
  assert(0==rc);
  rc = fsl_deck_F_set(&d, fname, NULL, FSL_FILE_PERM_REGULAR, NULL);
  assert(FSL_RC_NOT_FOUND==rc);
  fsl_deck_finalize(&d);
  f_out("%s() complete\n", __func__);







<
<







1067
1068
1069
1070
1071
1072
1073


1074
1075
1076
1077
1078
1079
1080
  rc = fsl_deck_load_sym(f, &d, "trunk", FSL_SATYPE_CHECKIN);
  assert(0==rc);
  assert(d.f == f);
  fname = "auto.def";
  rc = fsl_deck_F_set(&d, fname, NULL, FSL_FILE_PERM_REGULAR, NULL);
  assert(FSL_RC_MISUSE==rc);
  rc = 0;


  d.rid = 0;
  rc = fsl_deck_F_set(&d, fname, NULL, FSL_FILE_PERM_REGULAR, NULL);
  assert(0==rc);
  rc = fsl_deck_F_set(&d, fname, NULL, FSL_FILE_PERM_REGULAR, NULL);
  assert(FSL_RC_NOT_FOUND==rc);
  fsl_deck_finalize(&d);
  f_out("%s() complete\n", __func__);
2036
2037
2038
2039
2040
2041
2042




2043

2044
2045

2046
2047
2048
2049
2050
2051
2052
  }
  assert(fsl_repo_forbids_delta_manifests(f));
  rc = fsl_deck_load_sym(f, &d,
                         "f2f1612a0ca081462b4021d8126f394b6d6d8772",
                         FSL_SATYPE_CHECKIN);
  assert(0==rc && "We know f2f1612a0 to be a delta manifest.");
  assert(d.f == f);




  rc = fsl_deck_save(&d, false);

  assert(FSL_RC_ACCESS==rc && "Cannot save delta manifests.");
  f_out("Confirmed that we cannot save a delta in this repo.\n");

  fsl_cx_err_reset(f);
  assert(d.B.uuid && "We know this to be a delta manifest with 1 F-card.");
  assert(1==d.F.used);
  rc = fsl_deck_derive(&d);
  fsl_cx_transaction_end(f, true);
  assert(0==rc);
  assert(f == d.f);







>
>
>
>
|
>
|
|
>







2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
  }
  assert(fsl_repo_forbids_delta_manifests(f));
  rc = fsl_deck_load_sym(f, &d,
                         "f2f1612a0ca081462b4021d8126f394b6d6d8772",
                         FSL_SATYPE_CHECKIN);
  assert(0==rc && "We know f2f1612a0 to be a delta manifest.");
  assert(d.f == f);
  assert(d.rid>0);
  {
    fsl_id_t const oldRid = d.rid;
    d.rid = 0;
    rc = fsl_deck_save(&d, false);
    d.rid = oldRid;
    assert(FSL_RC_ACCESS==rc && "Cannot save delta manifests.");
    f_out("Confirmed that we cannot save a delta in this repo.\n");
  }
  fsl_cx_err_reset(f);
  assert(d.B.uuid && "We know this to be a delta manifest with 1 F-card.");
  assert(1==d.F.used);
  rc = fsl_deck_derive(&d);
  fsl_cx_transaction_end(f, true);
  assert(0==rc);
  assert(f == d.f);
Changes to f-apps/f-tag.c.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    printf pfexp;                                                   \
  } while(0)

/**
    Just experimenting with fsl_xlink_listener() and friends.
*/
static int tag_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.*s] (RID %"FSL_ID_T_PFMT")\n",
           fsl_satype_cstr(d->type), 8, d->uuid, d->rid));
  return *((char const *)state) /* demonstrate what happens when crosslinking fails. */
    ? FSL_RC_IO
    : 0;
}

static int MarkerA = 0;
static int MarkerT = 0;







|
|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    printf pfexp;                                                   \
  } while(0)

/**
    Just experimenting with fsl_xlink_listener() and friends.
*/
static int tag_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact RID %"FSL_ID_T_PFMT"\n",
           fsl_satype_cstr(d->type), d->rid));
  return *((char const *)state) /* demonstrate what happens when crosslinking fails. */
    ? FSL_RC_IO
    : 0;
}

static int MarkerA = 0;
static int MarkerT = 0;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    rc = fsl_deck_output(&mf, fsl_output_f_FILE, stdout);
    if(rc) goto end;
  }else{
    rc = fsl_deck_save( &mf, 0 );
  }

  if(!rc && vbose){
    f_out("Applied tags to [%s] for user [%s]. New tag: %.12s (RID %"FSL_ID_T_PFMT")\n",
          symToTag, userName, mf.uuid, mf.rid);
  }

  end:
  if(inTrans){
    if(fDryRun && vbose) f_out("Dry-run mode. Rolling back transaction.\n");
    fsl_db_transaction_end(db, fDryRun || rc);
    inTrans = 0;







|
|







211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    rc = fsl_deck_output(&mf, fsl_output_f_FILE, stdout);
    if(rc) goto end;
  }else{
    rc = fsl_deck_save( &mf, 0 );
  }

  if(!rc && vbose){
    f_out("Applied tags to [%s] for user [%s]. New tag: RID %"FSL_ID_T_PFMT"\n",
          symToTag, userName, mf.rid);
  }

  end:
  if(inTrans){
    if(fDryRun && vbose) f_out("Dry-run mode. Rolling back transaction.\n");
    fsl_db_transaction_end(db, fDryRun || rc);
    inTrans = 0;
Changes to f-apps/f-test-ciwoco.c.
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

  //////////////////////////////////////////////////////////////
  // Step 1: initialize our deck...
  if(App.addEmptyCommit){
    rc = fsl_deck_load_sym(f, &d, "trunk", FSL_SATYPE_CHECKIN);
    assert(0==rc);
    assert(f==d.f);
    f_out("Deriving from initial trunk checkin %s\n", d.uuid);
    rc = fsl_deck_derive(&d);
    assert(0==rc);
  }else{
    f_out("Creating initial commit with files.\n");
    fsl_deck_init(f, &d, FSL_SATYPE_CHECKIN);
    /* If we don't set a branch, we cannot resolve the checkins
       via a branch name! */







|







93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

  //////////////////////////////////////////////////////////////
  // Step 1: initialize our deck...
  if(App.addEmptyCommit){
    rc = fsl_deck_load_sym(f, &d, "trunk", FSL_SATYPE_CHECKIN);
    assert(0==rc);
    assert(f==d.f);
    f_out("Deriving from initial trunk checkin #%"FSL_ID_T_PFMT"\n", d.rid);
    rc = fsl_deck_derive(&d);
    assert(0==rc);
  }else{
    f_out("Creating initial commit with files.\n");
    fsl_deck_init(f, &d, FSL_SATYPE_CHECKIN);
    /* If we don't set a branch, we cannot resolve the checkins
       via a branch name! */
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    f_out("Added file: %s\n", fname);
  }

  //////////////////////////////////////////////////////////////
  // Step 4: save the deck...
  rc = fsl_deck_save(&d, false);
  assert(0==rc);
  f_out("Saved checkin %s\n", d.uuid);

  //////////////////////////////////////////////////////////////
  // Step 5: ...
  f_out("Now we'll try again so we can ensure that deltaing "
        "of parent file content works.\n");
  fsl_deck_derive(&d);
  setup_deck(&d, "Modified Makefile.in.");
  fname = "Makefile.in";
  rc = fsl_buffer_fill_from_filename(&content, fname);
  assert(0==rc);
  rc = fsl_buffer_append(&content,
                         "\n# This is an edit. There are many "
                         "like it, but this one is mine.\n", -1);
  assert(0==rc);
  rc = fsl_deck_F_set_content(&d, fname, &content,
                              FSL_FILE_PERM_REGULAR, NULL);
  assert(0==rc);
  f_out("Added file: %s\n", fname);
  rc = fsl_deck_save(&d, false);
  assert(0==rc);
  f_out("Saved checkin %s\n", d.uuid);
  f_out("You can confirm that the previous file version is delta'd "
        "by running:\n    f-acat -R %s --raw rid:X\n"
        "where X is the lowest-numbered entry in this list:\n",
        App.repoDbName);
  fsl_db_each( fsl_cx_db(f), fsl_stmt_each_f_dump, NULL,
               "SELECT m.fid FROM mlink m, filename f "
               "WHERE m.fnid=f.fnid and f.name=%Q",







|




















|







130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    f_out("Added file: %s\n", fname);
  }

  //////////////////////////////////////////////////////////////
  // Step 4: save the deck...
  rc = fsl_deck_save(&d, false);
  assert(0==rc);
  f_out("Saved checkin #%"FSL_ID_T_PFMT"\n", d.rid);

  //////////////////////////////////////////////////////////////
  // Step 5: ...
  f_out("Now we'll try again so we can ensure that deltaing "
        "of parent file content works.\n");
  fsl_deck_derive(&d);
  setup_deck(&d, "Modified Makefile.in.");
  fname = "Makefile.in";
  rc = fsl_buffer_fill_from_filename(&content, fname);
  assert(0==rc);
  rc = fsl_buffer_append(&content,
                         "\n# This is an edit. There are many "
                         "like it, but this one is mine.\n", -1);
  assert(0==rc);
  rc = fsl_deck_F_set_content(&d, fname, &content,
                              FSL_FILE_PERM_REGULAR, NULL);
  assert(0==rc);
  f_out("Added file: %s\n", fname);
  rc = fsl_deck_save(&d, false);
  assert(0==rc);
  f_out("Saved checkin #%"FSL_ID_T_PFMT"\n", d.rid);
  f_out("You can confirm that the previous file version is delta'd "
        "by running:\n    f-acat -R %s --raw rid:X\n"
        "where X is the lowest-numbered entry in this list:\n",
        App.repoDbName);
  fsl_db_each( fsl_cx_db(f), fsl_stmt_each_f_dump, NULL,
               "SELECT m.fid FROM mlink m, filename f "
               "WHERE m.fnid=f.fnid and f.name=%Q",
Changes to f-apps/f-wiki.c.
120
121
122
123
124
125
126
127

128
129
130
131
132
133
134
135
             ? "Time (UTC)"
             : "Time (local time)"),
            "UUID", "Size");
    }
    if(vbose){
      f_out("%-6"FSL_ID_T_PFMT" ", d->rid);
    }
    f_out("%-20s %.*s  %-6"FSL_SIZE_T_PFMT" %s\n",

          ts, 12, d->uuid, (fsl_size_t)d->W.used, d->L);
    fsl_free(ts);
  }
#if defined(DEBUG)
  {
    int rc;
    fsl_id_t ridCheck = 0;
    rc = fsl_wiki_latest_rid(f, d->L, &ridCheck);







|
>
|







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
             ? "Time (UTC)"
             : "Time (local time)"),
            "UUID", "Size");
    }
    if(vbose){
      f_out("%-6"FSL_ID_T_PFMT" ", d->rid);
    }
    f_out("%-20s %.*z  %-6"FSL_SIZE_T_PFMT" %s\n",
          ts, 12, fsl_rid_to_uuid(f, d->rid),
          (fsl_size_t)d->W.used, d->L);
    fsl_free(ts);
  }
#if defined(DEBUG)
  {
    int rc;
    fsl_id_t ridCheck = 0;
    rc = fsl_wiki_latest_rid(f, d->L, &ridCheck);
281
282
283
284
285
286
287
288
289

290
291
292
293
294
295
296
  fcli_command_help(aCommandsWiki, false);
}

/**
    Just experimenting with fsl_xlink_listener() and friends.
 */
static int wiki_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.*s] (RID %"FSL_ID_T_PFMT")\n",
           fsl_satype_cstr(d->type), 8, d->uuid, d->rid));

  return *((char const *)state) /* demonstrate what happens when crosslinking fails. */
    ? FSL_RC_NYI
    : 0;
}

int main(int argc, char const * const * argv ){
  int rc;







|
|
>







282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  fcli_command_help(aCommandsWiki, false);
}

/**
    Just experimenting with fsl_xlink_listener() and friends.
 */
static int wiki_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.*z] (RID %"FSL_ID_T_PFMT")\n",
          fsl_satype_cstr(d->type), 8,
          fsl_rid_to_uuid(d->f, d->rid), d->rid));
  return *((char const *)state) /* demonstrate what happens when crosslinking fails. */
    ? FSL_RC_NYI
    : 0;
}

int main(int argc, char const * const * argv ){
  int rc;
Changes to include/fossil-scm/fossil-internal.h.
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
    important for card ordering in generated manifests.

    This routine expects to get passed (fsl_card_J**) (namely from
    fsl_list entries), and will not work on an array of J-cards.
*/
FSL_EXPORT int fsl_qsort_cmp_J_cards( void const * lhs, void const * rhs );

/** @internal

    The internal version of fsl_deck_parse(). See that function
    for details regarding everything but the 3rd argument.

    If you happen to know the _correct_ RID for the deck being
    parsed, pass it as the rid argument, else pass 0. A negative
    value will result in a FSL_RC_RANGE error. This value is (or
    will be) only used as an optimization in other places and only
    if d->f is not NULL.  Passing a positive value has no effect on
    how the content is parsed or on the result - it only affects
    internal details/optimizations.
*/
FSL_EXPORT int fsl_deck_parse2(fsl_deck * d, fsl_buffer * src, fsl_id_t rid);

/** @internal

    This function updates the repo and/or global config databases
    with links between the dbs intended for various fossil-level
    bookkeeping and housecleaning. These links are not essential to
    fossil's functionality but assist in certain "global"
    operations.







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1411
1412
1413
1414
1415
1416
1417















1418
1419
1420
1421
1422
1423
1424
    important for card ordering in generated manifests.

    This routine expects to get passed (fsl_card_J**) (namely from
    fsl_list entries), and will not work on an array of J-cards.
*/
FSL_EXPORT int fsl_qsort_cmp_J_cards( void const * lhs, void const * rhs );
















/** @internal

    This function updates the repo and/or global config databases
    with links between the dbs intended for various fossil-level
    bookkeeping and housecleaning. These links are not essential to
    fossil's functionality but assist in certain "global"
    operations.
Changes to include/fossil-scm/fossil-repo.h.
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
     determined if a given card type is legal for a given value of
     this member. APIs which add/set cards use that to determine if
     the operation requested by the client is semantically legal.
  */
  fsl_satype_e type;

  /**
     DB repo.blob.rid value. Normally set by fsl_deck_parse().
  */
  fsl_id_t rid;

  /**
     Gets set by fsl_deck_parse() to the hash/UUID of the
     manifest it parsed.
  */
  fsl_uuid_str uuid;

  /**
     The Fossil context responsible for this deck. Though this data
     type is normally, at least conceptually, free of any given fossil
     context, many related algorithms need a context in order to
     perform db- or caching-related work, as well as to simplify error
     message propagation. We store this as a struct member to keep all
     such algorithms from redundantly requiring both pieces of







|



<
<
<
<
<
<







283
284
285
286
287
288
289
290
291
292
293






294
295
296
297
298
299
300
     determined if a given card type is legal for a given value of
     this member. APIs which add/set cards use that to determine if
     the operation requested by the client is semantically legal.
  */
  fsl_satype_e type;

  /**
     DB repo.blob.rid value. Normally set by fsl_deck_load_rid().
  */
  fsl_id_t rid;







  /**
     The Fossil context responsible for this deck. Though this data
     type is normally, at least conceptually, free of any given fossil
     context, many related algorithms need a context in order to
     perform db- or caching-related work, as well as to simplify error
     message propagation. We store this as a struct member to keep all
     such algorithms from redundantly requiring both pieces of
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
   Initialized-with-defaults fsl_deck structure, intended for
   in-struct and const copy initialization.
*/
#define fsl_deck_empty_m {                                  \
    FSL_SATYPE_ANY /*type*/,                                \
    0/*rid*/,                                            \
    NULL/*uuid*/,                                         \
    NULL/*f*/,                                            \
    {/*A*/ NULL /* name */,                               \
           NULL /* tgt */,                                   \
           NULL /* src */},                                  \
    {/*B*/ NULL /*uuid*/,                                 \
           NULL /*baseline*/},                               \
    NULL /* C */,                                       \







<







493
494
495
496
497
498
499

500
501
502
503
504
505
506
/**
   Initialized-with-defaults fsl_deck structure, intended for
   in-struct and const copy initialization.
*/
#define fsl_deck_empty_m {                                  \
    FSL_SATYPE_ANY /*type*/,                                \
    0/*rid*/,                                            \

    NULL/*f*/,                                            \
    {/*A*/ NULL /* name */,                               \
           NULL /* tgt */,                                   \
           NULL /* src */},                                  \
    {/*B*/ NULL /*uuid*/,                                 \
           NULL /*baseline*/},                               \
    NULL /* C */,                                       \
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
   state, but does not free() deck. Is a no-op if deck is NULL. As a
   special case, the (allocStamp, f) members of deck are kept intact.

   @see fsl_deck_finalize()
   @see fsl_deck_malloc()
   @see fsl_deck_clean2()
*/
FSL_EXPORT void fsl_deck_clean(fsl_deck *deck);

/**
   A variant of fsl_deck_clean() which "returns" its content buffer
   for re-use by transferring, after ensuring proper cleanup of its
   internals, its own content buffer's bytes into the given target
   buffer. Note that decks created "manually" do not have any content
   buffer contents, but those loaded via fsl_deck_load_rid() do. This
   function will fsl_buffer_swap() the contents of the given buffer
   (if any) with its own buffer, clean up its newly-acquired memory
   (tgt's previous contents, if any), and fsl_buffer_reuse() the
   output buffer.

   If tgt is NULL, this behaves exactly like fsl_deck_clean().
*/
FSL_EXPORT void fsl_deck_clean2(fsl_deck *deck, fsl_buffer *tgt);

/**
   Frees all memory owned by deck (see fsl_deck_clean()).  If deck
   was allocated using fsl_deck_malloc() then this function
   fsl_free()'s it, otherwise it does not free it.

   @see fsl_deck_malloc()







|














|







542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
   state, but does not free() deck. Is a no-op if deck is NULL. As a
   special case, the (allocStamp, f) members of deck are kept intact.

   @see fsl_deck_finalize()
   @see fsl_deck_malloc()
   @see fsl_deck_clean2()
*/
FSL_EXPORT void fsl_deck_clean(fsl_deck * const deck);

/**
   A variant of fsl_deck_clean() which "returns" its content buffer
   for re-use by transferring, after ensuring proper cleanup of its
   internals, its own content buffer's bytes into the given target
   buffer. Note that decks created "manually" do not have any content
   buffer contents, but those loaded via fsl_deck_load_rid() do. This
   function will fsl_buffer_swap() the contents of the given buffer
   (if any) with its own buffer, clean up its newly-acquired memory
   (tgt's previous contents, if any), and fsl_buffer_reuse() the
   output buffer.

   If tgt is NULL, this behaves exactly like fsl_deck_clean().
*/
FSL_EXPORT void fsl_deck_clean2(fsl_deck * const deck, fsl_buffer * const tgt);

/**
   Frees all memory owned by deck (see fsl_deck_clean()).  If deck
   was allocated using fsl_deck_malloc() then this function
   fsl_free()'s it, otherwise it does not free it.

   @see fsl_deck_malloc()
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
   wiki pages this is their normal name (e.g. "MyWikiPage"). For
   events and tickets it is their full 40-byte UUID.

   uuidSrc is the UUID of the attachment blob itself. If it is NULL
   or empty then this card indicates that the attachment will be
   "deleted" (insofar as anything is ever deleted in Fossil).
*/
FSL_EXPORT int fsl_deck_A_set( fsl_deck * mf, char const * filename,
                               char const * target,
                               fsl_uuid_cstr uuidSrc);

/**
   Sets or unsets (if uuidBaseline is NULL or empty) the B-card for
   the given manifest to a copy of the given UUID. Returns 0 on
   success, FSL_RC_MISUSE if !mf, FSL_RC_OOM on allocation







|







592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
   wiki pages this is their normal name (e.g. "MyWikiPage"). For
   events and tickets it is their full 40-byte UUID.

   uuidSrc is the UUID of the attachment blob itself. If it is NULL
   or empty then this card indicates that the attachment will be
   "deleted" (insofar as anything is ever deleted in Fossil).
*/
FSL_EXPORT int fsl_deck_A_set( fsl_deck * const mf, char const * filename,
                               char const * target,
                               fsl_uuid_cstr uuidSrc);

/**
   Sets or unsets (if uuidBaseline is NULL or empty) the B-card for
   the given manifest to a copy of the given UUID. Returns 0 on
   success, FSL_RC_MISUSE if !mf, FSL_RC_OOM on allocation
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
   fsl_card_is_legal(mf->type,...)).

   Sidebar: the ability to unset this card is unusual within this API,
   and is a requirement the library-internal delta manifest creation
   process. Most of the card-setting APIs, even when they are
   described as working like this one, do not accept NULL hash values.
*/
FSL_EXPORT int fsl_deck_B_set( fsl_deck * mf, fsl_uuid_cstr uuidBaseline);

/**
   Semantically identical to fsl_deck_B_set() but sets the C-card and
   does not place a practical limit on the comment's length.  comment
   must be the comment text for the change being applied.  If the
   given length is negative, fsl_strlen() is used to determine its
   length.
*/
FSL_EXPORT int fsl_deck_C_set( fsl_deck * mf, char const * comment, fsl_int_t cardLen);

/**
   Sets mf's D-card as a Julian Date value. Returns FSL_RC_MISUSE if
   !mf, FSL_RC_RANGE if date is negative, FSL_RC_TYPE if a D-card is
   not valid for the given deck, else 0. Passing a value of 0
   effectively unsets the card.
*/
FSL_EXPORT int fsl_deck_D_set( fsl_deck * mf, double date);

/**
   Sets the E-card in the given deck. date may not be negative -
   use fsl_db_julian_now() or fsl_julian_now() to get a default
   time if needed.  Retursn FSL_RC_MISUSE if !mf or !uuid,
   FSL_RC_RANGE if date is not positive, FSL_RC_RANGE if uuid is
   not a valid UUID string.

   Note that the UUID for an event, unlike most other UUIDs, need
   not be calculated - it may be a random hex string, but it must
   pass the fsl_is_uuid() test. Use fsl_db_random_hex() to generate
   random UUIDs. When editing events, e.g. using the HTML UI, only
   the most recent event with the same UUID is shown. So when
   updating events, be sure to apply the same UUID to the edited
   copies before saving them.
*/
FSL_EXPORT int fsl_deck_E_set( fsl_deck * mf, double date, fsl_uuid_cstr uuid);

/**
   Adds a new F-card to the given deck. The uuid argument is required
   to be NULL or pass the fsl_is_uuid() test. The name must be a
   "simplified path name" (as determined by fsl_is_simple_pathname()),
   or FSL_RC_RANGE is returned. Note that a NULL uuid is only valid
   when constructing a delta manifest, and this routine will return







|








|







|
















|







617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
   fsl_card_is_legal(mf->type,...)).

   Sidebar: the ability to unset this card is unusual within this API,
   and is a requirement the library-internal delta manifest creation
   process. Most of the card-setting APIs, even when they are
   described as working like this one, do not accept NULL hash values.
*/
FSL_EXPORT int fsl_deck_B_set( fsl_deck * const mf, fsl_uuid_cstr uuidBaseline);

/**
   Semantically identical to fsl_deck_B_set() but sets the C-card and
   does not place a practical limit on the comment's length.  comment
   must be the comment text for the change being applied.  If the
   given length is negative, fsl_strlen() is used to determine its
   length.
*/
FSL_EXPORT int fsl_deck_C_set( fsl_deck * const mf, char const * comment, fsl_int_t cardLen);

/**
   Sets mf's D-card as a Julian Date value. Returns FSL_RC_MISUSE if
   !mf, FSL_RC_RANGE if date is negative, FSL_RC_TYPE if a D-card is
   not valid for the given deck, else 0. Passing a value of 0
   effectively unsets the card.
*/
FSL_EXPORT int fsl_deck_D_set( fsl_deck * const mf, double date);

/**
   Sets the E-card in the given deck. date may not be negative -
   use fsl_db_julian_now() or fsl_julian_now() to get a default
   time if needed.  Retursn FSL_RC_MISUSE if !mf or !uuid,
   FSL_RC_RANGE if date is not positive, FSL_RC_RANGE if uuid is
   not a valid UUID string.

   Note that the UUID for an event, unlike most other UUIDs, need
   not be calculated - it may be a random hex string, but it must
   pass the fsl_is_uuid() test. Use fsl_db_random_hex() to generate
   random UUIDs. When editing events, e.g. using the HTML UI, only
   the most recent event with the same UUID is shown. So when
   updating events, be sure to apply the same UUID to the edited
   copies before saving them.
*/
FSL_EXPORT int fsl_deck_E_set( fsl_deck * const mf, double date, fsl_uuid_cstr uuid);

/**
   Adds a new F-card to the given deck. The uuid argument is required
   to be NULL or pass the fsl_is_uuid() test. The name must be a
   "simplified path name" (as determined by fsl_is_simple_pathname()),
   or FSL_RC_RANGE is returned. Note that a NULL uuid is only valid
   when constructing a delta manifest, and this routine will return
786
787
788
789
790
791
792
793

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
   - Moves d->uuid into d->P
   - Clears d->rid
   - Clears any other members which need to be (re)set by the new
     child/derived version.
   - It specifically keeps d->F intact OR creates a new one (see below).

   Returns 0 on success, FSL_RC_OOM on an allocation error,
   FSL_RC_MISUSE if d->uuid is NULL or d->rid<=0. If d->type is not

   FSL_SATYPE_CHECKIN, FSL_RC_TYPE is returned. On error, d may be
   left in an inconsistent state and must not be used further except
   to pass it to fsl_deck_finalize().

   The intention of this function is to simplify creation of decks
   which are to be used for creating checkins without requiring a
   checkin.

   To avoid certain corner cases, this function does not allow
   creation of delta manifests. If d has a B-card then it is a delta.
   This function clears its B-card and recreates the F-card list using
   the B-card's F-card list and any F-cards from the current delta. In
   other words, it creates a new baseline manifest.

   TODO: extend this to support other inheritable deck types, e.g.
   wiki, forum posts, and technotes.

   @see fsl_deck_F_set_content()
*/
FSL_EXPORT int fsl_deck_derive(fsl_deck * d);

/**
   Callback type for use with fsl_deck_F_foreach() and
   friends. Implementations must return 0 on success, FSL_RC_BREAK
   to abort looping without an error, and any other value on error.
*/
typedef int (*fsl_card_F_visitor_f)(fsl_card_F const * fc,







|
>
|
|
|
















|







779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
   - Moves d->uuid into d->P
   - Clears d->rid
   - Clears any other members which need to be (re)set by the new
     child/derived version.
   - It specifically keeps d->F intact OR creates a new one (see below).

   Returns 0 on success, FSL_RC_OOM on an allocation error,
   FSL_RC_MISUSE if d->rid<=0 (i.e. the deck has never been saved or
   was not loaded from the db. If d->type is not FSL_SATYPE_CHECKIN,
   FSL_RC_TYPE is returned. On error, d may be left in an inconsistent
   state and must not be used further except to pass it to
   fsl_deck_finalize().

   The intention of this function is to simplify creation of decks
   which are to be used for creating checkins without requiring a
   checkin.

   To avoid certain corner cases, this function does not allow
   creation of delta manifests. If d has a B-card then it is a delta.
   This function clears its B-card and recreates the F-card list using
   the B-card's F-card list and any F-cards from the current delta. In
   other words, it creates a new baseline manifest.

   TODO: extend this to support other inheritable deck types, e.g.
   wiki, forum posts, and technotes.

   @see fsl_deck_F_set_content()
*/
FSL_EXPORT int fsl_deck_derive(fsl_deck * const d);

/**
   Callback type for use with fsl_deck_F_foreach() and
   friends. Implementations must return 0 on success, FSL_RC_BREAK
   to abort looping without an error, and any other value on error.
*/
typedef int (*fsl_card_F_visitor_f)(fsl_card_F const * fc,
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034

1035
1036
1037
1038
1039
1040
1041






1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
FSL_EXPORT int fsl_card_F_content( fsl_cx * f, fsl_card_F const * fc,
                                   fsl_buffer * dest );

/**
   Sets the 'G' card on a forum-post deck to a copy of the given
   UUID.
*/
FSL_EXPORT int fsl_deck_G_set( fsl_deck * mf, fsl_uuid_cstr uuid);
/**
   Sets the 'H' card on a forum-post deck to a copy of the given
   comment. If cardLen is negative then fsl_strlen() is used to
   calculate its length.
 */
FSL_EXPORT int fsl_deck_H_set( fsl_deck * mf, char const * comment, fsl_int_t cardLen);
/**
   Sets the 'I' card on a forum-post deck to a copy of the given
   UUID.
*/
FSL_EXPORT int fsl_deck_I_set( fsl_deck * mf, fsl_uuid_cstr uuid);

/**
   Adds a J-card to the given deck, setting/updating the given ticket
   property key to the given value. The key is required but the value
   is optional (may be NULL). If isAppend then the value is appended
   to any existing value, otherwise it replaces any existing value.

   It is currently unclear whether it is legal to include multiple
   J cards for the same key in the same control artifact, in
   particular if their isAppend values differ.

   Returns 0 on success, FSL_RC_MISUSE if !mf or !key, FSL_RC_RANGE
   if !*field, FSL_RC_TYPE if mf is of a type for which J cards are
   not legal (see fsl_card_is_legal()), FSL_RC_OOM on allocation
   error.
*/
FSL_EXPORT int fsl_deck_J_add( fsl_deck * mf, char isAppend,
                               char const * key, char const * value );

/**
   Semantically identical fsl_deck_B_set() but sets the K-card and
   does not accept a NULL value.  uuid must be the UUID of the ticket
   this change is being applied to.
*/
FSL_EXPORT int fsl_deck_K_set( fsl_deck * mf, fsl_uuid_cstr uuid);

/**
   Semantically identical fsl_deck_B_set() but sets the L-card.
   title must be the wiki page title text of the wiki page this
   change is being applied to.
*/
FSL_EXPORT int fsl_deck_L_set( fsl_deck * mf, char const *title, fsl_int_t len);

/**
   Adds the given UUID as an M-card entry. Returns 0 on success, or:

   FSL_RC_MISUSE if !mf or !uuid

   FSL_RC_TYPE if fsl_deck_check_type(mf,'M') returns false.

   FSL_RC_RANGE if !fsl_is_uuid(uuid).

   FSL_RC_OOM if memory allocation fails while adding the entry.
*/
FSL_EXPORT int fsl_deck_M_add( fsl_deck * mf, fsl_uuid_cstr uuid );

/**
   Semantically identical to fsl_deck_B_set() but sets the N card.
   mimeType must be the content mime type for comment text of the
   change being applied.
*/
FSL_EXPORT int fsl_deck_N_set( fsl_deck * mf, char const *mimeType, fsl_int_t len);

/**
   Adds the given UUID as a parent of the given change record. If len
   is less than 0 then fsl_strlen(parentUuid) is used to determine
   its length. Returns FSL_RC_MISUE if !mf, !parentUuid, or
   !*parentUuid. Returns FSL_RC_RANGE if parentUuid is not 40
   bytes long.


   The first P-card added to a deck MUST be the UUID of its primary
   parent (one which was not involved in a merge operation). All
   others (from merges) are considered "non-primary."

*/
FSL_EXPORT int fsl_deck_P_add( fsl_deck * mf, fsl_uuid_cstr parentUuid);







/**
   If d contains a P card with the given index, this returns the
   RID corresponding to the UUID at that index. Returns a negative
   value on error, 0 if there is no for that index or the index
   is out of bounds.
*/
FSL_EXPORT fsl_id_t fsl_deck_P_get_id(fsl_deck * d, int index);

/**
   Adds a Q-card record to the given deck. The type argument must
   be negative for a backed-out change, positive for a cherrypicked
   change.  target must be a valid UUID string. If baseline is not
   NULL then it also must be a valid UUID.

   Returns 0 on success, non-0 on error. FSL_RC_MISUSE if !mf
   or !target, FSL_RC_RANGE if target/baseline are not valid
   UUID strings (baseline may be NULL).
*/
FSL_EXPORT int fsl_deck_Q_add( fsl_deck * mf, int type,
                    fsl_uuid_cstr target,
                    fsl_uuid_cstr baseline );

/**
   Functionally identical to fsl_deck_B_set() except that it sets
   the R-card. Returns 0 on succes, FSL_RC_RANGE if md5 is not NULL
   or exactly FSL_STRLEN_MD5 bytes long (not including trailing
   NUL). If md5==NULL the current R value is cleared.

   It would be highly unusual to have to set the R-card manually,
   as its calculation is quite intricate/intensive. See
   fsl_deck_R_calc() and fsl_deck_unshuffle() for details
*/
FSL_EXPORT int fsl_deck_R_set( fsl_deck * mf, char const *md5);

/**
   Adds a new T-card (tag) entry to the given deck.

   If uuid is not NULL and fsl_is_uuid(uuid) returns false then
   this function returns FSL_RC_RANGE. If uuid is NULL then it is
   assumed to be the UUID of the currently-being-constructed
   artifact in which the tag is contained (which appears as the '*'
   character in generated artifacts).

   Returns 0 on success. Returns FSL_RC_MISUE if !mf or
   !name. Returns FSL_RC_TYPE (and update's mf's error state with a
   message) if the T card is not legal for mf (see
   fsl_card_is_legal()).  Returns FSL_RC_RANGE if !*name, tagType
   is invalid, or if uuid is not NULL and fsl_is_uuid(uuid)
   return false. Returns FSL_RC_OOM if an allocation fails.
*/
FSL_EXPORT int fsl_deck_T_add( fsl_deck * mf, fsl_tagtype_e tagType,
                               fsl_uuid_cstr uuid, char const * name,
                               char const * value);

/**
   Adds the given tag instance to the given manifest.
   Returns 0 on success, FSL_RC_MISUSE if either argument
   is NULL, FSL_RC_OOM if appending the tag to the list
   fails.

   On success ownership of t is passed to mf. On error ownership is
   not modified.
*/
FSL_EXPORT int fsl_deck_T_add2( fsl_deck * mf, fsl_card_T * t);

/**
   A convenience form of fsl_deck_T_add() which adds two propagating
   tags to the given deck: "branch" with a value of branchName and
   "sym-branchName" with no value.

   Returns 0 on success. Returns FSL_RC_OOM on allocation error and







|





|




|
















|







|






|












|






|



|
|
|
|
>






|
>
>
>
>
>
>


|
|
|
|

|











|













|

















|












|







951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
FSL_EXPORT int fsl_card_F_content( fsl_cx * f, fsl_card_F const * fc,
                                   fsl_buffer * dest );

/**
   Sets the 'G' card on a forum-post deck to a copy of the given
   UUID.
*/
FSL_EXPORT int fsl_deck_G_set( fsl_deck * const mf, fsl_uuid_cstr uuid);
/**
   Sets the 'H' card on a forum-post deck to a copy of the given
   comment. If cardLen is negative then fsl_strlen() is used to
   calculate its length.
 */
FSL_EXPORT int fsl_deck_H_set( fsl_deck * const mf, char const * comment, fsl_int_t cardLen);
/**
   Sets the 'I' card on a forum-post deck to a copy of the given
   UUID.
*/
FSL_EXPORT int fsl_deck_I_set( fsl_deck * const mf, fsl_uuid_cstr uuid);

/**
   Adds a J-card to the given deck, setting/updating the given ticket
   property key to the given value. The key is required but the value
   is optional (may be NULL). If isAppend then the value is appended
   to any existing value, otherwise it replaces any existing value.

   It is currently unclear whether it is legal to include multiple
   J cards for the same key in the same control artifact, in
   particular if their isAppend values differ.

   Returns 0 on success, FSL_RC_MISUSE if !mf or !key, FSL_RC_RANGE
   if !*field, FSL_RC_TYPE if mf is of a type for which J cards are
   not legal (see fsl_card_is_legal()), FSL_RC_OOM on allocation
   error.
*/
FSL_EXPORT int fsl_deck_J_add( fsl_deck * const mf, char isAppend,
                               char const * key, char const * value );

/**
   Semantically identical fsl_deck_B_set() but sets the K-card and
   does not accept a NULL value.  uuid must be the UUID of the ticket
   this change is being applied to.
*/
FSL_EXPORT int fsl_deck_K_set( fsl_deck * const mf, fsl_uuid_cstr uuid);

/**
   Semantically identical fsl_deck_B_set() but sets the L-card.
   title must be the wiki page title text of the wiki page this
   change is being applied to.
*/
FSL_EXPORT int fsl_deck_L_set( fsl_deck * const mf, char const *title, fsl_int_t len);

/**
   Adds the given UUID as an M-card entry. Returns 0 on success, or:

   FSL_RC_MISUSE if !mf or !uuid

   FSL_RC_TYPE if fsl_deck_check_type(mf,'M') returns false.

   FSL_RC_RANGE if !fsl_is_uuid(uuid).

   FSL_RC_OOM if memory allocation fails while adding the entry.
*/
FSL_EXPORT int fsl_deck_M_add( fsl_deck * const mf, fsl_uuid_cstr uuid );

/**
   Semantically identical to fsl_deck_B_set() but sets the N card.
   mimeType must be the content mime type for comment text of the
   change being applied.
*/
FSL_EXPORT int fsl_deck_N_set( fsl_deck * const mf, char const *mimeType, fsl_int_t len);

/**
   Adds the given UUID as a parent of the given change record. If len
   is less than 0 then fsl_strlen(parentUuid) is used to determine its
   length. Returns FSL_RC_MISUE if !*parentUuid. Returns FSL_RC_RANGE
   if parentUuid does not pass fsl_is_uuid().

   Results are undefined if parentUuid is NULL.

   The first P-card added to a deck MUST be the UUID of its primary
   parent (one which was not involved in a merge operation). All
   others (from merges) are considered "non-primary."

*/
FSL_EXPORT int fsl_deck_P_add( fsl_deck * const mf, fsl_uuid_cstr parentUuid);
/**
   A convenience wrapper around fsl_deck_P_add() which resolves the given
   RID to its UUID and passes it on to fsl_deck_P_add(). Returns non-0
   on error.
*/
FSL_EXPORT int fsl_deck_P_add_rid( fsl_deck * const mf, fsl_id_t parentRid );

/**
   If d contains a P card with the given index, this returns the RID
   corresponding to the UUID at that index. Returns a negative value
   on error, 0 if there is no entry for that index (the index is out
   of bounds).
*/
FSL_EXPORT fsl_id_t fsl_deck_P_get_id(fsl_deck * const d, int index);

/**
   Adds a Q-card record to the given deck. The type argument must
   be negative for a backed-out change, positive for a cherrypicked
   change.  target must be a valid UUID string. If baseline is not
   NULL then it also must be a valid UUID.

   Returns 0 on success, non-0 on error. FSL_RC_MISUSE if !mf
   or !target, FSL_RC_RANGE if target/baseline are not valid
   UUID strings (baseline may be NULL).
*/
FSL_EXPORT int fsl_deck_Q_add( fsl_deck * const mf, int type,
                    fsl_uuid_cstr target,
                    fsl_uuid_cstr baseline );

/**
   Functionally identical to fsl_deck_B_set() except that it sets
   the R-card. Returns 0 on succes, FSL_RC_RANGE if md5 is not NULL
   or exactly FSL_STRLEN_MD5 bytes long (not including trailing
   NUL). If md5==NULL the current R value is cleared.

   It would be highly unusual to have to set the R-card manually,
   as its calculation is quite intricate/intensive. See
   fsl_deck_R_calc() and fsl_deck_unshuffle() for details
*/
FSL_EXPORT int fsl_deck_R_set( fsl_deck * const mf, char const *md5);

/**
   Adds a new T-card (tag) entry to the given deck.

   If uuid is not NULL and fsl_is_uuid(uuid) returns false then
   this function returns FSL_RC_RANGE. If uuid is NULL then it is
   assumed to be the UUID of the currently-being-constructed
   artifact in which the tag is contained (which appears as the '*'
   character in generated artifacts).

   Returns 0 on success. Returns FSL_RC_MISUE if !mf or
   !name. Returns FSL_RC_TYPE (and update's mf's error state with a
   message) if the T card is not legal for mf (see
   fsl_card_is_legal()).  Returns FSL_RC_RANGE if !*name, tagType
   is invalid, or if uuid is not NULL and fsl_is_uuid(uuid)
   return false. Returns FSL_RC_OOM if an allocation fails.
*/
FSL_EXPORT int fsl_deck_T_add( fsl_deck * const mf, fsl_tagtype_e tagType,
                               fsl_uuid_cstr uuid, char const * name,
                               char const * value);

/**
   Adds the given tag instance to the given manifest.
   Returns 0 on success, FSL_RC_MISUSE if either argument
   is NULL, FSL_RC_OOM if appending the tag to the list
   fails.

   On success ownership of t is passed to mf. On error ownership is
   not modified.
*/
FSL_EXPORT int fsl_deck_T_add2( fsl_deck * const mf, fsl_card_T * t);

/**
   A convenience form of fsl_deck_T_add() which adds two propagating
   tags to the given deck: "branch" with a value of branchName and
   "sym-branchName" with no value.

   Returns 0 on success. Returns FSL_RC_OOM on allocation error and
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
FSL_EXPORT int fsl_deck_R_calc2(fsl_deck *d, char ** tgt);

/**
   Semantically identical fsl_deck_B_set() but sets the U-card.
   userName must be the user who's name should be recorded for
   this change.
*/
FSL_EXPORT int fsl_deck_U_set( fsl_deck * mf, char const *userName);

/**
   Semantically identical fsl_deck_B_set() but sets the W-card.
   content must be the page content of the Wiki page or Event this
   change is being applied to.
*/
FSL_EXPORT int fsl_deck_W_set( fsl_deck * mf, char const *content, fsl_int_t len);

/**
   Must be called to initialize a newly-created/allocated deck
   instance. This function clears out all contents of the d
   parameter except for its (f, type, allocStamp) members, sets its
   (f, type) members, and leaves d->allocStamp intact.
*/







|






|







1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
FSL_EXPORT int fsl_deck_R_calc2(fsl_deck *d, char ** tgt);

/**
   Semantically identical fsl_deck_B_set() but sets the U-card.
   userName must be the user who's name should be recorded for
   this change.
*/
FSL_EXPORT int fsl_deck_U_set( fsl_deck * const mf, char const *userName);

/**
   Semantically identical fsl_deck_B_set() but sets the W-card.
   content must be the page content of the Wiki page or Event this
   change is being applied to.
*/
FSL_EXPORT int fsl_deck_W_set( fsl_deck * const mf, char const *content, fsl_int_t len);

/**
   Must be called to initialize a newly-created/allocated deck
   instance. This function clears out all contents of the d
   parameter except for its (f, type, allocStamp) members, sets its
   (f, type) members, and leaves d->allocStamp intact.
*/
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352

   If d->rid and d->uuid are set when this is called, it is assumed
   that we are saving existing or phantom content, and in that
   case:

   - An existing phantom is populated with the new content.

   - If an existing record is found with a non-0 size then it is
   not modified but this is currently not treated as an error (for
   historical reasons, though one could argue that it should result
   in FSL_RC_ALREADY_EXISTS).

   If d->rid and d->uuid are not set when this is called then... on
   success, d->rid and d->uuid will contain the values held by
   their counterparts in the blob table. They will only be set on
   success because they would otherwise refer to db records which
   get destroyed when the transaction rolls back.








|
|
|
|







1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353

   If d->rid and d->uuid are set when this is called, it is assumed
   that we are saving existing or phantom content, and in that
   case:

   - An existing phantom is populated with the new content.

   - If d->rid is positive or if an existing record is found with a
   non-0 size then it is not modified but this is currently not
   treated as an error (for historical reasons, though one could argue
   that it should result in FSL_RC_ALREADY_EXISTS).

   If d->rid and d->uuid are not set when this is called then... on
   success, d->rid and d->uuid will contain the values held by
   their counterparts in the blob table. They will only be set on
   success because they would otherwise refer to db records which
   get destroyed when the transaction rolls back.

1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378

   Maintenance reminder: this function also does a small bit of
   artifact-type-specific processing.

   @see fsl_deck_output()
   @see fsl_content_put_ex()
*/
FSL_EXPORT int fsl_deck_save( fsl_deck * d, bool isPrivate );

/**
    This starts a transaction (possibly nested) on the repository db
    and initializes some temporary db state needed for the
    crosslinking certain artifact types. It "should" (see below) be
    called at the start of the crosslinking process. Crosslinking
    *can* work without this but certain steps for certain (subject to







|







1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379

   Maintenance reminder: this function also does a small bit of
   artifact-type-specific processing.

   @see fsl_deck_output()
   @see fsl_content_put_ex()
*/
FSL_EXPORT int fsl_deck_save( fsl_deck * const d, bool isPrivate );

/**
    This starts a transaction (possibly nested) on the repository db
    and initializes some temporary db state needed for the
    crosslinking certain artifact types. It "should" (see below) be
    called at the start of the crosslinking process. Crosslinking
    *can* work without this but certain steps for certain (subject to
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
   from ::fsl_deck_empty is FSL_SATYPE_ANY, so normally clients do
   not need to set this (unless they want to, as a small
   optimization).

   On success it returns 0 and d will be updated with the state
   from the input artifact. (Ideally, outputing d via
   fsl_deck_output() will produce a lossless copy of the original.)
   d->uuid will be set to the SHA1 of the input artifact, ignoring
   any surrounding PGP signature for hashing purposes.

   If d->f has an opened repository db and the parsed artifact has a
   counterpart in the database (determined via a hash match) then
   d->rid is set to the record ID.

   On error, if there is error information to propagate beyond the
   result code then it is stored in d->f (if that is not NULL),
   else in d->error. Whether or not such error info is propagated
   depends on the type of error, but anything more trivial than
   invalid arguments will be noted there.








<
<
<
<
<
<







1449
1450
1451
1452
1453
1454
1455






1456
1457
1458
1459
1460
1461
1462
   from ::fsl_deck_empty is FSL_SATYPE_ANY, so normally clients do
   not need to set this (unless they want to, as a small
   optimization).

   On success it returns 0 and d will be updated with the state
   from the input artifact. (Ideally, outputing d via
   fsl_deck_output() will produce a lossless copy of the original.)







   On error, if there is error information to propagate beyond the
   result code then it is stored in d->f (if that is not NULL),
   else in d->error. Whether or not such error info is propagated
   depends on the type of error, but anything more trivial than
   invalid arguments will be noted there.

1475
1476
1477
1478
1479
1480
1481







1482
1483













1484
1485
1486
1487
1488
1489
1490

   - FSL_RC_SYNTAX on syntax errors.

   - FSL_RC_CONSISTENCY if validation of a Z-card fails.

   - Any number of errors coming from the allocator, database, or
   fsl_deck APIs used here.







*/
FSL_EXPORT int fsl_deck_parse(fsl_deck * d, fsl_buffer * src);














/**
   Quickly determines whether the content held by the given buffer
   "might" be a structural artifact. It performs a fast sanity check
   for prominent features which can be checked either in O(1) or very
   short O(N) time (with a fixed N). If it returns false then the
   given buffer's contents are, with 100% certainty, *not* a







>
>
>
>
>
>
>

|
>
>
>
>
>
>
>
>
>
>
>
>
>







1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505

   - FSL_RC_SYNTAX on syntax errors.

   - FSL_RC_CONSISTENCY if validation of a Z-card fails.

   - Any number of errors coming from the allocator, database, or
   fsl_deck APIs used here.

   ACHTUNG API CHANGE: prior to 2021-10-20, this routine set d->rid
   (and the now-removed d->uuid) based on the hash of the input buffer
   if a matching record could be found in the db. That proved to be
   a huge performance hit and was removed.

   @see fsl_deck_parse2()
*/
FSL_EXPORT int fsl_deck_parse(fsl_deck * const d, fsl_buffer * const src);

/**
    This variant of fsl_deck_parse() works identically to that
    function except for the 3rd argument.

    If you happen to know the _correct_ RID for the deck being parsed,
    pass it as the rid argument, else pass 0. A negative value will
    result in a FSL_RC_RANGE error. This value is (or will be) only
    used as an optimization in other places. Passing a positive value
    has no effect on how the content is parsed or on the result - it
    only affects internal details/optimizations.
*/
FSL_EXPORT int fsl_deck_parse2(fsl_deck * const d, fsl_buffer * const src, fsl_id_t rid);

/**
   Quickly determines whether the content held by the given buffer
   "might" be a structural artifact. It performs a fast sanity check
   for prominent features which can be checked either in O(1) or very
   short O(N) time (with a fixed N). If it returns false then the
   given buffer's contents are, with 100% certainty, *not* a
1499
1500
1501
1502
1503
1504
1505
1506









1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
   Fossil artifact. If rid==0 the current checkout (if opened) is
   used. (Trivia: there can never be a checkout with rid==0 but
   rid==0 is sometimes valid for an new/empty repo devoid of
   commits). If type==FSL_SATYPE_ANY then it will allow any type of
   control artifact, else it returns FSL_RC_TYPE if the loaded
   artifact is of the wrong type.

   Returns 0 on success.










   d may be partially populated on error, and the caller must
   eventually pass it to fsl_deck_finalize() resp. fsl_deck_clean()
   regardless of success or error. This function "could" clean it
   up on error, but leaving it partially populated makes debugging
   easier. If the error was an artifact type mismatch then d will
   "probably" be properly populated but will not hold the type of
   artifact requested. It "should" otherwise be well-formed because
   parsing errors occur before the type check can happen, but
   parsing of invalid manifests might also trigger a FSL_RC_TYPE
   error of a different nature. The morale of the storage is: if
   this function returns non-0, assume d is useless and needs to be
   cleaned up.

   f's error state may be updated on error (for anything more
   serious than basic argument validation errors).

   On success d->f is set to f.

   @see fsl_deck_load_sym()
*/
FSL_EXPORT int fsl_deck_load_rid( fsl_cx * f, fsl_deck * d,
                                  fsl_id_t rid, fsl_satype_e type );

/**
   A convenience form of fsl_deck_load_rid() which uses
   fsl_sym_to_rid() to convert symbolicName into an artifact RID.  See
   fsl_deck_load_rid() for the symantics of the first, second, and
   fourth arguments, as well as the return value. See fsl_sym_to_rid()
   for the allowable values of symbolicName.

   @see fsl_deck_load_rid()
*/
FSL_EXPORT int fsl_deck_load_sym( fsl_cx * f, fsl_deck * d,
                                  char const * symbolicName,
                                  fsl_satype_e type );

/**
   Loads the baseline manifest specified in d->B.uuid, if any and if
   necessary. Returns 0 on success. If d->B.baseline is already loaded
   or d->B.uuid is NULL (in which case there is no baseline), it







|
>
>
>
>
>
>
>
>
>

















|



|











|







1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
   Fossil artifact. If rid==0 the current checkout (if opened) is
   used. (Trivia: there can never be a checkout with rid==0 but
   rid==0 is sometimes valid for an new/empty repo devoid of
   commits). If type==FSL_SATYPE_ANY then it will allow any type of
   control artifact, else it returns FSL_RC_TYPE if the loaded
   artifact is of the wrong type.

   Returns 0 on success. Results are undefined if f or d are NULL. The
   potential error result codes include, but are not limited to:

   - FSL_RC_OOM

   - FSL_RC_RANGE if rid is negative or is 0 and no checkout is
     opened.

   - FSL_RC_TYPE if `type` is not `FSL_SATYPE_ANY` and the loaded
     result is of any artifact type other than `type`.

   d may be partially populated on error, and the caller must
   eventually pass it to fsl_deck_finalize() resp. fsl_deck_clean()
   regardless of success or error. This function "could" clean it
   up on error, but leaving it partially populated makes debugging
   easier. If the error was an artifact type mismatch then d will
   "probably" be properly populated but will not hold the type of
   artifact requested. It "should" otherwise be well-formed because
   parsing errors occur before the type check can happen, but
   parsing of invalid manifests might also trigger a FSL_RC_TYPE
   error of a different nature. The morale of the storage is: if
   this function returns non-0, assume d is useless and needs to be
   cleaned up.

   f's error state may be updated on error (for anything more
   serious than basic argument validation errors).

   On success this function sets d->rid to rid.

   @see fsl_deck_load_sym()
*/
FSL_EXPORT int fsl_deck_load_rid( fsl_cx * const f, fsl_deck * const d,
                                  fsl_id_t rid, fsl_satype_e type );

/**
   A convenience form of fsl_deck_load_rid() which uses
   fsl_sym_to_rid() to convert symbolicName into an artifact RID.  See
   fsl_deck_load_rid() for the symantics of the first, second, and
   fourth arguments, as well as the return value. See fsl_sym_to_rid()
   for the allowable values of symbolicName.

   @see fsl_deck_load_rid()
*/
FSL_EXPORT int fsl_deck_load_sym( fsl_cx * const f, fsl_deck * const d,
                                  char const * symbolicName,
                                  fsl_satype_e type );

/**
   Loads the baseline manifest specified in d->B.uuid, if any and if
   necessary. Returns 0 on success. If d->B.baseline is already loaded
   or d->B.uuid is NULL (in which case there is no baseline), it
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
   Error cases include: either argument is NULL, uuid does not
   appear to be a full or partial UUID (or is too long),
   uuid is ambiguous (try providing a longer one)

   This implementation is more efficient when given a full,
   valid UUID (one for which fsl_is_uuid() returns true).
*/
FSL_EXPORT fsl_id_t fsl_uuid_to_rid( fsl_cx * f, char const * uuid );

/**
   The opposite of fsl_uuid_to_rid(), this returns the UUID string
   of the given blob record ID. Ownership of the string is passed
   to the caller and it must eventually be freed using
   fsl_free(). Returns NULL on error (invalid arguments or f has no
   repo opened) or if no blob record is found. If no record is
   found, f's error state is updated with an explanation of the
   problem.
*/
FSL_EXPORT fsl_uuid_str fsl_rid_to_uuid(fsl_cx * f, fsl_id_t rid);

/**
   Works like fsl_rid_to_uuid() but assigns the UUID to the given
   buffer, re-using its memory, if any. Returns 0 on success,
   FSL_RC_MISUSE if rid is not positive, FSL_RC_OOM on allocation
   error, and FSL_RC_NOT_FOUND if no blob entry matching the given rid
   is found.
*/
FSL_EXPORT int fsl_rid_to_uuid2(fsl_cx * f, fsl_id_t rid, fsl_buffer *uuid);

/**
   This works identically to fsl_rid_to_uuid() except that it will
   only resolve to a UUID if an artifact matching the given type has
   that UUID. If no entry is found, f's error state gets updated
   with a description of the problem.








|










|








|







2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
   Error cases include: either argument is NULL, uuid does not
   appear to be a full or partial UUID (or is too long),
   uuid is ambiguous (try providing a longer one)

   This implementation is more efficient when given a full,
   valid UUID (one for which fsl_is_uuid() returns true).
*/
FSL_EXPORT fsl_id_t fsl_uuid_to_rid( fsl_cx * const f, char const * uuid );

/**
   The opposite of fsl_uuid_to_rid(), this returns the UUID string
   of the given blob record ID. Ownership of the string is passed
   to the caller and it must eventually be freed using
   fsl_free(). Returns NULL on error (invalid arguments or f has no
   repo opened) or if no blob record is found. If no record is
   found, f's error state is updated with an explanation of the
   problem.
*/
FSL_EXPORT fsl_uuid_str fsl_rid_to_uuid(fsl_cx * const f, fsl_id_t rid);

/**
   Works like fsl_rid_to_uuid() but assigns the UUID to the given
   buffer, re-using its memory, if any. Returns 0 on success,
   FSL_RC_MISUSE if rid is not positive, FSL_RC_OOM on allocation
   error, and FSL_RC_NOT_FOUND if no blob entry matching the given rid
   is found.
*/
FSL_EXPORT int fsl_rid_to_uuid2(fsl_cx * const f, fsl_id_t rid, fsl_buffer *uuid);

/**
   This works identically to fsl_rid_to_uuid() except that it will
   only resolve to a UUID if an artifact matching the given type has
   that UUID. If no entry is found, f's error state gets updated
   with a description of the problem.

Changes to src/checkin.c.
800
801
802
803
804
805
806

807
808
809
810
811
812
813
814
815
816
817




818
819
820





821
822

823

824

825

826
827
828
829
830
831
832
    static char const * errNoFilesMsg =
      "No files have changed. Cowardly refusing to commit.";
    static int const errNoFilesRc = FSL_RC_NOOP;
    fsl_deck * pBase = NULL /* baseline for delta generation purposes */;
    fsl_size_t szD = 0, szB = 0 /* see commentary below */;
    if(basedOnVid && deltaPolicy!=0){
      /* Figure out a baseline for a delta manifest... */

      rc = fsl_deck_load_rid(f, &dBase, basedOnVid, FSL_SATYPE_CHECKIN);
      RC;
      if(dBase.B.uuid){
        /* dBase is a delta. Let's use its baseline for manifest
           generation.
        */
        fsl_id_t const baseRid = fsl_uuid_to_rid(f, dBase.B.uuid);
        fsl_deck_finalize(&dBase);
        assert(baseRid>0);
        rc = fsl_deck_load_rid(f, &dBase, baseRid,
                               FSL_SATYPE_CHECKIN);




        RC;
      }else{
        /* dBase version is a suitable baseline. */





      }
      pBase = &dBase;

      /* MARKER(("Baseline = %d / %s\n", (int)pBase->rid, pBase->uuid)); */

      rc = fsl_deck_B_set(d, pBase->uuid);

      RC;

    }
    rc = fsl_checkin_calc_F_cards2(f, d, pBase, basedOnVid,
                                   &szD, opt);
    /*MARKER(("szD=%d\n", (int)szD));*/
    RC;
    if(basedOnVid && !szD){
      rc = fsl_cx_err_set(f, errNoFilesRc, errNoFilesMsg);







>




|
<


|
|
|
>
>
>
>



>
>
>
>
>
|
<
>

>
|
>

>







800
801
802
803
804
805
806
807
808
809
810
811
812

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830

831
832
833
834
835
836
837
838
839
840
841
842
843
844
    static char const * errNoFilesMsg =
      "No files have changed. Cowardly refusing to commit.";
    static int const errNoFilesRc = FSL_RC_NOOP;
    fsl_deck * pBase = NULL /* baseline for delta generation purposes */;
    fsl_size_t szD = 0, szB = 0 /* see commentary below */;
    if(basedOnVid && deltaPolicy!=0){
      /* Figure out a baseline for a delta manifest... */
      fsl_uuid_str bUuid = NULL /* UUID for d's B-card */;
      rc = fsl_deck_load_rid(f, &dBase, basedOnVid, FSL_SATYPE_CHECKIN);
      RC;
      if(dBase.B.uuid){
        /* dBase is a delta. Let's use its baseline for manifest
           generation. */

        fsl_id_t const baseRid = fsl_uuid_to_rid(f, dBase.B.uuid);
        fsl_deck_finalize(&dBase);
        if(baseRid>0){
          rc = fsl_deck_load_rid(f, &dBase, baseRid,
                                 FSL_SATYPE_CHECKIN);
        }else{
          rc = fsl_cx_err_get(f, NULL, NULL);
          assert(0!=rc);
        }
        RC;
      }else{
        /* dBase version is a suitable baseline. */
        bUuid = fsl_rid_to_uuid(f, basedOnVid);
        if(!bUuid){
          assert(f->error.code);
          rc = f->error.code;
          RC;
        }

      }
      /* MARKER(("Baseline = %d / %s\n", (int)pBase->rid, pBase->uuid)); */
      assert(dBase.B.uuid || bUuid);
      rc = fsl_deck_B_set(d, dBase.B.uuid ? dBase.B.uuid : bUuid);
      fsl_free(bUuid);
      RC;
      pBase = &dBase;
    }
    rc = fsl_checkin_calc_F_cards2(f, d, pBase, basedOnVid,
                                   &szD, opt);
    /*MARKER(("szD=%d\n", (int)szD));*/
    RC;
    if(basedOnVid && !szD){
      rc = fsl_cx_err_set(f, errNoFilesRc, errNoFilesMsg);
1035
1036
1037
1038
1039
1040
1041

1042
1043
1044
1045
1046
1047
1048
    RC;
  }

  end:
#undef RC
  fsl_stmt_finalize(&q);
  fsl_deck_finalize(&dBase);

  d->B.baseline = NULL /* if it was set, it was &dBase */;
  if(rc && !f->error.code){
    if(dbR->error.code) fsl_cx_uplift_db_error(f, dbR);
    else if(dbC->error.code) fsl_cx_uplift_db_error(f, dbC);
    else if(f->dbMain->error.code) fsl_cx_uplift_db_error(f, f->dbMain);
  }
  return rc;







>







1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
    RC;
  }

  end:
#undef RC
  fsl_stmt_finalize(&q);
  fsl_deck_finalize(&dBase);
  assert(NULL==d->B.baseline || &dBase==d->B.baseline);
  d->B.baseline = NULL /* if it was set, it was &dBase */;
  if(rc && !f->error.code){
    if(dbR->error.code) fsl_cx_uplift_db_error(f, dbR);
    else if(dbC->error.code) fsl_cx_uplift_db_error(f, dbC);
    else if(f->dbMain->error.code) fsl_cx_uplift_db_error(f, f->dbMain);
  }
  return rc;
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  }

  if(opt->calcRCard) f->flags |= FSL_CX_F_CALC_R_CARD;
  else f->flags &= ~FSL_CX_F_CALC_R_CARD;
  rc = fsl_deck_save( d, opt->isPrivate );
  RC;
  assert(d->rid>0);
  assert(d->uuid);
  /* Now get vfile back into shape. We do not do a vfile scan
     because that loses state like add/rm-queued files. */
  rc = fsl_db_exec_multi(dbC,
                         "DELETE FROM vfile WHERE vid<>"
                         "%" FSL_ID_T_PFMT ";"
                         "UPDATE vfile SET vid=%" FSL_ID_T_PFMT ";"
                         "DELETE FROM vfile WHERE deleted AND "
                         "fsl_is_enqueued(id); "
                         "UPDATE vfile SET rid=mrid, mhash=NULL, "
                         "chnged=0, deleted=0, origname=NULL "
                         "WHERE fsl_is_enqueued(id)",
                         vid, d->rid);
  if(!rc) rc = fsl_ckout_version_write(f, d->rid, d->uuid);
  RC;
  assert(d->f == f);
  rc = fsl_checkin_add_unsent(f, d->rid);
  RC;
  rc = fsl_ckout_clear_merge_state(f);
  RC;
  /*







<












|







1164
1165
1166
1167
1168
1169
1170

1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
  }

  if(opt->calcRCard) f->flags |= FSL_CX_F_CALC_R_CARD;
  else f->flags &= ~FSL_CX_F_CALC_R_CARD;
  rc = fsl_deck_save( d, opt->isPrivate );
  RC;
  assert(d->rid>0);

  /* Now get vfile back into shape. We do not do a vfile scan
     because that loses state like add/rm-queued files. */
  rc = fsl_db_exec_multi(dbC,
                         "DELETE FROM vfile WHERE vid<>"
                         "%" FSL_ID_T_PFMT ";"
                         "UPDATE vfile SET vid=%" FSL_ID_T_PFMT ";"
                         "DELETE FROM vfile WHERE deleted AND "
                         "fsl_is_enqueued(id); "
                         "UPDATE vfile SET rid=mrid, mhash=NULL, "
                         "chnged=0, deleted=0, origname=NULL "
                         "WHERE fsl_is_enqueued(id)",
                         vid, d->rid);
  if(!rc) rc = fsl_ckout_version_write(f, d->rid, NULL);
  RC;
  assert(d->f == f);
  rc = fsl_checkin_add_unsent(f, d->rid);
  RC;
  rc = fsl_ckout_clear_merge_state(f);
  RC;
  /*
1245
1246
1247
1248
1249
1250
1251
1252
1253

1254
1255
1256
1257
1258
1259
1260

1261
1262
1263
1264
1265
1266
1267
1268
  if(inTrans){
    if(rc) fsl_db_transaction_rollback(dbR);
    else{
      rc = fsl_db_transaction_commit(dbR);
      if(!rc){
        if(newRid) *newRid = d->rid;
        if(newUuid){
          *newUuid = d->uuid;
          d->uuid = NULL /* transfer ownership */;

        }
      }
    }
  }
  if(rc && !f->error.code){
    if(dbR->error.code) fsl_cx_uplift_db_error(f, dbR);
    else if(f->dbMain->error.code) fsl_cx_uplift_db_error(f, f->dbMain);

  }
  fsl_checkin_discard(f);
  fsl_deck_finalize(d);
  return rc;
}


#undef MARKER







|
|
>





<
|
>








1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271

1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
  if(inTrans){
    if(rc) fsl_db_transaction_rollback(dbR);
    else{
      rc = fsl_db_transaction_commit(dbR);
      if(!rc){
        if(newRid) *newRid = d->rid;
        if(newUuid){
          if(NULL==(*newUuid = fsl_rid_to_uuid(f, d->rid))){
            rc = FSL_RC_OOM;
          }
        }
      }
    }
  }
  if(rc && !f->error.code){

    if(f->dbMain->error.code) fsl_cx_uplift_db_error(f, f->dbMain);
    else f->error.code = rc;
  }
  fsl_checkin_discard(f);
  fsl_deck_finalize(d);
  return rc;
}


#undef MARKER
Changes to src/cx.c.
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
*/
static int fsl_cx_ckout_version_set(fsl_cx *f, fsl_id_t rid,
                                    fsl_uuid_cstr uuid){
  char * u = 0;
  assert(rid>=0);
  u = uuid
    ? fsl_strdup(uuid)
    : (rid ? fsl_rid_to_uuid(f, rid) : 0);
  if(rid && !u) return FSL_RC_OOM;
  f->ckout.rid = rid;
  fsl_free(f->ckout.uuid);
  f->ckout.uuid = u;
  fsl_ckout_mtime_set(f);
  return 0;
}







|







1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
*/
static int fsl_cx_ckout_version_set(fsl_cx *f, fsl_id_t rid,
                                    fsl_uuid_cstr uuid){
  char * u = 0;
  assert(rid>=0);
  u = uuid
    ? fsl_strdup(uuid)
    : (rid ? fsl_rid_to_uuid(f, rid) : NULL);
  if(rid && !u) return FSL_RC_OOM;
  f->ckout.rid = rid;
  fsl_free(f->ckout.uuid);
  f->ckout.uuid = u;
  fsl_ckout_mtime_set(f);
  return 0;
}
Changes to src/deck.c.
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#define SFREE(X) fsl_deck_clean_string(m, &m->X)
#define SLIST(X) fsl_list_clear(&m->X, fsl_list_v_card_string_free, m)
#define CBUF(X) fsl_buffer_clear(&m->X)
static void fsl_deck_clean_string(fsl_deck *m, char **member){
  fsl_deck_free_string(m, *member);
  *member = 0;
}
static void fsl_deck_clean_version(fsl_deck *m){
  fsl_deck_clean_string(m, &m->uuid);
  m->rid = 0;
}
static void fsl_deck_clean_A(fsl_deck *m){
  SFREE(A.name);
  SFREE(A.tgt);
  SFREE(A.src);
}
static void fsl_deck_clean_B(fsl_deck *m){
  if(m->B.baseline){
    assert(!m->B.baseline->B.uuid && "Baselines cannot have a B-card. API misuse?");
    fsl_deck_finalize(m->B.baseline);
    m->B.baseline = NULL;
  }
  SFREE(B.uuid);
}
static void fsl_deck_clean_C(fsl_deck *m){
  fsl_deck_clean_string(m, &m->C);
}
static void fsl_deck_clean_E(fsl_deck *m){
  fsl_deck_clean_string(m, &m->E.uuid);
  m->E = fsl_deck_empty.E;
}
static void fsl_deck_clean_F(fsl_deck *m){
  if(m->F.list){
    fsl_card_F_list_finalize(&m->F);
    m->F = fsl_deck_empty.F;
  }
}
static void fsl_deck_clean_G(fsl_deck *m){
  fsl_deck_clean_string(m, &m->G);
}
static void fsl_deck_clean_H(fsl_deck *m){
  fsl_deck_clean_string(m, &m->H);
}
static void fsl_deck_clean_I(fsl_deck *m){
  fsl_deck_clean_string(m, &m->I);
}
static void fsl_deck_clean_J(fsl_deck *m, bool alsoListMem){
  fsl_card_J_list_free(&m->J, alsoListMem);
}
static void fsl_deck_clean_K(fsl_deck *m){
  fsl_deck_clean_string(m, &m->K);
}
static void fsl_deck_clean_L(fsl_deck *m){
  fsl_deck_clean_string(m, &m->L);
}
static void fsl_deck_clean_M(fsl_deck *m){
  SLIST(M);
}
static void fsl_deck_clean_N(fsl_deck *m){
  fsl_deck_clean_string(m, &m->N);
}
static void fsl_deck_clean_P(fsl_deck *m){
  fsl_list_clear(&m->P, fsl_list_v_card_string_free, m);
}
static void fsl_deck_clean_Q(fsl_deck *m){
  fsl_list_clear(&m->Q, fsl_list_v_card_Q_free, NULL);
}
static void fsl_deck_clean_R(fsl_deck *m){
  fsl_deck_clean_string(m, &m->R);
}
static void fsl_deck_clean_T(fsl_deck *m){
  fsl_list_clear(&m->T, fsl_list_v_card_T_free, NULL);
}
static void fsl_deck_clean_U(fsl_deck *m){
  fsl_deck_clean_string(m, &m->U);
}
static void fsl_deck_clean_W(fsl_deck *m){
  CBUF(W);
}

void fsl_deck_clean2(fsl_deck *m, fsl_buffer *xferBuf){
  if(!m) return;
  fsl_deck_clean_version(m);  
  fsl_deck_clean_A(m);
  fsl_deck_clean_B(m);
  fsl_deck_clean_C(m);
  m->D = 0.0;
  fsl_deck_clean_E(m);







|
<


|




|







|


|



|





|


|


|


|


|


|


|


|


|


|


|


|


|


|



|







359
360
361
362
363
364
365
366

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#define SFREE(X) fsl_deck_clean_string(m, &m->X)
#define SLIST(X) fsl_list_clear(&m->X, fsl_list_v_card_string_free, m)
#define CBUF(X) fsl_buffer_clear(&m->X)
static void fsl_deck_clean_string(fsl_deck *m, char **member){
  fsl_deck_free_string(m, *member);
  *member = 0;
}
static void fsl_deck_clean_version(fsl_deck *const m){

  m->rid = 0;
}
static void fsl_deck_clean_A(fsl_deck *const m){
  SFREE(A.name);
  SFREE(A.tgt);
  SFREE(A.src);
}
static void fsl_deck_clean_B(fsl_deck * const m){
  if(m->B.baseline){
    assert(!m->B.baseline->B.uuid && "Baselines cannot have a B-card. API misuse?");
    fsl_deck_finalize(m->B.baseline);
    m->B.baseline = NULL;
  }
  SFREE(B.uuid);
}
static void fsl_deck_clean_C(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->C);
}
static void fsl_deck_clean_E(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->E.uuid);
  m->E = fsl_deck_empty.E;
}
static void fsl_deck_clean_F(fsl_deck * const m){
  if(m->F.list){
    fsl_card_F_list_finalize(&m->F);
    m->F = fsl_deck_empty.F;
  }
}
static void fsl_deck_clean_G(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->G);
}
static void fsl_deck_clean_H(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->H);
}
static void fsl_deck_clean_I(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->I);
}
static void fsl_deck_clean_J(fsl_deck * const m, bool alsoListMem){
  fsl_card_J_list_free(&m->J, alsoListMem);
}
static void fsl_deck_clean_K(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->K);
}
static void fsl_deck_clean_L(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->L);
}
static void fsl_deck_clean_M(fsl_deck * const m){
  SLIST(M);
}
static void fsl_deck_clean_N(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->N);
}
static void fsl_deck_clean_P(fsl_deck * const m){
  fsl_list_clear(&m->P, fsl_list_v_card_string_free, m);
}
static void fsl_deck_clean_Q(fsl_deck * const m){
  fsl_list_clear(&m->Q, fsl_list_v_card_Q_free, NULL);
}
static void fsl_deck_clean_R(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->R);
}
static void fsl_deck_clean_T(fsl_deck * const m){
  fsl_list_clear(&m->T, fsl_list_v_card_T_free, NULL);
}
static void fsl_deck_clean_U(fsl_deck * const m){
  fsl_deck_clean_string(m, &m->U);
}
static void fsl_deck_clean_W(fsl_deck * const m){
  CBUF(W);
}

void fsl_deck_clean2(fsl_deck * const m, fsl_buffer * const xferBuf){
  if(!m) return;
  fsl_deck_clean_version(m);  
  fsl_deck_clean_A(m);
  fsl_deck_clean_B(m);
  fsl_deck_clean_C(m);
  m->D = 0.0;
  fsl_deck_clean_E(m);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    m->f = f;
  }
}
#undef CBUF
#undef SFREE
#undef SLIST

void fsl_deck_clean(fsl_deck *m){
  fsl_deck_clean2(m, NULL);
}

void fsl_deck_finalize(fsl_deck *m){
  void const * allocStamp;
  if(!m) return;
  allocStamp = m->allocStamp;
  fsl_deck_clean(m);
  if(allocStamp == &fsl_deck_empty){
    fsl_free(m);
  }else{







|



|







474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    m->f = f;
  }
}
#undef CBUF
#undef SFREE
#undef SLIST

void fsl_deck_clean(fsl_deck * const m){
  fsl_deck_clean2(m, NULL);
}

void fsl_deck_finalize(fsl_deck * const m){
  void const * allocStamp;
  if(!m) return;
  allocStamp = m->allocStamp;
  fsl_deck_clean(m);
  if(allocStamp == &fsl_deck_empty){
    fsl_free(m);
  }else{
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

/**
   If the first n bytes of the given string contain any values <=32,
   returns FSL_RC_SYNTAX, else returns 0. mf->f's error state is
   updated no error. n<0 means to use fsl_strlen() to count the
   length.
*/
static int fsl_deck_strcheck_ctrl_chars(fsl_deck * mf, char cardName, char const * v, fsl_int_t n){
  const char * z = v;
  int rc = 0;
  if(v && n<0) n = fsl_strlen(v);
  for( ; v && z < v+n; ++z ){
    if(*z <= 32){
      rc = fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                         "Invalid character in %c-card.", cardName);







|







744
745
746
747
748
749
750
751
752
753
754
755
756
757
758

/**
   If the first n bytes of the given string contain any values <=32,
   returns FSL_RC_SYNTAX, else returns 0. mf->f's error state is
   updated no error. n<0 means to use fsl_strlen() to count the
   length.
*/
static int fsl_deck_strcheck_ctrl_chars(fsl_deck * const mf, char cardName, char const * v, fsl_int_t n){
  const char * z = v;
  int rc = 0;
  if(v && n<0) n = fsl_strlen(v);
  for( ; v && z < v+n; ++z ){
    if(*z <= 32){
      rc = fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                         "Invalid character in %c-card.", cardName);
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
  SHA3, or MD5 hash value and it is validated against
  fsl_validate16(value,valLen), returning FSL_RC_SYNTAX if that
  check fails. In debug builds, the expected ranges are assert()ed.

  If value is NULL then it is removed from the card instead
  (semantically freed), *mfMember is set to NULL, and 0 is returned.
*/
static int fsl_deck_sethex_impl( fsl_deck * mf, fsl_uuid_cstr value,
                                 char letter,
                                 fsl_size_t assertLen,
                                 char ** mfMember ){
  assert(mf);
  assert( value ? (assertLen==FSL_STRLEN_SHA1
                   || assertLen==FSL_STRLEN_K256
                   || assertLen==FSL_STRLEN_MD5)







|







772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
  SHA3, or MD5 hash value and it is validated against
  fsl_validate16(value,valLen), returning FSL_RC_SYNTAX if that
  check fails. In debug builds, the expected ranges are assert()ed.

  If value is NULL then it is removed from the card instead
  (semantically freed), *mfMember is set to NULL, and 0 is returned.
*/
static int fsl_deck_sethex_impl( fsl_deck * const mf, fsl_uuid_cstr value,
                                 char letter,
                                 fsl_size_t assertLen,
                                 char ** mfMember ){
  assert(mf);
  assert( value ? (assertLen==FSL_STRLEN_SHA1
                   || assertLen==FSL_STRLEN_K256
                   || assertLen==FSL_STRLEN_MD5)
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
    return *mfMember ? 0 : FSL_RC_OOM;
  }
}

/**
    Implements fsl_set_set_XXX() where XXX is a fsl_buffer member of fsl_deck.
 */
static int fsl_deck_b_setuffer_impl( fsl_deck * mf, char const * value,
                                     fsl_int_t valLen,
                                     char letter, fsl_buffer * buf){
  assert(mf);  
  if(!fsl_deck_check_type(mf,letter)) return mf->f->error.code;
  else if(valLen<0) valLen = (fsl_int_t)fsl_strlen(value);
  buf->used = 0;
  if(value && (valLen>0)){
    return fsl_buffer_append( buf, value, valLen );
  }else{
    if(buf->mem) buf->mem[0] = 0;
    return 0;
  }
}

int fsl_deck_B_set( fsl_deck * mf, fsl_uuid_cstr uuidBaseline){
  if(!mf) return FSL_RC_MISUSE;
  else{
    int const bLen = uuidBaseline ? fsl_is_uuid(uuidBaseline) : 0;
    if(uuidBaseline && !bLen){
      return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                            "Invalid B-card value: %s", uuidBaseline);
    }







|














|







803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
    return *mfMember ? 0 : FSL_RC_OOM;
  }
}

/**
    Implements fsl_set_set_XXX() where XXX is a fsl_buffer member of fsl_deck.
 */
static int fsl_deck_b_setuffer_impl( fsl_deck * const mf, char const * value,
                                     fsl_int_t valLen,
                                     char letter, fsl_buffer * buf){
  assert(mf);  
  if(!fsl_deck_check_type(mf,letter)) return mf->f->error.code;
  else if(valLen<0) valLen = (fsl_int_t)fsl_strlen(value);
  buf->used = 0;
  if(value && (valLen>0)){
    return fsl_buffer_append( buf, value, valLen );
  }else{
    if(buf->mem) buf->mem[0] = 0;
    return 0;
  }
}

int fsl_deck_B_set( fsl_deck * const mf, fsl_uuid_cstr uuidBaseline){
  if(!mf) return FSL_RC_MISUSE;
  else{
    int const bLen = uuidBaseline ? fsl_is_uuid(uuidBaseline) : 0;
    if(uuidBaseline && !bLen){
      return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                            "Invalid B-card value: %s", uuidBaseline);
    }
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935






936






937
938


939
940
941
942

943
944
945


946
947
948
949
950
951
952
953
954
955
956











957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
}

/**
    Internal impl for card setters which consist of a simple (char *)
    member. Replaces and frees any prior value. Passing NULL for the
    4th argument unsets the given card (assigns NULL to it).
 */
static int fsl_deck_set_string( fsl_deck * mf, char letter, char ** member, char const * v, fsl_int_t n ){
  if(!fsl_deck_check_type(mf, letter)) return mf->f->error.code;
  fsl_deck_free_string(mf, *member);
  *member = v ? fsl_strndup(v, n) : NULL;
  if(v && !*member) return FSL_RC_OOM;
  else return 0;
}

int fsl_deck_C_set( fsl_deck * mf, char const * v, fsl_int_t n){
  return fsl_deck_set_string( mf, 'C', &mf->C, v, n );
}

int fsl_deck_G_set( fsl_deck * mf, fsl_uuid_cstr uuid){
  int const uLen = fsl_is_uuid(uuid);
  return uLen
    ? fsl_deck_sethex_impl(mf, uuid, 'G', uLen, &mf->G)
    : FSL_RC_SYNTAX;
}

int fsl_deck_H_set( fsl_deck * mf, char const * v, fsl_int_t n){
  if(v && mf->I) return FSL_RC_SYNTAX;
  return fsl_deck_set_string( mf, 'H', &mf->H, v, n );
}

int fsl_deck_I_set( fsl_deck * mf, fsl_uuid_cstr uuid){
  if(uuid && mf->H) return FSL_RC_SYNTAX;
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  return fsl_deck_sethex_impl(mf, uuid, 'I', uLen, &mf->I);
}

int fsl_deck_J_add( fsl_deck * mf, char isAppend,
                    char const * field, char const * value){
  if(!field) return FSL_RC_MISUSE;
  else if(!*field) return FSL_RC_SYNTAX;
  else if(!fsl_deck_check_type(mf,'J')) return mf->f->error.code;
  else{
    int rc;
    fsl_card_J * cp = fsl_card_J_malloc(isAppend, field, value);
    if(!cp) rc = FSL_RC_OOM;
    else if( 0 != (rc = fsl_list_append(&mf->J, cp))){
      fsl_card_J_free(cp);
    }
    return rc;
  }
}


int fsl_deck_K_set( fsl_deck * mf, fsl_uuid_cstr uuid){
  int const uLen = fsl_is_uuid(uuid);
  return uLen
    ? fsl_deck_sethex_impl(mf, uuid, 'K', uLen, &mf->K)
    : FSL_RC_SYNTAX;
}

int fsl_deck_L_set( fsl_deck * mf, char const * v, fsl_int_t n){
  return mf
    ? fsl_deck_set_string(mf, 'L', &mf->L, v, n)
    : FSL_RC_SYNTAX;
}

int fsl_deck_M_add( fsl_deck * mf, char const *uuid){
  int rc;
  char * dupe;
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  if(!uuid) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'M')) return mf->f->error.code;
  else if(!uLen) return FSL_RC_SYNTAX;
  dupe = fsl_strndup(uuid, uLen);
  if(!dupe) rc = FSL_RC_OOM;
  else{
    rc = fsl_list_append( &mf->M, dupe );
    if(rc){
      fsl_free(dupe);
    }
  }
  return rc;
}

int fsl_deck_N_set( fsl_deck * mf, char const * v, fsl_int_t n){
  int rc = 0;
  if(v && n!=0){
    if(n<0) n = fsl_strlen(v);
    rc = fsl_deck_strcheck_ctrl_chars(mf, 'N', v, n);
  }
  return rc ? rc : fsl_deck_set_string( mf, 'N', &mf->N, v, n );
}








int fsl_deck_P_add( fsl_deck * mf, char const *parentUuid){






  int rc;
  char * dupe;


  int const uLen = parentUuid ? fsl_is_uuid(parentUuid) : 0;
  if(!mf || !parentUuid || !*parentUuid) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'P')) return mf->f->error.code;
  else if(!uLen){

    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                         "Invalid UUID for P-card.");
  }


  dupe = fsl_strndup(parentUuid, uLen);
  if(!dupe) rc = FSL_RC_OOM;
  else{
    rc = fsl_list_append( &mf->P, dupe );
    if(rc){
      fsl_free(dupe);
    }
  }
  return rc;
}












fsl_id_t fsl_deck_P_get_id(fsl_deck * d, int index){
  if(!d->f) return -1;
  else if(index>(int)d->P.used) return 0;
  else return fsl_uuid_to_rid(d->f, (char const *)d->P.list[index]);
}


int fsl_deck_Q_add( fsl_deck * mf, int type,
                    fsl_uuid_cstr target,
                    fsl_uuid_cstr baseline ){
  if(!target) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf,'Q')) return mf->f->error.code;
  else if(!type || !fsl_is_uuid(target)
          || (baseline && !fsl_is_uuid(baseline))) return FSL_RC_SYNTAX;
  else{







|







|



|






|




|





|
















|






|





|

















|









>
>
>
>
>
>
|
>
>
>
>
>
>


>
>
|
<
<
|
>



>
>
|










>
>
>
>
>
>
>
>
>
>
>
|






|







840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952


953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
}

/**
    Internal impl for card setters which consist of a simple (char *)
    member. Replaces and frees any prior value. Passing NULL for the
    4th argument unsets the given card (assigns NULL to it).
 */
static int fsl_deck_set_string( fsl_deck * const mf, char letter, char ** member, char const * v, fsl_int_t n ){
  if(!fsl_deck_check_type(mf, letter)) return mf->f->error.code;
  fsl_deck_free_string(mf, *member);
  *member = v ? fsl_strndup(v, n) : NULL;
  if(v && !*member) return FSL_RC_OOM;
  else return 0;
}

int fsl_deck_C_set( fsl_deck * const mf, char const * v, fsl_int_t n){
  return fsl_deck_set_string( mf, 'C', &mf->C, v, n );
}

int fsl_deck_G_set( fsl_deck * const mf, fsl_uuid_cstr uuid){
  int const uLen = fsl_is_uuid(uuid);
  return uLen
    ? fsl_deck_sethex_impl(mf, uuid, 'G', uLen, &mf->G)
    : FSL_RC_SYNTAX;
}

int fsl_deck_H_set( fsl_deck * const mf, char const * v, fsl_int_t n){
  if(v && mf->I) return FSL_RC_SYNTAX;
  return fsl_deck_set_string( mf, 'H', &mf->H, v, n );
}

int fsl_deck_I_set( fsl_deck * const mf, fsl_uuid_cstr uuid){
  if(uuid && mf->H) return FSL_RC_SYNTAX;
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  return fsl_deck_sethex_impl(mf, uuid, 'I', uLen, &mf->I);
}

int fsl_deck_J_add( fsl_deck * const mf, char isAppend,
                    char const * field, char const * value){
  if(!field) return FSL_RC_MISUSE;
  else if(!*field) return FSL_RC_SYNTAX;
  else if(!fsl_deck_check_type(mf,'J')) return mf->f->error.code;
  else{
    int rc;
    fsl_card_J * cp = fsl_card_J_malloc(isAppend, field, value);
    if(!cp) rc = FSL_RC_OOM;
    else if( 0 != (rc = fsl_list_append(&mf->J, cp))){
      fsl_card_J_free(cp);
    }
    return rc;
  }
}


int fsl_deck_K_set( fsl_deck * const mf, fsl_uuid_cstr uuid){
  int const uLen = fsl_is_uuid(uuid);
  return uLen
    ? fsl_deck_sethex_impl(mf, uuid, 'K', uLen, &mf->K)
    : FSL_RC_SYNTAX;
}

int fsl_deck_L_set( fsl_deck * const mf, char const * v, fsl_int_t n){
  return mf
    ? fsl_deck_set_string(mf, 'L', &mf->L, v, n)
    : FSL_RC_SYNTAX;
}

int fsl_deck_M_add( fsl_deck * const mf, char const *uuid){
  int rc;
  char * dupe;
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  if(!uuid) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'M')) return mf->f->error.code;
  else if(!uLen) return FSL_RC_SYNTAX;
  dupe = fsl_strndup(uuid, uLen);
  if(!dupe) rc = FSL_RC_OOM;
  else{
    rc = fsl_list_append( &mf->M, dupe );
    if(rc){
      fsl_free(dupe);
    }
  }
  return rc;
}

int fsl_deck_N_set( fsl_deck * const mf, char const * v, fsl_int_t n){
  int rc = 0;
  if(v && n!=0){
    if(n<0) n = fsl_strlen(v);
    rc = fsl_deck_strcheck_ctrl_chars(mf, 'N', v, n);
  }
  return rc ? rc : fsl_deck_set_string( mf, 'N', &mf->N, v, n );
}


/**
   Adds either parentUuid or takeParentUuid to mf->P. ONE
   of those must e non-NULL and the other must be NULL. If
   takeParentUuid is not NULL then ownership of it is transfered
   to this function regardless of success or failure.
*/
static int fsl__deck_P_add_impl( fsl_deck * const mf,
                                 fsl_uuid_cstr parentUuid,
                                 fsl_uuid_str takeParentUuid){
  if(!fsl_deck_check_type(mf, 'P')){
    fsl_free(takeParentUuid);
    return mf->f->error.code;
  }
  int rc;
  char * dupe;
  fsl_uuid_cstr z = parentUuid ? parentUuid : takeParentUuid;
  assert(parentUuid ? !takeParentUuid : !!takeParentUuid);
  int const uLen = fsl_is_uuid(z);


  if(!uLen){
    fsl_free(takeParentUuid);
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                         "Invalid UUID for P-card.");
  }
  dupe = takeParentUuid
    ? takeParentUuid
    : fsl_strndup(parentUuid, uLen);
  if(!dupe) rc = FSL_RC_OOM;
  else{
    rc = fsl_list_append( &mf->P, dupe );
    if(rc){
      fsl_free(dupe);
    }
  }
  return rc;
}


int fsl_deck_P_add(fsl_deck * const mf, char const *parentUuid){
  return fsl__deck_P_add_impl(mf, parentUuid, NULL);
}

int fsl_deck_P_add_rid( fsl_deck * const mf, fsl_id_t rid ){
  fsl_uuid_str pU = fsl_rid_to_uuid(mf->f, rid);
  return pU ? fsl__deck_P_add_impl(mf, NULL, pU) : mf->f->error.code;
}


fsl_id_t fsl_deck_P_get_id(fsl_deck * const d, int index){
  if(!d->f) return -1;
  else if(index>(int)d->P.used) return 0;
  else return fsl_uuid_to_rid(d->f, (char const *)d->P.list[index]);
}


int fsl_deck_Q_add( fsl_deck * const mf, int type,
                    fsl_uuid_cstr target,
                    fsl_uuid_cstr baseline ){
  if(!target) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf,'Q')) return mf->f->error.code;
  else if(!type || !fsl_is_uuid(target)
          || (baseline && !fsl_is_uuid(baseline))) return FSL_RC_SYNTAX;
  else{
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  if(FSL_CARD_F_LIST_NEEDS_SORT & li->flags){
    qsort(li->list, li->used, sizeof(fsl_card_F),
          fsl_card_F_cmp );
    li->flags &= ~FSL_CARD_F_LIST_NEEDS_SORT;
  }
}

static void fsl_deck_F_sort(fsl_deck * mf){
  fsl_card_F_list_sort(&mf->F);
}

int fsl_card_F_compare_name( fsl_card_F const * const lhs,
                             fsl_card_F const * const rhs){
  return (lhs == rhs) ? 0 : fsl_card_F_cmp( lhs, rhs );
}

int fsl_deck_R_set( fsl_deck * mf, fsl_uuid_cstr md5){
  return mf
    ? fsl_deck_sethex_impl(mf, md5, 'R', md5 ? FSL_STRLEN_MD5 : 0, &mf->R)
    : FSL_RC_MISUSE;
}

int fsl_deck_R_calc2(fsl_deck *mf, char ** tgt){
  fsl_cx * const f = mf->f;
  char const * theHash = 0;
  char hex[FSL_STRLEN_MD5+1];
  if(!f) return FSL_RC_MISUSE;
  else if(!fsl_needs_repo(f)){
    return FSL_RC_NOT_A_REPO;
  }else if(!fsl_deck_check_type(mf,'R')) {







|








|





|







1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
  if(FSL_CARD_F_LIST_NEEDS_SORT & li->flags){
    qsort(li->list, li->used, sizeof(fsl_card_F),
          fsl_card_F_cmp );
    li->flags &= ~FSL_CARD_F_LIST_NEEDS_SORT;
  }
}

static void fsl_deck_F_sort(fsl_deck * const mf){
  fsl_card_F_list_sort(&mf->F);
}

int fsl_card_F_compare_name( fsl_card_F const * const lhs,
                             fsl_card_F const * const rhs){
  return (lhs == rhs) ? 0 : fsl_card_F_cmp( lhs, rhs );
}

int fsl_deck_R_set( fsl_deck * const mf, fsl_uuid_cstr md5){
  return mf
    ? fsl_deck_sethex_impl(mf, md5, 'R', md5 ? FSL_STRLEN_MD5 : 0, &mf->R)
    : FSL_RC_MISUSE;
}

int fsl_deck_R_calc2(fsl_deck * const mf, char ** tgt){
  fsl_cx * const f = mf->f;
  char const * theHash = 0;
  char hex[FSL_STRLEN_MD5+1];
  if(!f) return FSL_RC_MISUSE;
  else if(!fsl_needs_repo(f)){
    return FSL_RC_NOT_A_REPO;
  }else if(!fsl_deck_check_type(mf,'R')) {
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
  }else{
    char * x = fsl_strdup(theHash);
    if(x) *tgt = x;
    return x ? 0 : FSL_RC_OOM;
  }
}

int fsl_deck_R_calc(fsl_deck * mf){
  char R[FSL_STRLEN_MD5+1] = {0};
  char * r = R;
  const int rc = fsl_deck_R_calc2(mf, &r);
  return rc ? rc : fsl_deck_R_set(mf, r);
}

int fsl_deck_T_add2( fsl_deck * mf, fsl_card_T * t){
  if(!t) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'T')){
    return mf->f->error.code;
  }else if(FSL_SATYPE_CONTROL==mf->type && NULL==t->uuid){
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                            "CONTROL artifacts may not have "
                            "self-referential tags.");







|






|







1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  }else{
    char * x = fsl_strdup(theHash);
    if(x) *tgt = x;
    return x ? 0 : FSL_RC_OOM;
  }
}

int fsl_deck_R_calc(fsl_deck * const mf){
  char R[FSL_STRLEN_MD5+1] = {0};
  char * r = R;
  const int rc = fsl_deck_R_calc2(mf, &r);
  return rc ? rc : fsl_deck_R_set(mf, r);
}

int fsl_deck_T_add2( fsl_deck * const mf, fsl_card_T * t){
  if(!t) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'T')){
    return mf->f->error.code;
  }else if(FSL_SATYPE_CONTROL==mf->type && NULL==t->uuid){
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                            "CONTROL artifacts may not have "
                            "self-referential tags.");
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
  }else if(t->uuid && !fsl_is_uuid(t->uuid)){
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                         "Invalid UUID in tag.");
  }
  return fsl_list_append(&mf->T, t);
}

int fsl_deck_T_add( fsl_deck * mf, fsl_tagtype_e tagType,
                    char const * uuid, char const * name,
                    char const * value){
  if(!name) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'T')) return mf->f->error.code;
  else if(!*name || (uuid &&!fsl_is_uuid(uuid))) return FSL_RC_SYNTAX;
  else switch(tagType){
    case FSL_TAGTYPE_CANCEL:







|







1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
  }else if(t->uuid && !fsl_is_uuid(t->uuid)){
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
                         "Invalid UUID in tag.");
  }
  return fsl_list_append(&mf->T, t);
}

int fsl_deck_T_add( fsl_deck * const mf, fsl_tagtype_e tagType,
                    char const * uuid, char const * name,
                    char const * value){
  if(!name) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'T')) return mf->f->error.code;
  else if(!*name || (uuid &&!fsl_is_uuid(uuid))) return FSL_RC_SYNTAX;
  else switch(tagType){
    case FSL_TAGTYPE_CANCEL:
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
    }else{
      rc = FSL_RC_OOM;
    }
  }
  return rc;
}

int fsl_deck_U_set( fsl_deck * mf, char const * v){
  return fsl_deck_set_string( mf, 'U', &mf->U, v, -1 );
}

int fsl_deck_W_set( fsl_deck * mf, char const * v, fsl_int_t n){
  return fsl_deck_b_setuffer_impl(mf, v, n, 'W', &mf->W);
}

int fsl_deck_A_set( fsl_deck * mf, char const * name,
                    char const * tgt,
                    char const * uuidSrc ){
  int const uLen = (uuidSrc && *uuidSrc) ? fsl_is_uuid(uuidSrc) : 0;
  if(!name || !tgt) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'A')) return mf->f->error.code;
  else if(!*tgt){
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,







|



|



|







1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
    }else{
      rc = FSL_RC_OOM;
    }
  }
  return rc;
}

int fsl_deck_U_set( fsl_deck * const mf, char const * v){
  return fsl_deck_set_string( mf, 'U', &mf->U, v, -1 );
}

int fsl_deck_W_set( fsl_deck * const mf, char const * v, fsl_int_t n){
  return fsl_deck_b_setuffer_impl(mf, v, n, 'W', &mf->W);
}

int fsl_deck_A_set( fsl_deck * const mf, char const * name,
                    char const * tgt,
                    char const * uuidSrc ){
  int const uLen = (uuidSrc && *uuidSrc) ? fsl_is_uuid(uuidSrc) : 0;
  if(!name || !tgt) return FSL_RC_MISUSE;
  else if(!fsl_deck_check_type(mf, 'A')) return mf->f->error.code;
  else if(!*tgt){
    return fsl_cx_err_set(mf->f, FSL_RC_SYNTAX,
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
        /* Leave mf->A.tgt/name for downstream cleanup. */;
    }
    return rc;
  }
}


int fsl_deck_D_set( fsl_deck * mf, double date){
  if(date<0) return FSL_RC_RANGE;
  else if(date>0 && !fsl_deck_check_type(mf, 'D')){
    return mf->f->error.code;
  }else{
    mf->D = date;
    return 0;
  }
}

int fsl_deck_E_set( fsl_deck * mf, double date, char const * uuid){
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  if(!mf || !uLen) return FSL_RC_MISUSE;
  else if(date<=0){
    return fsl_cx_err_set(mf->f, FSL_RC_RANGE,
                          "Invalid date value for E card.");
  }else if(!uLen){
    return fsl_cx_err_set(mf->f, FSL_RC_RANGE,
                          "Invalid UUID for E card.");
  }
  else{
    mf->E.julian = date;
    fsl_deck_free_string(mf, mf->E.uuid);
    mf->E.uuid = fsl_strndup(uuid, uLen);
    return mf->E.uuid ? 0 : FSL_RC_OOM;
  }
}

int fsl_deck_F_add( fsl_deck * mf, char const * name,
                    char const * uuid,
                    fsl_fileperm_e perms, 
                    char const * oldName){
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  if(!mf || !name) return FSL_RC_MISUSE;
  else if(!uuid && !mf->B.uuid){
    return fsl_cx_err_set(mf->f, FSL_RC_MISUSE,







|









|

















|







1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
        /* Leave mf->A.tgt/name for downstream cleanup. */;
    }
    return rc;
  }
}


int fsl_deck_D_set( fsl_deck * const mf, double date){
  if(date<0) return FSL_RC_RANGE;
  else if(date>0 && !fsl_deck_check_type(mf, 'D')){
    return mf->f->error.code;
  }else{
    mf->D = date;
    return 0;
  }
}

int fsl_deck_E_set( fsl_deck * const mf, double date, char const * uuid){
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  if(!mf || !uLen) return FSL_RC_MISUSE;
  else if(date<=0){
    return fsl_cx_err_set(mf->f, FSL_RC_RANGE,
                          "Invalid date value for E card.");
  }else if(!uLen){
    return fsl_cx_err_set(mf->f, FSL_RC_RANGE,
                          "Invalid UUID for E card.");
  }
  else{
    mf->E.julian = date;
    fsl_deck_free_string(mf, mf->E.uuid);
    mf->E.uuid = fsl_strndup(uuid, uLen);
    return mf->E.uuid ? 0 : FSL_RC_OOM;
  }
}

int fsl_deck_F_add( fsl_deck * const mf, char const * name,
                    char const * uuid,
                    fsl_fileperm_e perms, 
                    char const * oldName){
  int const uLen = uuid ? fsl_is_uuid(uuid) : 0;
  if(!mf || !name) return FSL_RC_MISUSE;
  else if(!uuid && !mf->B.uuid){
    return fsl_cx_err_set(mf->f, FSL_RC_MISUSE,
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744

int fsl_deck_F_set( fsl_deck * d, char const * zName,
                    char const * uuid,
                    fsl_fileperm_e perms, 
                    char const * priorName){
  uint32_t fcNdx = 0;
  fsl_card_F * fc = 0;
  if(d->uuid || d->rid>0){
    return fsl_cx_err_set(d->f, FSL_RC_MISUSE,
                          "%s() cannot be applied to a saved deck.",
                          __func__);
  }else if(!fsl_deck_check_type(d, 'F')){
    return d->f->error.code;
  }
  fc = fsl_deck_F_seek_base(d, zName, &fcNdx);







|







2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769

int fsl_deck_F_set( fsl_deck * d, char const * zName,
                    char const * uuid,
                    fsl_fileperm_e perms, 
                    char const * priorName){
  uint32_t fcNdx = 0;
  fsl_card_F * fc = 0;
  if(d->rid>0){
    return fsl_cx_err_set(d->f, FSL_RC_MISUSE,
                          "%s() cannot be applied to a saved deck.",
                          __func__);
  }else if(!fsl_deck_check_type(d, 'F')){
    return d->f->error.code;
  }
  fc = fsl_deck_F_seek_base(d, zName, &fcNdx);
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
                            fsl_buffer const * src,
                            fsl_fileperm_e perm, 
                            char const * priorName){
  fsl_uuid_str zHash = 0;
  fsl_id_t rid = 0;
  fsl_id_t prevRid = 0;
  int rc = 0;
  if(d->uuid || d->rid>0){
    return fsl_cx_err_set(d->f, FSL_RC_MISUSE,
                          "%s() cannot be applied to a saved deck.",
                          __func__);
  }else if(!fsl_cx_transaction_level(d->f)){
    return fsl_cx_err_set(d->f, FSL_RC_MISUSE,
                          "%s() requires that a transaction is active.",
                          __func__);







|







2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
                            fsl_buffer const * src,
                            fsl_fileperm_e perm, 
                            char const * priorName){
  fsl_uuid_str zHash = 0;
  fsl_id_t rid = 0;
  fsl_id_t prevRid = 0;
  int rc = 0;
  if(d->rid>0){
    return fsl_cx_err_set(d->f, FSL_RC_MISUSE,
                          "%s() cannot be applied to a saved deck.",
                          __func__);
  }else if(!fsl_cx_transaction_level(d->f)){
    return fsl_cx_err_set(d->f, FSL_RC_MISUSE,
                          "%s() requires that a transaction is active.",
                          __func__);
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865

2866
2867



2868








2869
2870

2871
2872
2873
2874
2875
2876
2877
      case 'U': fsl_deck_clean_U(d); break;
      case 'W': fsl_deck_clean_W(d); break;
      default: break;
    }
  }
}

int fsl_deck_derive(fsl_deck * d){
  int rc = 0;
  if(!d->uuid || d->rid<=0) return FSL_RC_MISUSE;

  else if(FSL_SATYPE_CHECKIN!=d->type) return FSL_RC_TYPE;
  fsl_deck_clean_P(d);



  rc = fsl_list_append(&d->P, d->uuid);








  if(rc) return rc;
  d->uuid = NULL;

  d->rid = 0;
  fsl_deck_clean_cards(d, "ACDEGHIJKLMNQRTUW");
  while(d->B.uuid){
    /* This is a delta manifest. Convert this deck into a baseline by
       build a new, complete F-card list. */
    fsl_card_F const * fc;
    fsl_card_F_list flist = fsl_card_F_list_empty;







|

|
>
|

>
>
>
|
>
>
>
>
>
>
>
>
|
<
>







2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906

2907
2908
2909
2910
2911
2912
2913
2914
      case 'U': fsl_deck_clean_U(d); break;
      case 'W': fsl_deck_clean_W(d); break;
      default: break;
    }
  }
}

int fsl_deck_derive(fsl_deck * const d){
  int rc = 0;
  if(d->rid<=0) return FSL_RC_MISUSE;
  assert(d->f);
  if(FSL_SATYPE_CHECKIN!=d->type) return FSL_RC_TYPE;
  fsl_deck_clean_P(d);
  {
    fsl_uuid_str pUuid = fsl_rid_to_uuid(d->f, d->rid);
    if(pUuid){
      rc = fsl_list_append(&d->P, pUuid);
      if(rc){
        assert(NULL==d->P.list);
        fsl_free(pUuid);
      }
    }else{
      assert(d->f->error.code);
      rc = d->f->error.code;
    }
    if(rc) return rc;

  }
  d->rid = 0;
  fsl_deck_clean_cards(d, "ACDEGHIJKLMNQRTUW");
  while(d->B.uuid){
    /* This is a delta manifest. Convert this deck into a baseline by
       build a new, complete F-card list. */
    fsl_card_F const * fc;
    fsl_card_F_list flist = fsl_card_F_list_empty;
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
      rc = xl->f( d, xl->state );
    }
    if(rc){
      assert(xl);
      if(!f->error.code){
        fsl_cx_err_set(f, rc, "Crosslink callback handler "
                       "'%s' failed with code %d (%s) for "
                       "artifact [%.12s].",
                       xl->name, rc, fsl_rc_cstr(rc),
                       d->uuid);
      }
    }
  }/*end crosslink callbacks*/
  end:
  if(!rc){
    rc = fsl_db_transaction_end(db, false);
  }else{







|

|







4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
      rc = xl->f( d, xl->state );
    }
    if(rc){
      assert(xl);
      if(!f->error.code){
        fsl_cx_err_set(f, rc, "Crosslink callback handler "
                       "'%s' failed with code %d (%s) for "
                       "artifact RID #%" FSL_ID_T_PFMT ".",
                       xl->name, rc, fsl_rc_cstr(rc),
                       d->rid);
      }
    }
  }/*end crosslink callbacks*/
  end:
  if(!rc){
    rc = fsl_db_transaction_end(db, false);
  }else{
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
          || z[n-34]!=' '
          || !fsl_validate16((const char *)z+n-33, FSL_STRLEN_MD5)){
    return 0;
  }
  return 1;
}

int fsl_deck_parse2(fsl_deck * d, fsl_buffer * src, fsl_id_t rid){
#ifdef ERROR
#  undef ERROR
#endif
#define ERROR(RC,MSG) do{ rc = (RC); zMsg = (MSG); goto bailout; } while(0)
#define SYNTAX(MSG) ERROR(rc ? rc : FSL_RC_SYNTAX,MSG)
  bool isRepeat = 0/* , hasSelfRefTag = 0 */;
  int rc = 0;
  fsl_src x = fsl_src_empty;
  char const * zMsg = NULL;
  fsl_id_bag * seen;
  char cType = 0, cPrevType = 0;
  unsigned char * z = src ? src->mem : NULL;
  fsl_size_t tokLen = 0;
  unsigned char * token;
  fsl_size_t n = z ? src->used : 0;
  unsigned char * uuid;
  double ts;
  int cardCount = 0;
  fsl_db * db;
  fsl_cx * f;
  fsl_error * err;
  int stealBuf = 0 /* gets incremented if we need to steal src->mem. */;
  unsigned nSelfTag = 0 /* number of T cards which refer to '*' (this artifact). */;
  unsigned nSimpleTag = 0 /* number of T cards with "+" prefix */;
  /*
    lettersSeen keeps track of the card letters we have seen so that







|


















<







4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689

4690
4691
4692
4693
4694
4695
4696
          || z[n-34]!=' '
          || !fsl_validate16((const char *)z+n-33, FSL_STRLEN_MD5)){
    return 0;
  }
  return 1;
}

int fsl_deck_parse2(fsl_deck * const d, fsl_buffer * const src, fsl_id_t rid){
#ifdef ERROR
#  undef ERROR
#endif
#define ERROR(RC,MSG) do{ rc = (RC); zMsg = (MSG); goto bailout; } while(0)
#define SYNTAX(MSG) ERROR(rc ? rc : FSL_RC_SYNTAX,MSG)
  bool isRepeat = 0/* , hasSelfRefTag = 0 */;
  int rc = 0;
  fsl_src x = fsl_src_empty;
  char const * zMsg = NULL;
  fsl_id_bag * seen;
  char cType = 0, cPrevType = 0;
  unsigned char * z = src ? src->mem : NULL;
  fsl_size_t tokLen = 0;
  unsigned char * token;
  fsl_size_t n = z ? src->used : 0;
  unsigned char * uuid;
  double ts;
  int cardCount = 0;

  fsl_cx * f;
  fsl_error * err;
  int stealBuf = 0 /* gets incremented if we need to steal src->mem. */;
  unsigned nSelfTag = 0 /* number of T cards which refer to '*' (this artifact). */;
  unsigned nSimpleTag = 0 /* number of T cards with "+" prefix */;
  /*
    lettersSeen keeps track of the card letters we have seen so that
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
                         : "Zero-length input");
  }
  else if(rid<0){
    return fsl_error_set(err, FSL_RC_RANGE,
                         "Invalid (negative) RID %"FSL_ID_T_PFMT
                         " for fsl_deck_parse()", rid);
  }
  db = fsl_cx_db_repo(f);
  seen = f ? &f->cache.mfSeen : NULL;
  if(seen){
    if((0==rid) || fsl_id_bag_contains(seen,rid)){
      isRepeat = 1;
    }else{
      isRepeat = 0;
      rc = fsl_id_bag_insert(seen, rid);
      if(rc){
        assert(FSL_RC_OOM==rc);
        return rc;
      }
    }
  }
  fsl_deck_clean(d);
  fsl_deck_init(f, d, FSL_SATYPE_ANY);
  /* We have to hash BEFORE parsing, as parsing modifies
     the input */
  if(rid){
    assert(rid>0);
    d->rid = rid;
    d->uuid = fsl_rid_to_uuid(f, rid);
    if(!d->uuid){
      rc = f->error.code ? f->error.code : FSL_RC_OOM;
      goto end;
    }
  }else if(db){
    rc = fsl_repo_blob_lookup(f, src, &d->rid, &d->uuid);
    if(FSL_RC_NOT_FOUND==rc){
      /* Non-fatal */
      rc = 0;
    }
  }else{
    fsl_buffer hash = fsl_buffer_empty;
    rc = fsl_cx_hash_buffer(f, false, src, &hash);
    if(rc){
      fsl_buffer_clear(&hash);
    }else{
      d->uuid = fsl_buffer_str(&hash);
    }
  }
  if(rc) return rc;

  /* legacy: not yet clear if we need this:
     if( !isRepeat ) g.parseCnt[0]++; */

  /*
    Verify that the first few characters of the artifact look like a
    control artifact.
  */
  if( !fsl_might_be_artifact(src) ){
    ERROR(FSL_RC_SYNTAX, "Content does not look like "







<
|
<
|
|
|
|
|
|
|
|
|
|
<


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







4712
4713
4714
4715
4716
4717
4718

4719

4720
4721
4722
4723
4724
4725
4726
4727
4728
4729

4730
4731





























4732
4733
4734
4735
4736
4737
4738
                         : "Zero-length input");
  }
  else if(rid<0){
    return fsl_error_set(err, FSL_RC_RANGE,
                         "Invalid (negative) RID %"FSL_ID_T_PFMT
                         " for fsl_deck_parse()", rid);
  }

  seen = &f->cache.mfSeen;

  if((0==rid) || fsl_id_bag_contains(seen,rid)){
    isRepeat = 1;
  }else{
    isRepeat = 0;
    rc = fsl_id_bag_insert(seen, rid);
    if(rc){
      assert(FSL_RC_OOM==rc);
      return rc;
    }
  }

  fsl_deck_clean(d);
  fsl_deck_init(f, d, FSL_SATYPE_ANY);






























  /*
    Verify that the first few characters of the artifact look like a
    control artifact.
  */
  if( !fsl_might_be_artifact(src) ){
    ERROR(FSL_RC_SYNTAX, "Content does not look like "
4747
4748
4749
4750
4751
4752
4753



4754
4755
4756
4757
4758
4759
4760
    z = (unsigned char *)zz;
  }

  /* Verify the Z card */
  if( fsl_deck_verify_Z_card(z, n) < 0 ){
    ERROR(FSL_RC_CONSISTENCY, "Z-card checksum mismatch");
  }




  /*
    Reminder: parsing modifies the input (to simplify the
    tokenization/parsing).

    As of mid-201403, we recycle as much as possible from the source
    buffer and take over ownership _if_ we do so.







>
>
>







4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
    z = (unsigned char *)zz;
  }

  /* Verify the Z card */
  if( fsl_deck_verify_Z_card(z, n) < 0 ){
    ERROR(FSL_RC_CONSISTENCY, "Z-card checksum mismatch");
  }

  /* legacy: not yet clear if we need this:
     if( !isRepeat ) g.parseCnt[0]++; */

  /*
    Reminder: parsing modifies the input (to simplify the
    tokenization/parsing).

    As of mid-201403, we recycle as much as possible from the source
    buffer and take over ownership _if_ we do so.
5450
5451
5452
5453
5454
5455
5456

5457
5458
5459
5460
5461
5462
5463
  if(stealBuf>0){
    /* We stashed something which points to src->mem, so we need to
       steal that memory.
    */
    d->content = *src;
    *src = fsl_buffer_empty;
  }

  d->F.flags &= ~FSL_CARD_F_LIST_NEEDS_SORT/*we know all cards were read in order*/;
  return 0;

  bailout:
  if(stealBuf>0){
    d->content = *src;
    *src = fsl_buffer_empty;







>







5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
  if(stealBuf>0){
    /* We stashed something which points to src->mem, so we need to
       steal that memory.
    */
    d->content = *src;
    *src = fsl_buffer_empty;
  }
  d->rid = rid;
  d->F.flags &= ~FSL_CARD_F_LIST_NEEDS_SORT/*we know all cards were read in order*/;
  return 0;

  bailout:
  if(stealBuf>0){
    d->content = *src;
    *src = fsl_buffer_empty;
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496

5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
#undef TOKEN_EXISTS
#undef TOKEN_UUID
#undef TOKEN_MD5
#undef TOKEN
#undef ERROR
}

int fsl_deck_parse(fsl_deck * d, fsl_buffer * src){
  return fsl_deck_parse2(d, src, 0);
}

int fsl_deck_load_rid( fsl_cx * f, fsl_deck * d,
                       fsl_id_t rid, fsl_satype_e type ){
  fsl_buffer buf = fsl_buffer_empty;
  int rc = 0;
  if(!f || !d) return FSL_RC_SYNTAX;
  if(0==rid) rid = f->ckout.rid;
  if(rid<0){
    return fsl_cx_err_set(f, FSL_RC_RANGE,
                          "Invalid RID for fsl_deck_load_rid(): "
                          "%"FSL_ID_T_PFMT, rid);
  }
  fsl_deck_clean(d);

  if(fsl_cx_mcache_search(f, rid, d)){
    assert(d->f);
    if(type!=FSL_SATYPE_ANY && type!=d->type){
      rc = fsl_cx_err_set(f, FSL_RC_ERROR,
                          "Unexpected match of RID #%" FSL_ID_T_PFMT " "
                          "to a different artifact type (%d) "
                          "than requested (%d).",
                          d->type, type);
      fsl_cx_mcache_insert(f, d);
      assert(!d->f);
    }else{







|



|



<







>



|







5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496

5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
#undef TOKEN_EXISTS
#undef TOKEN_UUID
#undef TOKEN_MD5
#undef TOKEN
#undef ERROR
}

int fsl_deck_parse(fsl_deck * const d, fsl_buffer * const src){
  return fsl_deck_parse2(d, src, 0);
}

int fsl_deck_load_rid( fsl_cx * const f, fsl_deck * const d,
                       fsl_id_t rid, fsl_satype_e type ){
  fsl_buffer buf = fsl_buffer_empty;
  int rc = 0;

  if(0==rid) rid = f->ckout.rid;
  if(rid<0){
    return fsl_cx_err_set(f, FSL_RC_RANGE,
                          "Invalid RID for fsl_deck_load_rid(): "
                          "%"FSL_ID_T_PFMT, rid);
  }
  fsl_deck_clean(d);
  d->f = f;
  if(fsl_cx_mcache_search(f, rid, d)){
    assert(d->f);
    if(type!=FSL_SATYPE_ANY && type!=d->type){
      rc = fsl_cx_err_set(f, FSL_RC_TYPE,
                          "Unexpected match of RID #%" FSL_ID_T_PFMT " "
                          "to a different artifact type (%d) "
                          "than requested (%d).",
                          d->type, type);
      fsl_cx_mcache_insert(f, d);
      assert(!d->f);
    }else{
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544

5545

5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
    it might be worth considering as a small optimization later on.
  */
  d->type = type /* may help parsing fail more quickly if
                    it's not the type we want.*/;
#endif
  rc = fsl_deck_parse(d, &buf);
  if(!rc){
    assert(rid == d->rid);
    if( type!=FSL_SATYPE_ANY && d->type!=type ){
      rc = fsl_cx_err_set(f, FSL_RC_TYPE,
                          "RID %"FSL_ID_T_PFMT" is of type %s, "
                          "but the caller requested type %s.",
                          rid,
                          fsl_satype_cstr(d->type),
                          fsl_satype_cstr(type));
    }
  }
  if( !rc && d->B.uuid ){
    rc = fsl_cx_update_seen_delta_mf(f);
  }

  end:

  fsl_buffer_clear(&buf);
  return rc;
}

int fsl_deck_load_sym( fsl_cx * f, fsl_deck * d,
                       char const * symbolicName, fsl_satype_e type ){
  if(!symbolicName || !d) return FSL_RC_MISUSE;
  else{
    fsl_id_t vid = 0;
    int rc = fsl_sym_to_rid(f, symbolicName, type, &vid);
    if(!rc){
      assert(vid>0);







<







<
<
|
|
|
>

>




|







5533
5534
5535
5536
5537
5538
5539

5540
5541
5542
5543
5544
5545
5546


5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
    it might be worth considering as a small optimization later on.
  */
  d->type = type /* may help parsing fail more quickly if
                    it's not the type we want.*/;
#endif
  rc = fsl_deck_parse(d, &buf);
  if(!rc){

    if( type!=FSL_SATYPE_ANY && d->type!=type ){
      rc = fsl_cx_err_set(f, FSL_RC_TYPE,
                          "RID %"FSL_ID_T_PFMT" is of type %s, "
                          "but the caller requested type %s.",
                          rid,
                          fsl_satype_cstr(d->type),
                          fsl_satype_cstr(type));


    }else if(d->B.uuid ){
      rc = fsl_cx_update_seen_delta_mf(f);
    }
  }
  end:
  if(0==rc) d->rid = rid;
  fsl_buffer_clear(&buf);
  return rc;
}

int fsl_deck_load_sym( fsl_cx * const f, fsl_deck * const d,
                       char const * symbolicName, fsl_satype_e type ){
  if(!symbolicName || !d) return FSL_RC_MISUSE;
  else{
    fsl_id_t vid = 0;
    int rc = fsl_sym_to_rid(f, symbolicName, type, &vid);
    if(!rc){
      assert(vid>0);
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725

5726

5727
5728

5729


5730
5731
5732
5733
5734
5735
5736
5737
      }      
    }
    return 0;
  }
#undef FCARD
}

int fsl_deck_save( fsl_deck * d, bool isPrivate ){
  int rc;
  fsl_cx * f = d ? d->f : NULL;
  fsl_db * db = f ? fsl_needs_repo(f) : NULL;
  fsl_buffer buf = fsl_buffer_empty;
  fsl_id_t newRid = 0;
  bool const oldPrivate = f ? f->cache.markPrivate : 0;
  bool const isNew = d && !d->uuid;
  if(!f || !d ) return FSL_RC_MISUSE;
  else if(!db) return FSL_RC_NOT_A_REPO;
  else if( (d->rid>0 && !d->uuid) ||

           (d->rid<=0 && d->uuid)){

    return fsl_cx_err_set(f, FSL_RC_MISUSE,
                          "The input deck must have either rid _and_ UUID "

                          "or neither of rid/UUID. A mixture is ambiguous.");


  }else if(d->B.uuid && fsl_repo_forbids_delta_manifests(f)){
    return fsl_cx_err_set(f, FSL_RC_ACCESS,
                          "This deck is a delta manifest, but this "
                          "repository has disallowed those via the "
                          "forbid-delta-manifests config option.");
  }

  fsl_cx_err_reset(f);







|

|
|


|
<


|
>
|
>
|
<
>
|
>
>
|







5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728

5729
5730
5731
5732
5733
5734
5735

5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
      }      
    }
    return 0;
  }
#undef FCARD
}

int fsl_deck_save( fsl_deck * const d, bool isPrivate ){
  int rc;
  fsl_cx * const f = d->f;
  fsl_db * const db = fsl_needs_repo(f);
  fsl_buffer buf = fsl_buffer_empty;
  fsl_id_t newRid = 0;
  bool const oldPrivate = f->cache.markPrivate;

  if(!f || !d ) return FSL_RC_MISUSE;
  else if(!db) return FSL_RC_NOT_A_REPO;
  if(d->rid>0){
#if 1
    return 0;
#else
    return fsl_cx_err_set(f, FSL_RC_ALREADY_EXISTS,

                          "Cannot re-save an existing deck, as that could "
                          "lead to inconsistent data.");
#endif
  }
  if(d->B.uuid && fsl_repo_forbids_delta_manifests(f)){
    return fsl_cx_err_set(f, FSL_RC_ACCESS,
                          "This deck is a delta manifest, but this "
                          "repository has disallowed those via the "
                          "forbid-delta-manifests config option.");
  }

  fsl_cx_err_reset(f);
5749
5750
5751
5752
5753
5754
5755

5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
  if(0){
    MARKER(("Saving deck:\n%s\n", fsl_buffer_cstr(&buf)));
  }

  /* Starting here, don't return, use (goto end) instead. */

  f->cache.markPrivate = isPrivate;

  rc = fsl_content_put_ex(f, &buf, d->uuid, 0,
                          0U, isPrivate, &newRid);
  if(rc) goto end;
  assert(newRid>0);

  rc = fsl_cx_hash_buffer( f, false, &buf, &buf );
  if(rc) goto end;

  /* We need d->uuid and d->rid for crosslinking purposes, but will
     unset them on error (if we set them) because their values will no
     longer be in the db after rollback...
  */
#if 0
  /* practice shows that this is not needed/desired. There are cases
     where round-tripping a manifest results in a 1-millisecond
     difference in the D-card (Julian Day/double), which changes the
     has but otherwise has no difference on the meaning. That said,
     the way we use decks means that we really don't care (for
     purposes of this function) what their initial UUID is except for
     the purpose of fsl_content_put_ex(), above.
  */
  if(d->uuid){
    /* Compare original and resulting hashes. */
    if(0 != fsl_uuidcmp(fsl_buffer_cstr(&buf), d->uuid)){
      rc = fsl_cx_err_set(f, FSL_RC_CONSISTENCY,
                          "Input deck's original UUID and resulting UUID "
                          "do not match: [%s] vs [%b]",
                          d->uuid, &buf);
      goto end;
    }
    /* ??? assert(d->rid==newRid); */
    d->rid = newRid;
  }else
#endif  
  {
    fsl_free(d->uuid);
    d->uuid = fsl_buffer_str(&buf) /* transfer ownership */;
    buf = fsl_buffer_empty;
    d->rid = newRid;
  }

#if 0
  /* Something to consider: if d is new and has a parent, deltify the
     parent. The branch operation does this, but it is not yet clear
     whether that is a general pattern for manifests.
  */
  if(isNew && d->P.used){
    fsl_id_t pid;
    assert(FSL_SATYPE_CHECKIN == d->type);
    pid = fsl_uuid_to_rid(f, (char const *)d->P.list[0]);
    if(pid>0){
      rc = fsl_content_deltify(f, pid, d->rid, 0);
      if(rc) goto end;
    }
  }
#endif

  if(isNew && (FSL_SATYPE_WIKI==d->type)){
    /* Analog to fossil's wiki.c:wiki_put(): */
    /*
      MISSING:
      fossil's wiki.c:wiki_put() handles the moderation bits.
    */
    if(d->P.used){
      fsl_id_t const pid = fsl_deck_P_get_id(d, 0);







>
|
|
|
|
|
<
<

|
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<






|










|







5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771


5772
5773
5774
5775
5776



















5777








5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
  if(0){
    MARKER(("Saving deck:\n%s\n", fsl_buffer_cstr(&buf)));
  }

  /* Starting here, don't return, use (goto end) instead. */

  f->cache.markPrivate = isPrivate;
  {
    rc = fsl_content_put_ex(f, &buf, NULL, 0,
                            0U, isPrivate, &newRid);
    if(rc) goto end;
    assert(newRid>0);
  }



  /* We need d->rid for crosslinking purposes, but will
     unset it on error (if we set it) because its value will no
     longer be in the db after rollback...
  */



















  d->rid = newRid;









#if 0
  /* Something to consider: if d is new and has a parent, deltify the
     parent. The branch operation does this, but it is not yet clear
     whether that is a general pattern for manifests.
  */
  if(d->P.used){
    fsl_id_t pid;
    assert(FSL_SATYPE_CHECKIN == d->type);
    pid = fsl_uuid_to_rid(f, (char const *)d->P.list[0]);
    if(pid>0){
      rc = fsl_content_deltify(f, pid, d->rid, 0);
      if(rc) goto end;
    }
  }
#endif

  if(FSL_SATYPE_WIKI==d->type){
    /* Analog to fossil's wiki.c:wiki_put(): */
    /*
      MISSING:
      fossil's wiki.c:wiki_put() handles the moderation bits.
    */
    if(d->P.used){
      fsl_id_t const pid = fsl_deck_P_get_id(d, 0);
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871

  rc = f->cache.isCrosslinking
    ? fsl_deck_crosslink(d)
    : fsl_deck_crosslink_one(d);

  end:
  f->cache.markPrivate = oldPrivate;
  if(!rc) rc = fsl_db_transaction_end( db, 0);
  else fsl_db_transaction_end(db, 1);
  if(rc){
    if(isNew){
      fsl_free(d->uuid);
      d->uuid = NULL;
      d->rid = 0;
    }
    if(!f->error.code && db->error.code){
      rc = fsl_cx_uplift_db_error(f, db);
    }
  }
  fsl_buffer_clear(&buf);
  return rc;
}







|


<
<
<
|
<







5832
5833
5834
5835
5836
5837
5838
5839
5840
5841



5842

5843
5844
5845
5846
5847
5848
5849

  rc = f->cache.isCrosslinking
    ? fsl_deck_crosslink(d)
    : fsl_deck_crosslink_one(d);

  end:
  f->cache.markPrivate = oldPrivate;
  if(!rc) rc = fsl_db_transaction_end(db, 0);
  else fsl_db_transaction_end(db, 1);
  if(rc){



    d->rid = 0 /* this blob.rid will be lost after rollback */;

    if(!f->error.code && db->error.code){
      rc = fsl_cx_uplift_db_error(f, db);
    }
  }
  fsl_buffer_clear(&buf);
  return rc;
}
Changes to src/repo.c.
492
493
494
495
496
497
498
499
500
501
502
503
504

505
506
507
508
509
510
511
    }else{
      fsl_free( rvv );
    }
  }
  return rc;
}

fsl_id_t fsl_uuid_to_rid( fsl_cx * f, char const * uuid ){
  fsl_db * const db = fsl_needs_repo(f);
  fsl_size_t const uuidLen = (uuid && db) ? fsl_strlen(uuid) : 0;
  if(!f || !uuid || !uuidLen) return -1;
  else if(!db){
    /* f's error state has already been set */

    return -2;
  }
  else if(!fsl_validate16(uuid, uuidLen)){
    fsl_cx_err_set(f, FSL_RC_RANGE, "Invalid UUID (prefix): %s", uuid);
    return -3;
  }
  else if(uuidLen>FSL_STRLEN_K256){







|


|


>







492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
    }else{
      fsl_free( rvv );
    }
  }
  return rc;
}

fsl_id_t fsl_uuid_to_rid( fsl_cx * const f, char const * uuid ){
  fsl_db * const db = fsl_needs_repo(f);
  fsl_size_t const uuidLen = (uuid && db) ? fsl_strlen(uuid) : 0;
  if(!uuid || !uuidLen) return -1;
  else if(!db){
    /* f's error state has already been set */
    assert(FSL_RC_NOT_A_REPO == f->error.code);
    return -2;
  }
  else if(!fsl_validate16(uuid, uuidLen)){
    fsl_cx_err_set(f, FSL_RC_RANGE, "Invalid UUID (prefix): %s", uuid);
    return -3;
  }
  else if(uuidLen>FSL_STRLEN_K256){
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
    : FSL_RC_RANGE;    
}

void fsl_repo_verify_cancel( fsl_cx * f ){
  fsl_id_bag_clear(&f->cache.toVerify);
}

int fsl_rid_to_uuid2(fsl_cx * f, fsl_id_t rid, fsl_buffer *uuid){
  fsl_db * db = f ? fsl_cx_db_repo(f) : NULL;
  if(!f || !db || (rid<=0)){
    return fsl_cx_err_set(f, FSL_RC_MISUSE,
                          "fsl_rid_to_uuid2() requires "
                          "an opened repository and a "
                          "positive RID value. rid=%" FSL_ID_T_PFMT,
                          rid);







|







706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
    : FSL_RC_RANGE;    
}

void fsl_repo_verify_cancel( fsl_cx * f ){
  fsl_id_bag_clear(&f->cache.toVerify);
}

int fsl_rid_to_uuid2(fsl_cx * const f, fsl_id_t rid, fsl_buffer *uuid){
  fsl_db * db = f ? fsl_cx_db_repo(f) : NULL;
  if(!f || !db || (rid<=0)){
    return fsl_cx_err_set(f, FSL_RC_MISUSE,
                          "fsl_rid_to_uuid2() requires "
                          "an opened repository and a "
                          "positive RID value. rid=%" FSL_ID_T_PFMT,
                          rid);
737
738
739
740
741
742
743
744
745



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
          rc = fsl_cx_err_set(f, FSL_RC_NOT_FOUND,
                              "No blob found for rid %" FSL_ID_T_PFMT ".",
                              rid);
        }
      }
      fsl_stmt_cached_yield(st);
      if(rc && !f->error.code){
        assert(db->error.code);
        fsl_cx_uplift_db_error(f, db);



      }
    }
    return rc;
  }
}

fsl_uuid_str fsl_rid_to_uuid(fsl_cx * f, fsl_id_t rid){
  fsl_buffer uuid = fsl_buffer_empty;
  fsl_rid_to_uuid2(f, rid, &uuid);
  return fsl_buffer_take(&uuid);
}

fsl_uuid_str fsl_rid_to_artifact_uuid(fsl_cx * f, fsl_id_t rid, fsl_satype_e type){
  fsl_db * db = f ? fsl_cx_db_repo(f) : NULL;
  if(!f || !db || (rid<=0)) return NULL;
  else{
    char * rv = NULL;
    fsl_stmt * st = NULL;
    int rc;
    rc = fsl_db_prepare_cached(db, &st,







|
|
>
>
>






|





|







738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
          rc = fsl_cx_err_set(f, FSL_RC_NOT_FOUND,
                              "No blob found for rid %" FSL_ID_T_PFMT ".",
                              rid);
        }
      }
      fsl_stmt_cached_yield(st);
      if(rc && !f->error.code){
        if(db->error.code){
          fsl_cx_uplift_db_error(f, db);
        }else{
          fsl_cx_err_set(f, rc, NULL);
        }
      }
    }
    return rc;
  }
}

fsl_uuid_str fsl_rid_to_uuid(fsl_cx * const f, fsl_id_t rid){
  fsl_buffer uuid = fsl_buffer_empty;
  fsl_rid_to_uuid2(f, rid, &uuid);
  return fsl_buffer_take(&uuid);
}

fsl_uuid_str fsl_rid_to_artifact_uuid(fsl_cx * const f, fsl_id_t rid, fsl_satype_e type){
  fsl_db * db = f ? fsl_cx_db_repo(f) : NULL;
  if(!f || !db || (rid<=0)) return NULL;
  else{
    char * rv = NULL;
    fsl_stmt * st = NULL;
    int rc;
    rc = fsl_db_prepare_cached(db, &st,
Changes to src/tag.c.
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    rc = fsl_deck_F_add(&deck, fc->name, fc->uuid, fc->perm, fc->priorName);
      if(rc) goto end;
  }

  rc = fsl_deck_U_set(&deck, user);
  if(rc) goto end;

  rc = fsl_deck_P_add(&deck, parent.uuid);
  if(rc) goto end;

  if(opt->comment && *opt->comment){
    rc = fsl_deck_C_set(&deck, opt->comment, -1);
  }else{
    fsl_buffer c = fsl_buffer_empty;
    rc = fsl_buffer_appendf(&c, "Created branch [%s].", opt->name);







|







524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    rc = fsl_deck_F_add(&deck, fc->name, fc->uuid, fc->perm, fc->priorName);
      if(rc) goto end;
  }

  rc = fsl_deck_U_set(&deck, user);
  if(rc) goto end;

  rc = fsl_deck_P_add_rid(&deck, parent.rid);
  if(rc) goto end;

  if(opt->comment && *opt->comment){
    rc = fsl_deck_C_set(&deck, opt->comment, -1);
  }else{
    fsl_buffer c = fsl_buffer_empty;
    rc = fsl_buffer_appendf(&c, "Created branch [%s].", opt->name);
Changes to src/vfile.c.
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  fsl_stmt_finalize(&qIns);
  fsl_stmt_finalize(&qRid);
  /* Update f->ckout state and some db bits we need
     when changing the checkout. */
  if(!rc && vid>0){
    if(!alreadyHad){
      assert(d.rid>0);
      assert(d.uuid);
    }
  }
  fsl_deck_finalize(&d);
  if(rc) fsl_db_transaction_rollback(dbC);
  else rc = fsl_db_transaction_commit(dbC);
  if(rc && !f->error.code){
    if(dbC->error.code) fsl_cx_uplift_db_error(f, dbC);







<







127
128
129
130
131
132
133

134
135
136
137
138
139
140
  fsl_stmt_finalize(&qIns);
  fsl_stmt_finalize(&qRid);
  /* Update f->ckout state and some db bits we need
     when changing the checkout. */
  if(!rc && vid>0){
    if(!alreadyHad){
      assert(d.rid>0);

    }
  }
  fsl_deck_finalize(&d);
  if(rc) fsl_db_transaction_rollback(dbC);
  else rc = fsl_db_transaction_commit(dbC);
  if(rc && !f->error.code){
    if(dbC->error.code) fsl_cx_uplift_db_error(f, dbC);
Changes to src/zip.c.
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  if(!rc){
    if(!zs.z.entryCount){
      if(rootDir && *rootDir){
        rc = fsl_zip_file_add( &zs.z, rootDir, NULL, FSL_FILE_PERM_REGULAR );
      }else{
        rc = fsl_cx_err_set(f, FSL_RC_RANGE, "Cowardly refusing to create "
                            "empty ZIP file for repo version [%.*s].",
                            12, mf.uuid);
      }
      if(rc) goto end;
    }
  }

  /**
     Always write he manifest files to the zip, regardless of







|







505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  if(!rc){
    if(!zs.z.entryCount){
      if(rootDir && *rootDir){
        rc = fsl_zip_file_add( &zs.z, rootDir, NULL, FSL_FILE_PERM_REGULAR );
      }else{
        rc = fsl_cx_err_set(f, FSL_RC_RANGE, "Cowardly refusing to create "
                            "empty ZIP file for repo version [%.*s].",
                            12, sym);
      }
      if(rc) goto end;
    }
  }

  /**
     Always write he manifest files to the zip, regardless of