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
|
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
|
-
-
+
+
-
+
-
+
-
+
|
/*
** Add a single entry to the mlink table. Also add the filename to
** the filename table if it is not there already.
*/
static void add_one_mlink(
int mid, /* The record ID of the manifest */
const char *zFromUuid, /* UUID for the mlink.pid field */
const char *zToUuid, /* UUID for the mlink.fid field */
const char *zFromUuid, /* UUID for the mlink.pid. "" to add file */
const char *zToUuid, /* UUID for the mlink.fid. "" to delele */
const char *zFilename, /* Filename */
const char *zPrior /* Previous filename. NULL if unchanged */
const char *zPrior /* Previous filename. NULL if unchanged */
){
int fnid, pfnid, pid, fid;
static Stmt s1;
fnid = filename_to_fnid(zFilename);
if( zPrior==0 ){
pfnid = 0;
}else{
pfnid = filename_to_fnid(zPrior);
}
if( zFromUuid==0 ){
if( zFromUuid==0 || zFromUuid[0]==0 ){
pid = 0;
}else{
pid = uuid_to_rid(zFromUuid, 1);
}
if( zToUuid==0 ){
if( zToUuid==0 || zToUuid[0]==0 ){
fid = 0;
}else{
fid = uuid_to_rid(zToUuid, 1);
}
db_static_prepare(&s1,
"INSERT INTO mlink(mid,pid,fid,fnid,pfnid)"
"VALUES(:m,:p,:f,:n,:pfn)"
|
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
|
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
+
-
-
+
+
-
-
-
+
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
-
|
/*
** Locate a file named zName in the aFile[] array of the given
** manifest. We assume that filenames are in sorted order.
** Use a binary search. Return turn the index of the matching
** entry. Or return -1 if not found.
*/
static int find_file_in_manifest(Manifest *p, const char *zName){
static ManifestFile *manifest_file_seek_base(Manifest *p, const char *zName){
int lwr, upr;
int c;
int i;
lwr = 0;
upr = p->nFile - 1;
if( p->iFile>=lwr && p->iFile<upr ){
c = strcmp(p->aFile[p->iFile+1].zName, zName);
if( c==0 ){
return &p->aFile[++p->iFile];
}else if( c>0 ){
upr = p->iFile;
}else{
lwr = p->iFile+1;
}
}
while( lwr<=upr ){
i = (lwr+upr)/2;
c = strcmp(p->aFile[i].zName, zName);
if( c<0 ){
lwr = i+1;
}else if( c>0 ){
upr = i-1;
}else{
p->iFile = i;
return &p->aFile[i];
}
}
return i;
}
}
return -1;
return 0;
}
static ManifestFile *manifest_file_seek(Manifest *p, const char *zName){
ManifestFile *pFile;
pFile = manifest_file_seek_base(p, zName);
if( pFile && pFile->zUuid[0]==0 ) return 0;
if( pFile==0 && p->zBaseline ){
fetch_baseline(p);
pFile = manifest_file_seek_base(p->pBaseline, zName);
}
return pFile;
}
/*
** Add mlink table entries associated with manifest cid. The
** parent manifest is pid.
**
** A single mlink entry is added for every file that changed content
** and/or name going from pid to cid.
**
** Deleted files have mlink.fid=0.
** Added files have mlink.pid=0.
** Edited files have both mlink.pid!=0 and mlink.fid!=0
*/
static void add_mlink(int pid, Manifest *pParent, int cid, Manifest *pChild){
Manifest other;
Blob otherContent;
int otherRid;
int i, j;
int i;
ManifestFile *pChildFile, *pParentFile;
if( db_exists("SELECT 1 FROM mlink WHERE mid=%d", cid) ){
return;
}
assert( pParent==0 || pChild==0 );
if( pParent==0 ){
pParent = &other;
otherRid = pid;
}else{
pChild = &other;
otherRid = cid;
}
if( manifest_cache_find(otherRid, &other)==0 ){
content_get(otherRid, &otherContent);
if( blob_size(&otherContent)==0 ) return;
if( manifest_parse(&other, &otherContent)==0 ) return;
}
if( (pParent->zBaseline==0)==(pChild->zBaseline==0) ){
content_deltify(pid, cid, 0);
content_deltify(pid, cid, 0);
}
/* Use the iRename fields to find the cross-linkage between
** renamed files. */
for(j=0; j<pChild->nFile; j++){
const char *zPrior = pChild->aFile[j].zPrior;
if( zPrior && zPrior[0] ){
i = find_file_in_manifest(pParent, zPrior);
if( i>=0 ){
pChild->aFile[j].iRename = i;
pParent->aFile[i].iRename = j;
}
}
}
for(i=0, pChildFile=pChild->aFile; i<pChild->nFile; i++, pChildFile++){
/* Construct the mlink entries */
for(i=j=0; i<pParent->nFile && j<pChild->nFile; ){
if( pChildFile->zPrior ){
int c;
if( pParent->aFile[i].iRename>=0 ){
pParentFile = manifest_file_seek(pParent, pChildFile->zPrior);
if( pParentFile ){
i++;
}else if( (c = strcmp(pParent->aFile[i].zName, pChild->aFile[j].zName))<0 ){
add_one_mlink(cid, pParent->aFile[i].zUuid,0,pParent->aFile[i].zName,0);
add_one_mlink(cid, pParentFile->zUuid, pChildFile->zUuid,
i++;
}else if( c>0 ){
int rn = pChild->aFile[j].iRename;
pChildFile->zName, pChildFile->zPrior);
if( rn>=0 ){
add_one_mlink(cid, pParent->aFile[rn].zUuid, pChild->aFile[j].zUuid,
pChild->aFile[j].zName, pParent->aFile[rn].zName);
}else{
add_one_mlink(cid, 0, pChild->aFile[j].zUuid, pChild->aFile[j].zName,0);
}
}
j++;
}else{
if( strcmp(pParent->aFile[i].zUuid, pChild->aFile[j].zUuid)!=0 ){
add_one_mlink(cid, pParent->aFile[i].zUuid, pChild->aFile[j].zUuid,
pChild->aFile[j].zName, 0);
}
i++;
j++;
}
}
while( i<pParent->nFile ){
if( pParent->aFile[i].iRename<0 ){
add_one_mlink(cid, pParent->aFile[i].zUuid, 0, pParent->aFile[i].zName,0);
}
pParentFile = manifest_file_seek(pParent, pChildFile->zName);
if( pParentFile==0 ){
add_one_mlink(cid, 0, pChildFile->zUuid, pChildFile->zName, 0);
}else if( strcmp(pChildFile->zUuid, pParentFile->zUuid)!=0 ){
add_one_mlink(cid, pParentFile->zUuid, pChildFile->zUuid,
pChildFile->zName, 0);
}
i++;
}
}
while( j<pChild->nFile ){
int rn = pChild->aFile[j].iRename;
if( rn>=0 ){
add_one_mlink(cid, pParent->aFile[rn].zUuid, pChild->aFile[j].zUuid,
pChild->aFile[j].zName, pParent->aFile[rn].zName);
}else{
add_one_mlink(cid, 0, pChild->aFile[j].zUuid, pChild->aFile[j].zName,0);
}
j++;
}
manifest_cache_insert(otherRid, &other);
}
/*
** True if manifest_crosslink_begin() has been called but
** manifest_crosslink_end() is still pending.
|