Skip to content

Commit 6f35792

Browse files
committed
Split out io_fd_t
1 parent f1b375b commit 6f35792

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

exec.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,6 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t &out_chain, s
406406
*/
407407
case IO_FILE:
408408
{
409-
out.reset(new io_data_t());
410-
out->fd = in->fd;
411-
out->io_mode = IO_FD;
412-
out->param2.close_old = 1;
413-
414409
int fd;
415410
if ((fd=open(in->filename_cstr, in->param2.flags, OPEN_MASK))==-1)
416411
{
@@ -424,7 +419,7 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t &out_chain, s
424419
}
425420

426421
opened_fds.push_back(fd);
427-
out->param1.old_fd = fd;
422+
out.reset(new io_fd_t(in->fd, fd, true));
428423

429424
break;
430425
}
@@ -852,7 +847,8 @@ void exec(parser_t &parser, job_t *j)
852847

853848
case IO_FD:
854849
{
855-
builtin_stdin = in->param1.old_fd;
850+
CAST_INIT(const io_fd_t *, in_fd, in.get());
851+
builtin_stdin = in_fd->old_fd;
856852
break;
857853
}
858854
case IO_PIPE:

io.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ void io_data_t::print() const
6161
case IO_PIPE:
6262
fprintf(stderr, "pipe {%d, %d}\n", param1.pipe_fd[0], param1.pipe_fd[1]);
6363
break;
64-
case IO_FD:
65-
fprintf(stderr, "FD map %d -> %d\n", param1.old_fd, fd);
66-
break;
6764
case IO_BUFFER:
6865
fprintf(stderr, "buffer %p (size %lu)\n", out_buffer_ptr(), out_buffer_size());
6966
break;
@@ -75,6 +72,11 @@ void io_close_t::print() const
7572
fprintf(stderr, "close %d\n", fd);
7673
}
7774

75+
void io_fd_t::print() const
76+
{
77+
fprintf(stderr, "FD map %d -> %d\n", old_fd, fd);
78+
}
79+
7880
void io_buffer_read(io_data_t *d)
7981
{
8082
exec_close(d->param1.pipe_fd[1]);

io.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class io_data_t
3737
{
3838
/** Fds for IO_PIPE and for IO_BUFFER */
3939
int pipe_fd[2];
40-
/** fd to redirect specified fd to, for IO_FD */
41-
int old_fd;
4240
} param1;
4341

4442

@@ -47,8 +45,6 @@ class io_data_t
4745
{
4846
/** file creation flags to send to open for IO_FILE */
4947
int flags;
50-
/** Whether to close old_fd for IO_FD */
51-
int close_old;
5248
} param2;
5349

5450
/** Filename IO_FILE. malloc'd. This needs to be used after fork, so don't use wcstring here. */
@@ -127,6 +123,24 @@ class io_close_t : public io_data_t
127123
virtual void print() const;
128124
};
129125

126+
class io_fd_t : public io_data_t
127+
{
128+
public:
129+
/** fd to redirect specified fd to */
130+
int old_fd;
131+
/** Whether to close old_fd */
132+
int close_old;
133+
134+
virtual void print() const;
135+
136+
io_fd_t(int f, int old, bool close = false) :
137+
io_data_t(IO_FD, f),
138+
old_fd(old),
139+
close_old(close)
140+
{
141+
}
142+
};
143+
130144
class io_chain_t : public std::vector<shared_ptr<io_data_t> >
131145
{
132146
public:

parser.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -1607,13 +1607,11 @@ void parser_t::parse_job_argument_list(process_t *p,
16071607
{
16081608
wchar_t *end;
16091609

1610-
new_io.reset(new io_data_t(IO_FD, fd));
16111610
errno = 0;
16121611

1613-
new_io->param1.old_fd = fish_wcstoi(target.c_str(), &end, 10);
1612+
int old_fd = fish_wcstoi(target.c_str(), &end, 10);
16141613

1615-
if ((new_io->param1.old_fd < 0) ||
1616-
errno || *end)
1614+
if (old_fd < 0 || errno || *end)
16171615
{
16181616
error(SYNTAX_ERROR,
16191617
tok_get_pos(tok),
@@ -1622,6 +1620,10 @@ void parser_t::parse_job_argument_list(process_t *p,
16221620

16231621
tok_next(tok);
16241622
}
1623+
else
1624+
{
1625+
new_io.reset(new io_fd_t(fd, old_fd));
1626+
}
16251627
}
16261628
break;
16271629
}

postfork.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static int handle_child_io(io_chain_t &io_chain)
169169
io_data_t *io = io_chain.at(idx).get();
170170
int tmp;
171171

172-
if (io->io_mode == IO_FD && io->fd == io->param1.old_fd)
172+
if (io->io_mode == IO_FD && io->fd == static_cast<io_fd_t*>(io)->old_fd)
173173
{
174174
continue;
175175
}
@@ -232,7 +232,7 @@ static int handle_child_io(io_chain_t &io_chain)
232232
*/
233233
close(io->fd);
234234

235-
if (dup2(io->param1.old_fd, io->fd) == -1)
235+
if (dup2(static_cast<const io_fd_t *>(io)->old_fd, io->fd) == -1)
236236
{
237237
debug_safe_int(1, FD_ERROR, io->fd);
238238
safe_perror("dup2");
@@ -443,9 +443,11 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil
443443
{
444444
shared_ptr<const io_data_t> io = j->io.at(idx);
445445

446-
if (io->io_mode == IO_FD && io->fd == io->param1.old_fd)
446+
if (io->io_mode == IO_FD)
447447
{
448-
continue;
448+
CAST_INIT(const io_fd_t *, io_fd, io.get());
449+
if (io->fd == io_fd->old_fd)
450+
continue;
449451
}
450452

451453
if (io->fd > 2)
@@ -472,8 +474,9 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil
472474

473475
case IO_FD:
474476
{
477+
CAST_INIT(const io_fd_t *, io_fd, io.get());
475478
if (! err)
476-
err = posix_spawn_file_actions_adddup2(actions, io->param1.old_fd /* from */, io->fd /* to */);
479+
err = posix_spawn_file_actions_adddup2(actions, io_fd->old_fd /* from */, io->fd /* to */);
477480
break;
478481
}
479482

0 commit comments

Comments
 (0)