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")
static VALUE
rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
{
#ifdef HAVE_TRUNCATE
#define NUM2POS(n) NUM2OFFT(n)
off_t pos;
#else
#define NUM2POS(n) NUM2LONG(n)
long pos;
#endif
pos = NUM2POS(len);
FilePathValue(path);
path = rb_str_encode_ospath(path);
#ifdef HAVE_TRUNCATE
if (truncate(StringValueCStr(path), pos) < 0)
rb_sys_fail_path(path);
#else /* defined(HAVE_CHSIZE) */
{
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);
}
#endif
return INT2FIX(0);
#undef NUM2POS
}