Skip to content

Commit

Permalink
added keep-data option to RemoveTorrent command
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Jan 2, 2025
1 parent e8a4831 commit fa1cac4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2025-01-02

### Added

- "keep-data" option to RemoveTorrent command.

## [2.0.0] - 2024-12-20

### Changed
Expand Down
2 changes: 1 addition & 1 deletion internal/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ func (c *Console) removeTorrent(g *gocui.Gui, v *gocui.View) error {
id := c.selectedID
c.m.Unlock()

err := c.client.RemoveTorrent(id)
err := c.client.RemoveTorrent(id, false)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/rpctypes/rpctypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ type AddURIResponse struct {

// RemoveTorrentRequest contains request arguments for Session.RemoveTorrent method.
type RemoveTorrentRequest struct {
ID string
ID string
KeepData bool
}

// RemoveTorrentResponse contains response arguments for Session.RemoveTorrent method.
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func main() {
Name: "id",
Required: true,
},
cli.BoolFlag{
Name: "keep-data",
},
},
},
{
Expand Down Expand Up @@ -959,7 +962,7 @@ func handleAdd(c *cli.Context) error {
}

func handleRemove(c *cli.Context) error {
return clt.RemoveTorrent(c.String("id"))
return clt.RemoveTorrent(c.String("id"), c.Bool("keep-data"))
}

func handleCleanDatabase(c *cli.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions rainrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (c *Client) AddURI(uri string, options *AddTorrentOptions) (*rpctypes.Torre
}

// RemoveTorrent removes a torrent from remote Session and deletes its data.
func (c *Client) RemoveTorrent(id string) error {
args := rpctypes.RemoveTorrentRequest{ID: id}
func (c *Client) RemoveTorrent(id string, keepData bool) error {
args := rpctypes.RemoveTorrentRequest{ID: id, KeepData: keepData}
var reply rpctypes.RemoveTorrentResponse
return c.client.Call("Session.RemoveTorrent", args, &reply)
}
Expand Down
9 changes: 6 additions & 3 deletions torrent/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ func (s *Session) GetTorrent(id string) *Torrent {
}

// RemoveTorrent removes the torrent from the session and delete its files.
func (s *Session) RemoveTorrent(id string) error {
func (s *Session) RemoveTorrent(id string, keepData bool) error {
t, err := s.removeTorrentFromClient(id)
if t != nil {
err = s.stopAndRemoveData(t)
err = s.stopAndRemoveData(t, keepData)
}
return err
}
Expand Down Expand Up @@ -384,9 +384,12 @@ func (s *Session) removeTorrentFromClient(id string) (*Torrent, error) {
})
}

func (s *Session) stopAndRemoveData(t *Torrent) error {
func (s *Session) stopAndRemoveData(t *Torrent, keepData bool) error {
t.torrent.Close()
s.releasePort(t.torrent.port)
if keepData {
return nil
}
var err error
var dest string
if s.config.DataDirIncludesTorrentID {
Expand Down
4 changes: 2 additions & 2 deletions torrent/session_rpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newTorrent(t *Torrent) rpctypes.Torrent {
}

func (h *rpcHandler) RemoveTorrent(args *rpctypes.RemoveTorrentRequest, reply *rpctypes.RemoveTorrentResponse) error {
return h.session.RemoveTorrent(args.ID)
return h.session.RemoveTorrent(args.ID, args.KeepData)
}

func (h *rpcHandler) GetMagnet(args *rpctypes.GetMagnetRequest, reply *rpctypes.GetMagnetResponse) error {
Expand Down Expand Up @@ -523,7 +523,7 @@ func (h *rpcHandler) handleMoveTorrent(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = h.session.stopAndRemoveData(t)
err = h.session.stopAndRemoveData(t, false)
if err != nil {
h.session.log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion torrent/session_torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (t *Torrent) Move(target string) error {
return fmt.Errorf("http error: %d", resp.StatusCode)
}
defer resp.Body.Close()
return t.torrent.session.RemoveTorrent(t.torrent.id)
return t.torrent.session.RemoveTorrent(t.torrent.id, true)
}

func (t *Torrent) prepareBody(pw *io.PipeWriter, mw *multipart.Writer, spec *boltdbresumer.Spec) {
Expand Down

0 comments on commit fa1cac4

Please sign in to comment.