| ︙ | | | ︙ | |
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
va_end(ap);
if( db_step(&s)==SQLITE_ROW ){
z = mprintf("%s", sqlite3_column_text(s.pStmt, 0));
}
db_finalize(&s);
return z;
}
#ifdef __MINGW32__
/*
** These routines (copied out of the os_win.c driver for SQLite) convert
** character strings in various microsoft multi-byte character formats
** into UTF-8. Fossil and SQLite always use only UTF-8 internally. These
** routines are needed in order to convert from the default character set
** currently in use by windows into UTF-8 when strings are imported from
** the outside world.
*/
/*
** Convert microsoft unicode to UTF-8. Space to hold the returned string is
** obtained from malloc().
** Copied from sqlite3.c as is (petr)
*/
static char *unicodeToUtf8(const WCHAR *zWideFilename){
int nByte;
char *zFilename;
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
zFilename = malloc( nByte );
if( zFilename==0 ){
return 0;
}
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
0, 0);
if( nByte == 0 ){
free(zFilename);
zFilename = 0;
}
return zFilename;
}
/*
** Convert an ansi string to microsoft unicode, based on the
** current codepage settings for file apis.
**
** Space to hold the returned string is obtained
** from malloc.
*/
static WCHAR *mbcsToUnicode(const char *zFilename){
int nByte;
WCHAR *zMbcsFilename;
int codepage = CP_ACP;
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
if( zMbcsFilename==0 ){
return 0;
}
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
if( nByte==0 ){
free(zMbcsFilename);
zMbcsFilename = 0;
}
return zMbcsFilename;
}
/*
** Convert multibyte character string to UTF-8. Space to hold the
** returned string is obtained from malloc().
*/
static char *mbcsToUtf8(const char *zFilename){
char *zFilenameUtf8;
WCHAR *zTmpWide;
zTmpWide = mbcsToUnicode(zFilename);
if( zTmpWide==0 ){
return 0;
}
zFilenameUtf8 = unicodeToUtf8(zTmpWide);
free(zTmpWide);
return zFilenameUtf8;
}
#endif /* __MINGW32__ */
/*
** Initialize a new database file with the given schema. If anything
** goes wrong, call db_err() to exit.
*/
void db_init_database(
const char *zFileName, /* Name of database file to create */
const char *zSchema, /* First part of schema */
... /* Additional SQL to run. Terminate with NULL. */
){
sqlite3 *db;
int rc;
const char *zSql;
va_list ap;
#ifdef __MINGW32__
zFileName = mbcsToUtf8(zFileName);
#endif
rc = sqlite3_open(zFileName, &db);
if( rc!=SQLITE_OK ){
db_err(sqlite3_errmsg(db));
}
sqlite3_busy_timeout(db, 5000);
sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
va_end(ap);
if( db_step(&s)==SQLITE_ROW ){
z = mprintf("%s", sqlite3_column_text(s.pStmt, 0));
}
db_finalize(&s);
return z;
}
/*
** Initialize a new database file with the given schema. If anything
** goes wrong, call db_err() to exit.
*/
void db_init_database(
const char *zFileName, /* Name of database file to create */
const char *zSchema, /* First part of schema */
... /* Additional SQL to run. Terminate with NULL. */
){
sqlite3 *db;
int rc;
const char *zSql;
va_list ap;
#ifdef __MINGW32__
zFileName = sqlite3_win32_mbcs_to_utf8(zFileName);
#endif
rc = sqlite3_open(zFileName, &db);
if( rc!=SQLITE_OK ){
db_err(sqlite3_errmsg(db));
}
sqlite3_busy_timeout(db, 5000);
sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
|
| ︙ | | | ︙ | |
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
static sqlite3 *openDatabase(const char *zDbName){
int rc;
const char *zVfs;
sqlite3 *db;
zVfs = getenv("FOSSIL_VFS");
#ifdef __MINGW32__
zDbName = mbcsToUtf8(zDbName);
#endif
rc = sqlite3_open_v2(
zDbName, &db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
zVfs
);
if( rc!=SQLITE_OK ){
|
|
|
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
static sqlite3 *openDatabase(const char *zDbName){
int rc;
const char *zVfs;
sqlite3 *db;
zVfs = getenv("FOSSIL_VFS");
#ifdef __MINGW32__
zDbName = sqlite3_win32_mbcs_to_utf8(zDbName);
#endif
rc = sqlite3_open_v2(
zDbName, &db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
zVfs
);
if( rc!=SQLITE_OK ){
|
| ︙ | | | ︙ | |
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
*/
void db_open_or_attach(const char *zDbName, const char *zLabel){
if( !g.db ){
g.db = openDatabase(zDbName);
db_connection_init();
}else{
#ifdef __MINGW32__
zDbName = mbcsToUtf8(zDbName);
#endif
db_multi_exec("ATTACH DATABASE %Q AS %s", zDbName, zLabel);
}
}
/*
** Open the user database in "~/.fossil". Create the database anew if
|
|
|
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
*/
void db_open_or_attach(const char *zDbName, const char *zLabel){
if( !g.db ){
g.db = openDatabase(zDbName);
db_connection_init();
}else{
#ifdef __MINGW32__
zDbName = sqlite3_win32_mbcs_to_utf8(zDbName);
#endif
db_multi_exec("ATTACH DATABASE %Q AS %s", zDbName, zLabel);
}
}
/*
** Open the user database in "~/.fossil". Create the database anew if
|
| ︙ | | | ︙ | |
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
|
}
db_open_repository(zRep);
if( g.repositoryOpen ){
return;
}
rep_not_found:
if( errIfNotFound ){
fossil_fatal("use --repository or -R to specific the repository database");
}
}
/*
** Open the local database. If unable, exit with an error.
*/
void db_must_be_within_tree(void){
|
|
|
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
|
}
db_open_repository(zRep);
if( g.repositoryOpen ){
return;
}
rep_not_found:
if( errIfNotFound ){
fossil_fatal("use --repository or -R to specify the repository database");
}
}
/*
** Open the local database. If unable, exit with an error.
*/
void db_must_be_within_tree(void){
|
| ︙ | | | ︙ | |
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
|
);
isNewRepo = 1;
}
/*
** Create the default user accounts in the USER table.
*/
void db_create_default_users(int setupUserOnly){
const char *zUser;
zUser = db_get("default-user", 0);
if( zUser==0 ){
#ifdef __MINGW32__
zUser = getenv("USERNAME");
#else
zUser = getenv("USER");
#endif
}
|
|
>
>
>
|
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
);
isNewRepo = 1;
}
/*
** Create the default user accounts in the USER table.
*/
void db_create_default_users(int setupUserOnly, const char *zDefaultUser){
const char *zUser;
zUser = db_get("default-user", 0);
if( zUser==0 ){
zUser = zDefaultUser;
}
if( zUser==0 ){
#ifdef __MINGW32__
zUser = getenv("USERNAME");
#else
zUser = getenv("USER");
#endif
}
|
| ︙ | | | ︙ | |
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
|
** new repositories.
**
** The zInitialDate parameter determines the date of the initial check-in
** that is automatically created. If zInitialDate is 0 then no initial
** check-in is created. The makeServerCodes flag determines whether or
** not server and project codes are invented for this repository.
*/
void db_initial_setup (const char *zInitialDate, int makeServerCodes){
char *zDate;
Blob hash;
Blob manifest;
db_set("content-schema", CONTENT_SCHEMA, 0);
db_set("aux-schema", AUX_SCHEMA, 0);
if( makeServerCodes ){
db_multi_exec(
"INSERT INTO config(name,value)"
" VALUES('server-code', lower(hex(randomblob(20))));"
"INSERT INTO config(name,value)"
" VALUES('project-code', lower(hex(randomblob(20))));"
);
}
if( !db_is_global("autosync") ) db_set_int("autosync", 1, 0);
if( !db_is_global("localauth") ) db_set_int("localauth", 0, 0);
db_create_default_users(0);
user_select();
if( zInitialDate ){
int rid;
blob_zero(&manifest);
blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
zDate = db_text(0, "SELECT datetime(%Q)", zInitialDate);
|
|
|
|
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
|
** new repositories.
**
** The zInitialDate parameter determines the date of the initial check-in
** that is automatically created. If zInitialDate is 0 then no initial
** check-in is created. The makeServerCodes flag determines whether or
** not server and project codes are invented for this repository.
*/
void db_initial_setup (const char *zInitialDate, const char *zDefaultUser, int makeServerCodes){
char *zDate;
Blob hash;
Blob manifest;
db_set("content-schema", CONTENT_SCHEMA, 0);
db_set("aux-schema", AUX_SCHEMA, 0);
if( makeServerCodes ){
db_multi_exec(
"INSERT INTO config(name,value)"
" VALUES('server-code', lower(hex(randomblob(20))));"
"INSERT INTO config(name,value)"
" VALUES('project-code', lower(hex(randomblob(20))));"
);
}
if( !db_is_global("autosync") ) db_set_int("autosync", 1, 0);
if( !db_is_global("localauth") ) db_set_int("localauth", 0, 0);
db_create_default_users(0, zDefaultUser);
user_select();
if( zInitialDate ){
int rid;
blob_zero(&manifest);
blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
zDate = db_text(0, "SELECT datetime(%Q)", zInitialDate);
|
| ︙ | | | ︙ | |
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
|
manifest_crosslink(rid, &manifest);
}
}
/*
** COMMAND: new
**
** Usage: %fossil new FILENAME
**
** Create a repository for a new project in the file named FILENAME.
** This command is distinct from "clone". The "clone" command makes
** a copy of an existing project. This command starts a new project.
*/
void create_repository_cmd(void){
char *zPassword;
const char *zDate; /* Date of the initial check-in */
zDate = find_option("date-override",0,1);
if( zDate==0 ) zDate = "now";
if( g.argc!=3 ){
usage("REPOSITORY-NAME");
}
db_create_repository(g.argv[2]);
db_open_repository(g.argv[2]);
db_open_config(0);
db_begin_transaction();
db_initial_setup(zDate, 1);
db_end_transaction(0);
printf("project-id: %s\n", db_get("project-code", 0));
printf("server-id: %s\n", db_get("server-code", 0));
zPassword = db_text(0, "SELECT pw FROM user WHERE login=%Q", g.zLogin);
printf("admin-user: %s (initial password is \"%s\")\n", g.zLogin, zPassword);
}
|
|
>
>
>
>
>
>
>
>
>
>
>
|
|
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
|
manifest_crosslink(rid, &manifest);
}
}
/*
** COMMAND: new
**
** Usage: %fossil new ?OPTIONS? FILENAME
**
** Create a repository for a new project in the file named FILENAME.
** This command is distinct from "clone". The "clone" command makes
** a copy of an existing project. This command starts a new project.
**
** By default, your current login name is used to create the default
** admin user. This can be overridden using the -A|--admin-user
** parameter.
**
** Options:
**
** --admin-user|-A USERNAME
**
*/
void create_repository_cmd(void){
char *zPassword;
const char *zDate; /* Date of the initial check-in */
const char *zDefaultUser; /* Optional name of the default user */
zDate = find_option("date-override",0,1);
zDefaultUser = find_option("admin-user","A",1);
if( zDate==0 ) zDate = "now";
if( g.argc!=3 ){
usage("REPOSITORY-NAME");
}
db_create_repository(g.argv[2]);
db_open_repository(g.argv[2]);
db_open_config(0);
db_begin_transaction();
db_initial_setup(zDate, zDefaultUser, 1);
db_end_transaction(0);
printf("project-id: %s\n", db_get("project-code", 0));
printf("server-id: %s\n", db_get("server-code", 0));
zPassword = db_text(0, "SELECT pw FROM user WHERE login=%Q", g.zLogin);
printf("admin-user: %s (initial password is \"%s\")\n", g.zLogin, zPassword);
}
|
| ︙ | | | ︙ | |