Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL: Load and write history to file #49

Closed

Conversation

renatoalencar
Copy link
Contributor

Use libreadline to load and write command history to file. By default the history is written to `~/.hermes_history' and is created when does not exists.

I was not able to resolve the full path, so I had to check the home directory and append to .hermes_history.

Use libreadline to load and write command history
to file. By default the history is written to
~/.hermes_history and is created when does not
exists.
@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jul 17, 2019
tools/repl/repl.cpp Outdated Show resolved Hide resolved
tools/repl/repl.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@avp avp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be pretty useful, thanks for working on it. Some comments concerning macros and Twines.

tools/repl/repl.cpp Outdated Show resolved Hide resolved
tools/repl/repl.cpp Outdated Show resolved Hide resolved
* Switch from macros to static const variables
  in order to have proper typings;
* Remove explicit Twine instantiation to implicit.
Copy link
Contributor

@avp avp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good to me. Just one minor comment.

tools/repl/repl.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhorowitz has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.


if (!llvm::sys::fs::exists(historyFile)) {
int fd;
auto err = llvm::sys::fs::openFileForWrite(llvm::Twine(historyFile), fd);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm running into some test errors around this code. This creates an empty file, which causes a confusing error from ::read_history():

$ rm ~/.hermes_history
$ ./bin/hermes-repl
Could not load history file: No such file or directory
>>

I did some experimentation, and this seems to be due to the file being empty, and not including the magic version header. If I start with cp /dev/null ~/.hermes_history I get the same warning.

And if I C-d immediately, so that a file is written with no contents, then when I run the next time, I get a different confusing error:

$ cat ~/.hermes_history
_HiStOrY_V2_
$ ./bin/hermes-repl
Could not load history file: Operation not permitted
>>

What's the purpose of creating the empty file here? Can we suppress the empty write, so we don't get a warning?

Finally, I noticed the history file is mode 600, even though my umask is 0022. Shouldn't it be mode 644? Although I see other history files with the same mode, so maybe this is a readline "feature", it's not universal: node's history file is mode 644.

Sorry for all this. The libreadline history support seems unfortunately brittle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes it could happens when you don't have a HOME environment variable set. Besides that, I really don't know what could happens.

The empty file is created in order to write_history to work, it opens the history file only on "append" mode, so if there is no file it returns a ENOENT error.

The LLVM file system API should actually create a 666 mode, but 600 is actually a good choice because of security issues on accessing history files.

I'm reading the libreadline source code in order to understand it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I'm avoiding creating empty files and ignoring ENOENT errors, because it happens in this case and that's an expected behavior.

About the permission, actually there is nothing we could do about it (at least using libreadline), both version 7 and 8 set the file permission hardcoded.

Besides that, let the `write_history` function create
the history file on its own. And when reading the history
ignore ENOENT errors.
@facebook-github-bot
Copy link
Contributor

@renatoalencar has updated the pull request. Re-import the pull request

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willholen has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mhorowitz merged this pull request in f2d9a98.

mganandraj pushed a commit to mganandraj/hermes that referenced this pull request Oct 15, 2019
Summary:
Use libreadline to load and write command history to file. By default the history is written to `~/.hermes_history' and is created when does not exists.

I was not able to resolve the full path, so I had to check the home directory and append to `.hermes_history`.
Pull Request resolved: facebook#49

Test Plan:
run the repl; run some commands; run the repl again.  verify history works.
look at the ~/.hermes_history file

Reviewed By: avp

Differential Revision: D16367773

Pulled By: mhorowitz

fbshipit-source-id: 48b053a1c95d7dcd29709860ff43aab8035b1344
facebook-github-bot pushed a commit that referenced this pull request Feb 24, 2021
Summary:
Document the new semi-automated workflow.

Pull Request resolved: facebookincubator/fbjni#49

Test Plan: _eyes

Reviewed By: nikoant

Differential Revision: D26607139

Pulled By: passy

fbshipit-source-id: 1f335b6b254bd5053c8f63ab872f55254207295a
@whatleo whatleo mentioned this pull request Apr 1, 2021
mganandraj pushed a commit to mganandraj/hermes that referenced this pull request Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants