2021-12-06 02:19:28 -05:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2021-12-06 02:19:28 -05:00
package repo
import (
"context"
"fmt"
2022-11-15 03:08:59 -05:00
"strconv"
"strings"
2021-12-06 02:19:28 -05:00
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/timeutil"
2022-11-15 03:08:59 -05:00
"code.gitea.io/gitea/modules/util"
2021-12-06 02:19:28 -05:00
"xorm.io/builder"
)
// ArchiverStatus represents repo archive status
type ArchiverStatus int
// enumerate all repo archive statuses
const (
ArchiverGenerating = iota // the archiver is generating
ArchiverReady // it's ready
)
// RepoArchiver represents all archivers
type RepoArchiver struct { //revive:disable-line:exported
ID int64 ` xorm:"pk autoincr" `
RepoID int64 ` xorm:"index unique(s)" `
Type git . ArchiveType ` xorm:"unique(s)" `
Status ArchiverStatus
2024-01-19 11:05:02 -05:00
CommitID string ` xorm:"VARCHAR(64) unique(s)" `
2021-12-06 02:19:28 -05:00
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX NOT NULL created" `
2024-04-02 09:34:57 -05:00
ReleaseID int64 ` xorm:"-" `
2021-12-06 02:19:28 -05:00
}
func init ( ) {
db . RegisterModel ( new ( RepoArchiver ) )
}
2022-08-29 04:45:20 -05:00
// RelativePath returns the archive path relative to the archive storage root.
func ( archiver * RepoArchiver ) RelativePath ( ) string {
return fmt . Sprintf ( "%d/%s/%s.%s" , archiver . RepoID , archiver . CommitID [ : 2 ] , archiver . CommitID , archiver . Type . String ( ) )
2021-12-06 02:19:28 -05:00
}
2022-11-15 03:08:59 -05:00
// repoArchiverForRelativePath takes a relativePath created from (archiver *RepoArchiver) RelativePath() and creates a shell repoArchiver struct representing it
func repoArchiverForRelativePath ( relativePath string ) ( * RepoArchiver , error ) {
parts := strings . SplitN ( relativePath , "/" , 3 )
if len ( parts ) != 3 {
return nil , util . SilentWrap { Message : fmt . Sprintf ( "invalid storage path: %s" , relativePath ) , Err : util . ErrInvalidArgument }
}
repoID , err := strconv . ParseInt ( parts [ 0 ] , 10 , 64 )
if err != nil {
return nil , util . SilentWrap { Message : fmt . Sprintf ( "invalid storage path: %s" , relativePath ) , Err : util . ErrInvalidArgument }
}
nameExts := strings . SplitN ( parts [ 2 ] , "." , 2 )
if len ( nameExts ) != 2 {
return nil , util . SilentWrap { Message : fmt . Sprintf ( "invalid storage path: %s" , relativePath ) , Err : util . ErrInvalidArgument }
}
return & RepoArchiver {
RepoID : repoID ,
CommitID : parts [ 1 ] + nameExts [ 0 ] ,
Type : git . ToArchiveType ( nameExts [ 1 ] ) ,
} , nil
}
2021-12-06 02:19:28 -05:00
// GetRepoArchiver get an archiver
func GetRepoArchiver ( ctx context . Context , repoID int64 , tp git . ArchiveType , commitID string ) ( * RepoArchiver , error ) {
var archiver RepoArchiver
has , err := db . GetEngine ( ctx ) . Where ( "repo_id=?" , repoID ) . And ( "`type`=?" , tp ) . And ( "commit_id=?" , commitID ) . Get ( & archiver )
if err != nil {
return nil , err
}
if has {
return & archiver , nil
}
return nil , nil
}
2022-11-15 03:08:59 -05:00
// ExistsRepoArchiverWithStoragePath checks if there is a RepoArchiver for a given storage path
func ExistsRepoArchiverWithStoragePath ( ctx context . Context , storagePath string ) ( bool , error ) {
// We need to invert the path provided func (archiver *RepoArchiver) RelativePath() above
archiver , err := repoArchiverForRelativePath ( storagePath )
if err != nil {
return false , err
}
return db . GetEngine ( ctx ) . Exist ( archiver )
}
2021-12-06 02:19:28 -05:00
// UpdateRepoArchiverStatus updates archiver's status
func UpdateRepoArchiverStatus ( ctx context . Context , archiver * RepoArchiver ) error {
_ , err := db . GetEngine ( ctx ) . ID ( archiver . ID ) . Cols ( "status" ) . Update ( archiver )
return err
}
// DeleteAllRepoArchives deletes all repo archives records
2023-09-16 09:39:12 -05:00
func DeleteAllRepoArchives ( ctx context . Context ) error {
2023-12-25 15:25:29 -05:00
// 1=1 to enforce delete all data, otherwise it will delete nothing
2023-09-16 09:39:12 -05:00
_ , err := db . GetEngine ( ctx ) . Where ( "1=1" ) . Delete ( new ( RepoArchiver ) )
2021-12-06 02:19:28 -05:00
return err
}
// FindRepoArchiversOption represents an archiver options
type FindRepoArchiversOption struct {
db . ListOptions
OlderThan time . Duration
}
2024-01-14 21:19:25 -05:00
func ( opts FindRepoArchiversOption ) ToConds ( ) builder . Cond {
2022-01-20 12:46:10 -05:00
cond := builder . NewCond ( )
2021-12-06 02:19:28 -05:00
if opts . OlderThan > 0 {
cond = cond . And ( builder . Lt { "created_unix" : time . Now ( ) . Add ( - opts . OlderThan ) . Unix ( ) } )
}
return cond
}
2024-01-14 21:19:25 -05:00
func ( opts FindRepoArchiversOption ) ToOrders ( ) string {
return "created_unix ASC"
2021-12-06 02:19:28 -05:00
}
2021-12-12 10:48:20 -05:00
// SetArchiveRepoState sets if a repo is archived
2023-09-16 09:39:12 -05:00
func SetArchiveRepoState ( ctx context . Context , repo * Repository , isArchived bool ) ( err error ) {
2021-12-12 10:48:20 -05:00
repo . IsArchived = isArchived
2023-04-26 09:46:26 -05:00
if isArchived {
repo . ArchivedUnix = timeutil . TimeStampNow ( )
} else {
repo . ArchivedUnix = timeutil . TimeStamp ( 0 )
}
2023-09-16 09:39:12 -05:00
_ , err = db . GetEngine ( ctx ) . ID ( repo . ID ) . Cols ( "is_archived" , "archived_unix" ) . NoAutoTime ( ) . Update ( repo )
2022-06-20 05:02:49 -05:00
return err
2021-12-12 10:48:20 -05:00
}