File: truncate.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (31 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package hdfs

import (
	"errors"
	"os"

	hdfs "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_hdfs"
	"google.golang.org/protobuf/proto"
)

// Truncate truncates the file specified by name to the given size, and returns
// the status any error encountered. The returned status will false in the case
// of any error or, if the error is nil, if HDFS indicated that the operation
// will be performed asynchronously and is not yet complete.
func (c *Client) Truncate(name string, size int64) (bool, error) {
	req := &hdfs.TruncateRequestProto{
		Src:        proto.String(name),
		NewLength:  proto.Uint64(uint64(size)),
		ClientName: proto.String(c.namenode.ClientName),
	}
	resp := &hdfs.TruncateResponseProto{}

	err := c.namenode.Execute("truncate", req, resp)
	if err != nil {
		return false, &os.PathError{"truncate", name, interpretException(err)}
	} else if resp.Result == nil {
		return false, &os.PathError{"truncate", name, errors.New("unexpected empty response")}
	}

	return resp.GetResult(), nil
}