Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
874797f3
Commit
874797f3
authored
Jan 06, 2020
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dearblue-file-size-truncate'
parents
ccf28775
b545f62a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
130 additions
and
8 deletions
+130
-8
mrbgems/mruby-io/include/mruby/ext/io.h
mrbgems/mruby-io/include/mruby/ext/io.h
+1
-1
mrbgems/mruby-io/src/file.c
mrbgems/mruby-io/src/file.c
+102
-3
mrbgems/mruby-io/src/io.c
mrbgems/mruby-io/src/io.c
+11
-4
mrbgems/mruby-io/test/file.rb
mrbgems/mruby-io/test/file.rb
+16
-0
No files found.
mrbgems/mruby-io/include/mruby/ext/io.h
View file @
874797f3
...
...
@@ -30,7 +30,7 @@ struct mrb_io {
#define E_IO_ERROR (mrb_class_get(mrb, "IOError"))
#define E_EOF_ERROR (mrb_class_get(mrb, "EOFError"))
mrb_value
mrb_io_fileno
(
mrb_state
*
mrb
,
mrb_value
io
);
int
mrb_io_fileno
(
mrb_state
*
mrb
,
mrb_value
io
);
#if defined(__cplusplus)
}
/* extern "C" { */
...
...
mrbgems/mruby-io/src/file.c
View file @
874797f3
...
...
@@ -65,7 +65,15 @@
#define LOCK_UN 8
#endif
#define STAT(p, s) stat(p, s)
#ifndef _WIN32
typedef
struct
stat
mrb_stat
;
# define mrb_stat(path, sb) stat(path, sb)
# define mrb_fstat(fd, sb) fstat(fd, sb)
#else
typedef
struct
__stat64
mrb_stat
;
# define mrb_stat(path, sb) _stat64(path, sb)
# define mrb_fstat(fd, sb) _fstat64(fd, sb)
#endif
#ifdef _WIN32
static
int
...
...
@@ -339,7 +347,7 @@ mrb_file_mtime(mrb_state *mrb, mrb_value self)
int
fd
;
obj
=
mrb_obj_value
(
mrb_class_get
(
mrb
,
"Time"
));
fd
=
(
int
)
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
self
)
);
fd
=
mrb_io_fileno
(
mrb
,
self
);
if
(
fstat
(
fd
,
&
st
)
==
-
1
)
return
mrb_false_value
();
return
mrb_funcall
(
mrb
,
obj
,
"at"
,
1
,
mrb_fixnum_value
(
st
.
st_mtime
));
...
...
@@ -355,7 +363,7 @@ mrb_file_flock(mrb_state *mrb, mrb_value self)
int
fd
;
mrb_get_args
(
mrb
,
"i"
,
&
operation
);
fd
=
(
int
)
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
self
)
);
fd
=
mrb_io_fileno
(
mrb
,
self
);
while
(
flock
(
fd
,
(
int
)
operation
)
==
-
1
)
{
switch
(
errno
)
{
...
...
@@ -379,6 +387,95 @@ mrb_file_flock(mrb_state *mrb, mrb_value self)
return
mrb_fixnum_value
(
0
);
}
static
mrb_value
mrb_file_size
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_stat
st
;
int
fd
;
fd
=
mrb_io_fileno
(
mrb
,
self
);
if
(
mrb_fstat
(
fd
,
&
st
)
==
-
1
)
{
mrb_raise
(
mrb
,
E_RUNTIME_ERROR
,
"fstat failed"
);
}
if
(
st
.
st_size
>
MRB_INT_MAX
)
{
#ifdef MRB_WITHOUT_FLOAT
mrb_raise
(
mrb
,
E_RUNTIME_ERROR
,
"File#size too large for MRB_WITHOUT_FLOAT"
);
#else
return
mrb_float_value
(
mrb
,
st
.
st_size
);
#endif
}
return
mrb_fixnum_value
(
st
.
st_size
);
}
static
int
mrb_ftruncate
(
int
fd
,
int64_t
length
)
{
#ifndef _WIN32
return
ftruncate
(
fd
,
(
off_t
)
length
);
#else
HANDLE
file
;
__int64
cur
;
file
=
(
HANDLE
)
_get_osfhandle
(
fd
);
if
(
file
==
INVALID_HANDLE_VALUE
)
{
return
-
1
;
}
cur
=
_lseeki64
(
fd
,
0
,
SEEK_CUR
);
if
(
cur
==
-
1
)
return
-
1
;
if
(
_lseeki64
(
fd
,
(
__int64
)
length
,
SEEK_SET
)
==
-
1
)
return
-
1
;
if
(
!
SetEndOfFile
(
file
))
{
errno
=
EINVAL
;
/* TODO: GetLastError to errno */
return
-
1
;
}
if
(
_lseeki64
(
fd
,
cur
,
SEEK_SET
)
==
-
1
)
return
-
1
;
return
0
;
#endif
/* _WIN32 */
}
static
mrb_value
mrb_file_truncate
(
mrb_state
*
mrb
,
mrb_value
self
)
{
int
fd
;
int64_t
length
;
mrb_value
lenv
;
fd
=
mrb_io_fileno
(
mrb
,
self
);
mrb_get_args
(
mrb
,
"o"
,
&
lenv
);
switch
(
mrb_type
(
lenv
))
{
#ifndef MRB_WITHOUT_FLOAT
case
MRB_TT_FLOAT
:
{
mrb_float
lenf
=
mrb_float
(
lenv
);
if
(
lenf
>
INT64_MAX
)
{
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"length too large"
);
}
length
=
(
int64_t
)
lenf
;
}
break
;
#endif
case
MRB_TT_FIXNUM
:
default:
{
mrb_int
leni
=
mrb_int
(
mrb
,
lenv
);
length
=
(
int64_t
)
leni
;
}
break
;
}
if
(
mrb_ftruncate
(
fd
,
length
)
!=
0
)
{
mrb_raise
(
mrb
,
E_IO_ERROR
,
"ftruncate failed"
);
}
return
mrb_fixnum_value
(
0
);
}
static
mrb_value
mrb_file_s_symlink
(
mrb_state
*
mrb
,
mrb_value
klass
)
{
...
...
@@ -485,6 +582,8 @@ mrb_init_file(mrb_state *mrb)
mrb_define_method
(
mrb
,
file
,
"flock"
,
mrb_file_flock
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
file
,
"mtime"
,
mrb_file_mtime
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
file
,
"size"
,
mrb_file_size
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
file
,
"truncate"
,
mrb_file_truncate
,
MRB_ARGS_REQ
(
1
));
cnst
=
mrb_define_module_under
(
mrb
,
file
,
"Constants"
);
mrb_define_const
(
mrb
,
cnst
,
"LOCK_SH"
,
mrb_fixnum_value
(
LOCK_SH
));
...
...
mrbgems/mruby-io/src/io.c
View file @
874797f3
...
...
@@ -281,7 +281,7 @@ option_to_fd(mrb_state *mrb, mrb_value hash, const char *key)
switch
(
mrb_type
(
opt
))
{
case
MRB_TT_DATA
:
/* IO */
return
(
int
)
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt
)
);
return
mrb_io_fileno
(
mrb
,
opt
);
case
MRB_TT_FIXNUM
:
return
(
int
)
mrb_fixnum
(
opt
);
default:
...
...
@@ -1190,12 +1190,19 @@ retry:
return
result
;
}
mrb_value
int
mrb_io_fileno
(
mrb_state
*
mrb
,
mrb_value
io
)
{
struct
mrb_io
*
fptr
;
fptr
=
io_get_open_fptr
(
mrb
,
io
);
return
mrb_fixnum_value
(
fptr
->
fd
);
return
fptr
->
fd
;
}
static
mrb_value
mrb_io_fileno_m
(
mrb_state
*
mrb
,
mrb_value
io
)
{
int
fd
=
mrb_io_fileno
(
mrb
,
io
);
return
mrb_fixnum_value
(
fd
);
}
mrb_value
...
...
@@ -1332,7 +1339,7 @@ mrb_init_io(mrb_state *mrb)
mrb_define_method
(
mrb
,
io
,
"close_on_exec?"
,
mrb_io_close_on_exec_p
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
io
,
"closed?"
,
mrb_io_closed
,
MRB_ARGS_NONE
());
/* 15.2.20.5.2 */
mrb_define_method
(
mrb
,
io
,
"pid"
,
mrb_io_pid
,
MRB_ARGS_NONE
());
/* 15.2.20.5.2 */
mrb_define_method
(
mrb
,
io
,
"fileno"
,
mrb_io_fileno
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
io
,
"fileno"
,
mrb_io_fileno
_m
,
MRB_ARGS_NONE
());
mrb_define_class_method
(
mrb
,
io
,
"_bufread"
,
io_bufread
,
MRB_ARGS_REQ
(
2
));
}
mrbgems/mruby-io/test/file.rb
View file @
874797f3
...
...
@@ -80,6 +80,22 @@ assert('File#mtime') do
end
end
assert
(
'File#size and File#truncate'
)
do
fname
=
"
#{
$mrbtest_io_wfname
}
.resize"
begin
File
.
open
(
fname
,
'w'
)
do
|
f
|
assert_equal
0
,
f
.
size
assert_equal
0
,
f
.
truncate
(
100
)
assert_equal
100
,
f
.
size
assert_equal
0
,
f
.
pos
assert_equal
0
,
f
.
truncate
(
5
)
assert_equal
5
,
f
.
size
end
ensure
File
.
delete
(
fname
)
end
end
assert
(
'File.join'
)
do
assert_equal
""
,
File
.
join
()
assert_equal
"a"
,
File
.
join
(
"a"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment