@@ -1058,6 +1058,12 @@ create_tables_mysql (SeafRepoManager *mgr)
10581058 if (seaf_db_query (db , sql ) < 0 )
10591059 return -1 ;
10601060
1061+ sql = "CREATE TABLE IF NOT EXISTS WebUploadTempFiles ( "
1062+ "id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, repo_id CHAR(40) NOT NULL, "
1063+ "file_path TEXT NOT NULL, tmp_file_path TEXT NOT NULL) ENGINE=INNODB" ;
1064+ if (seaf_db_query (db , sql ) < 0 )
1065+ return -1 ;
1066+
10611067 return 0 ;
10621068}
10631069
@@ -1206,6 +1212,11 @@ create_tables_sqlite (SeafRepoManager *mgr)
12061212 if (seaf_db_query (db , sql ) < 0 )
12071213 return -1 ;
12081214
1215+ sql = "CREATE TABLE IF NOT EXISTS WebUploadTempFiles (repo_id CHAR(40) NOT NULL, "
1216+ "file_path TEXT NOT NULL, tmp_file_path TEXT NOT NULL)" ;
1217+ if (seaf_db_query (db , sql ) < 0 )
1218+ return -1 ;
1219+
12091220 return 0 ;
12101221}
12111222
@@ -1344,6 +1355,11 @@ create_tables_pgsql (SeafRepoManager *mgr)
13441355 if (seaf_db_query (db , sql ) < 0 )
13451356 return -1 ;
13461357
1358+ sql = "CREATE TABLE IF NOT EXISTS WebUploadTempFiles (repo_id CHAR(40) NOT NULL, "
1359+ "file_path TEXT NOT NULL, tmp_file_path TEXT NOT NULL)" ;
1360+ if (seaf_db_query (db , sql ) < 0 )
1361+ return -1 ;
1362+
13471363 sql = "CREATE TABLE IF NOT EXISTS RepoInfo (repo_id CHAR(36) PRIMARY KEY, "
13481364 "name VARCHAR(255) NOT NULL, update_time BIGINT, version INTEGER, "
13491365 "is_encrypted INTEGER, last_modifier VARCHAR(255))" ;
@@ -3918,6 +3934,119 @@ seaf_get_total_storage (GError **error)
39183934 return size ;
39193935}
39203936
3937+ int
3938+ seaf_repo_manager_add_upload_tmp_file (SeafRepoManager * mgr ,
3939+ const char * repo_id ,
3940+ const char * file_path ,
3941+ const char * tmp_file ,
3942+ GError * * error )
3943+ {
3944+ int ret = seaf_db_statement_query (mgr -> seaf -> db ,
3945+ "INSERT INTO WebUploadTempFiles "
3946+ "(repo_id, file_path, tmp_file_path) "
3947+ "VALUES (?, ?, ?)" , 3 , "string" , repo_id ,
3948+ "string" , file_path , "string" , tmp_file );
3949+
3950+ if (ret < 0 ) {
3951+ g_set_error (error , SEAFILE_DOMAIN , SEAF_ERR_GENERAL ,
3952+ "Failed to add upload tmp file record to db." );
3953+ }
3954+
3955+ return ret ;
3956+ }
3957+
3958+ int
3959+ seaf_repo_manager_del_upload_tmp_file (SeafRepoManager * mgr ,
3960+ const char * repo_id ,
3961+ const char * file_path ,
3962+ GError * * error )
3963+ {
3964+ int ret = seaf_db_statement_query (mgr -> seaf -> db ,
3965+ "DELETE FROM WebUploadTempFiles WHERE "
3966+ "repo_id = ? AND file_path = ?" ,
3967+ 2 , "string" , repo_id , "string" , file_path );
3968+ if (ret < 0 ) {
3969+ g_set_error (error , SEAFILE_DOMAIN , SEAF_ERR_GENERAL ,
3970+ "Failed to delete upload tmp file record from db." );
3971+ }
3972+
3973+ return ret ;
3974+ }
3975+
3976+ static gboolean
3977+ get_tmp_file_path (SeafDBRow * row , void * data )
3978+ {
3979+ char * * path = data ;
3980+
3981+ * path = g_strdup (seaf_db_row_get_column_text (row , 0 ));
3982+
3983+ return FALSE;
3984+ }
3985+
3986+ char *
3987+ seaf_repo_manager_get_upload_tmp_file (SeafRepoManager * mgr ,
3988+ const char * repo_id ,
3989+ const char * file_path ,
3990+ GError * * error )
3991+ {
3992+ char * tmp_file_path = NULL ;
3993+
3994+ int ret = seaf_db_statement_foreach_row (mgr -> seaf -> db ,
3995+ "SELECT tmp_file_path FROM WebUploadTempFiles "
3996+ "WHERE repo_id = ? AND file_path = ?" ,
3997+ get_tmp_file_path , & tmp_file_path ,
3998+ 2 , "string" , repo_id , "string" , file_path );
3999+ if (ret < 0 ) {
4000+ g_set_error (error , SEAFILE_DOMAIN , SEAF_ERR_GENERAL ,
4001+ "Failed to get upload temp file path from db." );
4002+ return NULL ;
4003+ }
4004+
4005+ return tmp_file_path ;
4006+ }
4007+
4008+ gint64
4009+ seaf_repo_manager_get_upload_tmp_file_offset (SeafRepoManager * mgr ,
4010+ const char * repo_id ,
4011+ const char * file_path ,
4012+ GError * * error )
4013+ {
4014+ char * tmp_file_path = NULL ;
4015+ SeafStat file_stat ;
4016+
4017+ tmp_file_path = seaf_repo_manager_get_upload_tmp_file (mgr , repo_id ,
4018+ file_path , error );
4019+ if (* error ) {
4020+ return -1 ;
4021+ }
4022+
4023+ if (!tmp_file_path )
4024+ return 0 ;
4025+
4026+ if (seaf_stat (tmp_file_path , & file_stat ) < 0 ) {
4027+ if (errno == ENOENT ) {
4028+ seaf_message ("Temp file %s doesn't exist, remove reocrd from db.\n" ,
4029+ tmp_file_path );
4030+ if (seaf_repo_manager_del_upload_tmp_file (mgr , repo_id ,
4031+ file_path , error ) < 0 ) {
4032+ g_free (tmp_file_path );
4033+ return -1 ;
4034+ }
4035+ return 0 ;
4036+ }
4037+ seaf_warning ("Failed to stat temp file %s: %s.\n" ,
4038+ tmp_file_path , strerror (errno ));
4039+ g_set_error (error , SEAFILE_DOMAIN , SEAF_ERR_GENERAL ,
4040+ "Failed to stat temp file." );
4041+ g_free (tmp_file_path );
4042+ return -1 ;
4043+ }
4044+
4045+ g_free (tmp_file_path );
4046+
4047+ return file_stat .st_size ;
4048+ }
4049+
39214050void
39224051seaf_repo_manager_update_repo_info (SeafRepoManager * mgr ,
39234052 const char * repo_id , const char * head_commit_id )
0 commit comments