1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* dir.c, file.c, io.c (rb_sys_fail_path): use rb_sys_fail_str.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34788 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-02-24 07:48:34 +00:00
parent 7d742d47cc
commit adcb7e9e7b
4 changed files with 36 additions and 46 deletions

View file

@ -1,4 +1,6 @@
Fri Feb 24 16:39:57 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Feb 24 16:48:29 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c, file.c, io.c (rb_sys_fail_path): use rb_sys_fail_str.
* error.c: new functions to deal exceptions with string instances. * error.c: new functions to deal exceptions with string instances.

2
dir.c
View file

@ -75,7 +75,7 @@ char *strchr(char*,char);
#define opendir(p) rb_w32_uopendir(p) #define opendir(p) rb_w32_uopendir(p)
#endif #endif
#define rb_sys_fail_path(path) rb_sys_fail(RSTRING_PTR(path)) #define rb_sys_fail_path(path) rb_sys_fail_str(path)
#define FNM_NOESCAPE 0x01 #define FNM_NOESCAPE 0x01
#define FNM_PATHNAME 0x02 #define FNM_PATHNAME 0x02

74
file.c
View file

@ -94,7 +94,7 @@ int flock(int, int);
#define STAT(p, s) stat((p), (s)) #define STAT(p, s) stat((p), (s))
#endif #endif
#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path)) #define rb_sys_fail_path(path) rb_sys_fail_str(path)
#if defined(__BEOS__) || defined(__HAIKU__) /* should not change ID if -1 */ #if defined(__BEOS__) || defined(__HAIKU__) /* should not change ID if -1 */
static int static int
@ -211,16 +211,18 @@ rb_str_encode_ospath(VALUE path)
} }
static long static long
apply2files(void (*func)(const char *, void *), VALUE vargs, void *arg) apply2files(void (*func)(const char *, VALUE, void *), VALUE vargs, void *arg)
{ {
long i; long i;
volatile VALUE path; volatile VALUE path;
rb_secure(4); rb_secure(4);
for (i=0; i<RARRAY_LEN(vargs); i++) { for (i=0; i<RARRAY_LEN(vargs); i++) {
const char *s;
path = rb_get_path(RARRAY_PTR(vargs)[i]); path = rb_get_path(RARRAY_PTR(vargs)[i]);
path = rb_str_encode_ospath(path); path = rb_str_encode_ospath(path);
(*func)(StringValueCStr(path), arg); s = RSTRING_PTR(path);
(*func)(s, path, arg);
} }
return RARRAY_LEN(vargs); return RARRAY_LEN(vargs);
@ -1953,10 +1955,10 @@ rb_file_size(VALUE obj)
} }
static void static void
chmod_internal(const char *path, void *mode) chmod_internal(const char *path, VALUE pathv, void *mode)
{ {
if (chmod(path, *(int *)mode) < 0) if (chmod(path, *(int *)mode) < 0)
rb_sys_fail(path); rb_sys_fail_path(pathv);
} }
/* /*
@ -2029,10 +2031,10 @@ rb_file_chmod(VALUE obj, VALUE vmode)
#if defined(HAVE_LCHMOD) #if defined(HAVE_LCHMOD)
static void static void
lchmod_internal(const char *path, void *mode) lchmod_internal(const char *path, VALUE pathv, void *mode)
{ {
if (lchmod(path, (int)(VALUE)mode) < 0) if (lchmod(path, (int)(VALUE)mode) < 0)
rb_sys_fail(path); rb_sys_fail_path(pathv);
} }
/* /*
@ -2069,11 +2071,11 @@ struct chown_args {
}; };
static void static void
chown_internal(const char *path, void *arg) chown_internal(const char *path, VALUE pathv, void *arg)
{ {
struct chown_args *args = arg; struct chown_args *args = arg;
if (chown(path, args->owner, args->group) < 0) if (chown(path, args->owner, args->group) < 0)
rb_sys_fail(path); rb_sys_fail_path(pathv);
} }
/* /*
@ -2160,11 +2162,11 @@ rb_file_chown(VALUE obj, VALUE owner, VALUE group)
#if defined(HAVE_LCHOWN) #if defined(HAVE_LCHOWN)
static void static void
lchown_internal(const char *path, void *arg) lchown_internal(const char *path, VALUE pathv, void *arg)
{ {
struct chown_args *args = arg; struct chown_args *args = arg;
if (lchown(path, args->owner, args->group) < 0) if (lchown(path, args->owner, args->group) < 0)
rb_sys_fail(path); rb_sys_fail_path(pathv);
} }
/* /*
@ -2213,10 +2215,10 @@ struct utime_args {
}; };
#if defined DOSISH || defined __CYGWIN__ #if defined DOSISH || defined __CYGWIN__
NORETURN(static void utime_failed(const char *, const struct timespec *, VALUE, VALUE)); NORETURN(static void utime_failed(VALUE, const struct timespec *, VALUE, VALUE));
static void static void
utime_failed(const char *path, const struct timespec *tsp, VALUE atime, VALUE mtime) utime_failed(VALUE path, const struct timespec *tsp, VALUE atime, VALUE mtime)
{ {
if (tsp && errno == EINVAL) { if (tsp && errno == EINVAL) {
VALUE e[2], a = Qnil, m = Qnil; VALUE e[2], a = Qnil, m = Qnil;
@ -2237,23 +2239,23 @@ utime_failed(const char *path, const struct timespec *tsp, VALUE atime, VALUE mt
if (!NIL_P(e[0])) { if (!NIL_P(e[0])) {
if (path) { if (path) {
if (!d) e[0] = rb_str_dup(e[0]); if (!d) e[0] = rb_str_dup(e[0]);
rb_str_cat2(rb_str_cat2(e[0], " for "), path); rb_str_append(rb_str_cat2(e[0], " for "), path);
} }
e[1] = INT2FIX(EINVAL); e[1] = INT2FIX(EINVAL);
rb_exc_raise(rb_class_new_instance(2, e, rb_eSystemCallError)); rb_exc_raise(rb_class_new_instance(2, e, rb_eSystemCallError));
} }
errno = EINVAL; errno = EINVAL;
} }
rb_sys_fail(path); rb_sys_fail_path(path);
} }
#else #else
#define utime_failed(path, tsp, atime, mtime) rb_sys_fail(path) #define utime_failed(path, tsp, atime, mtime) rb_sys_fail_path(path)
#endif #endif
#if defined(HAVE_UTIMES) #if defined(HAVE_UTIMES)
static void static void
utime_internal(const char *path, void *arg) utime_internal(const char *path, VALUE pathv, void *arg)
{ {
struct utime_args *v = arg; struct utime_args *v = arg;
const struct timespec *tsp = v->tsp; const struct timespec *tsp = v->tsp;
@ -2283,7 +2285,7 @@ no_utimensat:
tvp = tvbuf; tvp = tvbuf;
} }
if (utimes(path, tvp) < 0) if (utimes(path, tvp) < 0)
utime_failed(path, tsp, v->atime, v->mtime); utime_failed(pathv, tsp, v->atime, v->mtime);
} }
#else #else
@ -2296,7 +2298,7 @@ struct utimbuf {
#endif #endif
static void static void
utime_internal(const char *path, void *arg) utime_internal(const char *path, VALUE pathv, void *arg)
{ {
struct utime_args *v = arg; struct utime_args *v = arg;
const struct timespec *tsp = v->tsp; const struct timespec *tsp = v->tsp;
@ -2307,7 +2309,7 @@ utime_internal(const char *path, void *arg)
utp = &utbuf; utp = &utbuf;
} }
if (utime(path, utp) < 0) if (utime(path, utp) < 0)
utime_failed(path, tsp, v->atime, v->mtime); utime_failed(pathv, tsp, v->atime, v->mtime);
} }
#endif #endif
@ -2347,33 +2349,19 @@ NORETURN(static void sys_fail2(VALUE,VALUE));
static void static void
sys_fail2(VALUE s1, VALUE s2) sys_fail2(VALUE s1, VALUE s2)
{ {
char *buf; VALUE str;
#ifdef MAX_PATH #ifdef MAX_PATH
const int max_pathlen = MAX_PATH; const int max_pathlen = MAX_PATH;
#else #else
const int max_pathlen = MAXPATHLEN; const int max_pathlen = MAXPATHLEN;
#endif #endif
const char *e1, *e2;
int len = 5;
long l1 = RSTRING_LEN(s1), l2 = RSTRING_LEN(s2);
e1 = e2 = ""; str = rb_str_new_cstr("(");
if (l1 > max_pathlen) { rb_str_append(str, rb_str_ellipsize(s1, max_pathlen));
l1 = max_pathlen - 3; rb_str_cat2(str, ", ");
e1 = "..."; rb_str_append(str, rb_str_ellipsize(s2, max_pathlen));
len += 3; rb_str_cat2(str, ")");
} rb_sys_fail_path(str);
if (l2 > max_pathlen) {
l2 = max_pathlen - 3;
e2 = "...";
len += 3;
}
len += (int)l1 + (int)l2;
buf = ALLOCA_N(char, len);
snprintf(buf, len, "(%.*s%s, %.*s%s)",
(int)l1, RSTRING_PTR(s1), e1,
(int)l2, RSTRING_PTR(s2), e2);
rb_sys_fail(buf);
} }
#ifdef HAVE_LINK #ifdef HAVE_LINK
@ -2492,10 +2480,10 @@ rb_readlink(VALUE path)
#endif #endif
static void static void
unlink_internal(const char *path, void *arg) unlink_internal(const char *path, VALUE pathv, void *arg)
{ {
if (unlink(path) < 0) if (unlink(path) < 0)
rb_sys_fail(path); rb_sys_fail_path(pathv);
} }
/* /*

2
io.c
View file

@ -374,7 +374,7 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
# endif # endif
#endif #endif
#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path)) #define rb_sys_fail_path(path) rb_sys_fail_str(path)
static int io_fflush(rb_io_t *); static int io_fflush(rb_io_t *);