truncate(p1, p2)
public
Truncates the file file_name to be at most integer bytes
long. Not available on all platforms.
f = File.new("out", "w")
f.write("1234567890")
f.close
File.truncate("out", 5)
File.size("out")
Show source
static VALUE
rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
{
off_t pos;
long pos;
pos = NUM2POS(len);
FilePathValue(path);
path = rb_str_encode_ospath(path);
if (truncate(StringValueCStr(path), pos) < 0)
rb_sys_fail_path(path);
{
int tmpfd;
if ((tmpfd = rb_cloexec_open(StringValueCStr(path), 0, 0)) < 0) {
rb_sys_fail_path(path);
}
rb_update_max_fd(tmpfd);
if (chsize(tmpfd, pos) < 0) {
int e = errno;
close(tmpfd);
rb_syserr_fail_path(e, path);
}
close(tmpfd);
}
return INT2FIX(0);
}