-1

I am writing a file transfer program, which I would like to know that the destination has enough disk space before start transfer.

I used method found here to create a "sparse" (or not) file:


func main() {
    f, err := os.Create("foo.bar")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    if err := f.Truncate(1e7); err != nil {
        log.Fatal(err)
    }
}

My question is, will the "Truncate()" function create a real file, which actually uses that much disk space, or it just create a record in the "FAT" table to "claim" that the file uses that much space?

In another word, will Truncate() fail if there is not enough disk space?

EDIT

I removed the "go" tag, as it is NOT related to golang. And to emphasize, my purpose is to create a file of specific size that ACTUALLY uses the space, so that if there is not enough disk space, file creation will fail.

xrfang
  • 1,754
  • 4
  • 18
  • 36

1 Answers1

0

My question is, will the "Truncate()" function create a real file, which actually uses that much disk space, or it just create a record in the "FAT" table to "claim" that the file uses that much space?

This depends on the underlying file system. Some support sparse files, others don't. If the file system does not support sparse files the space need to be actually allocated. See here for some information which file systems sparse files and which not.

In another word, will Truncate() fail if there is not enough disk space?

Truncate will fail, if there is not enough disk space. But what exactly means "not enough" depends on the file system. If it supports sparse files it only needs the space for the actually written data and some overhead.

If you want to make sure that the actual disk space which might ever be needed for a file of this size will be allocated, then you need to actually write the data and not just call truncate or similar.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172