Skip to content

Commit

Permalink
[Win32] Remove dead code using chsize
Browse files Browse the repository at this point in the history
Already using `rb_w32_truncate` and `rb_w32_ftruncate`, and
`HAVE_FTRUNCATE` has been added 14 years ago.
  • Loading branch information
nobu committed Sep 8, 2022
1 parent b7fa78b commit 55fef08
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions file.c
Expand Up @@ -5086,41 +5086,17 @@ rb_file_s_join(VALUE klass, VALUE args)
return rb_file_join(args);
}

#if defined(HAVE_TRUNCATE) || defined(HAVE_CHSIZE)
#if defined(HAVE_TRUNCATE)
struct truncate_arg {
const char *path;
#if defined(HAVE_TRUNCATE)
#define NUM2POS(n) NUM2OFFT(n)
off_t pos;
#else
#define NUM2POS(n) NUM2LONG(n)
long pos;
#endif
};

static void *
nogvl_truncate(void *ptr)
{
struct truncate_arg *ta = ptr;
#ifdef HAVE_TRUNCATE
return (void *)(VALUE)truncate(ta->path, ta->pos);
#else /* defined(HAVE_CHSIZE) */
{
int tmpfd = rb_cloexec_open(ta->path, 0, 0);

if (tmpfd < 0)
return (void *)-1;
rb_update_max_fd(tmpfd);
if (chsize(tmpfd, ta->pos) < 0) {
int e = errno;
close(tmpfd);
errno = e;
return (void *)-1;
}
close(tmpfd);
return 0;
}
#endif
}

/*
Expand All @@ -5144,7 +5120,7 @@ rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
struct truncate_arg ta;
int r;

ta.pos = NUM2POS(len);
ta.pos = NUM2OFFT(len);
FilePathValue(path);
path = rb_str_encode_ospath(path);
ta.path = StringValueCStr(path);
Expand All @@ -5154,34 +5130,23 @@ rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
if (r < 0)
rb_sys_fail_path(path);
return INT2FIX(0);
#undef NUM2POS
}
#else
#define rb_file_s_truncate rb_f_notimplement
#endif

#if defined(HAVE_FTRUNCATE) || defined(HAVE_CHSIZE)
#if defined(HAVE_FTRUNCATE)
struct ftruncate_arg {
int fd;
#if defined(HAVE_FTRUNCATE)
#define NUM2POS(n) NUM2OFFT(n)
off_t pos;
#else
#define NUM2POS(n) NUM2LONG(n)
long pos;
#endif
};

static VALUE
nogvl_ftruncate(void *ptr)
{
struct ftruncate_arg *fa = ptr;

#ifdef HAVE_FTRUNCATE
return (VALUE)ftruncate(fa->fd, fa->pos);
#else /* defined(HAVE_CHSIZE) */
return (VALUE)chsize(fa->fd, fa->pos);
#endif
}

/*
Expand All @@ -5204,7 +5169,7 @@ rb_file_truncate(VALUE obj, VALUE len)
rb_io_t *fptr;
struct ftruncate_arg fa;

fa.pos = NUM2POS(len);
fa.pos = NUM2OFFT(len);
GetOpenFile(obj, fptr);
if (!(fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "not opened for writing");
Expand All @@ -5215,7 +5180,6 @@ rb_file_truncate(VALUE obj, VALUE len)
rb_sys_fail_path(fptr->pathv);
}
return INT2FIX(0);
#undef NUM2POS
}
#else
#define rb_file_truncate rb_f_notimplement
Expand Down

0 comments on commit 55fef08

Please sign in to comment.