Strange exception throw - assign: Operation not permitted
Boost.Asio's POSIX stream-oriented descriptors explicitly do not support regular files. Hence, if test
is a regular file, then ./client < test
will result in posix::stream_descriptor::assign()
failing when attempting to assign STDIN_FILENO
to the stream_descriptor
. The documentation states:
Boost.Asio includes classes added to permit synchronous and asynchronous read and write operations to be performed on POSIX file descriptors, such as pipes, standard input and output, and various devices (but not regular files).
Consider passing the contents of the test
file to client
through a pipe.
$ cat test | ./client
Here is a complete example program and demonstration:
#include <iostream>
#include <boost/asio.hpp>
void handle_read(
const boost::system::error_code& error,
std::size_t bytes_transferred
)
{
std::cout << "read " << bytes_transferred << " bytes with "
<< error.message() << std::endl;
}
int main()
{
boost::asio::io_service io_service;
boost::asio::posix::stream_descriptor input(io_service);
// Assign STDIN_FILENO to the stream_descriptor. It will support
// pipes, standard input and output, and various devices, but NOT
// regular files.
boost::system::error_code error;
input.assign(STDIN_FILENO, error);
if (error)
{
std::cerr << error.message() << std::endl;
return -1;
}
boost::asio::streambuf input_buffer;
async_read_until(input, input_buffer, '\n', &handle_read);
io_service.run();
}
Demonstration
$ ./client
testing standard inputenter
read 23 bytes with Success
$ echo "this is a test" > test
$ ./client < test
Operation not permitted
$ cat test | ./client
read 15 bytes with Success
How to fix Operation not permitted in http_listener
I found the problem. My app is sandbox app so the folder of app data is ~/Library/Container/.. and it's not allowed to write to that folder. Changing the folder solves problem.
Why am I getting Operation not permitted?
The file permissions need to be changed.
When I ran the code on my system it didn't throw an exception until I changed the file permissions to block access. The error message was different though ("Access is denied"), though that is probably not very consequential here.
stdin pipe not closing when read with Boost.ASIO
You could catch the exception from async_read_until
size_t bytes = 0;
bool eof = false;
try {
bytes = boost::asio::async_read_until(as_stdin, buffer, '\n', yield);
} catch(std::exception const& e) {
std::cerr << "Exception: " << e.what() << "\n";
bytes = 0;
eof = true;
}
// ...
if (eof) break;
Or use the error_code:
boost::system::error_code ec;
auto bytes = boost::asio::async_read_until(as_stdin, buffer, '\n', yield[ec]);
// ...
if (ec) {
std::cerr << "Error: " << ec.message() << "\n";
break;
}
Output is very similar in both cases
Exception: End of file
No bytes read
Done
Or
No bytes read
Error: End of file
Done
Limitations
Regular files cannot be used with POSIX stream_descriptor, see https://stackoverflow.com/a/23631715/85371
Not Permitted to Use File Not Found Exception, but it is necessary
This line,
File trying = new File(response);
even if, like you can see below written in the JavaDoc, will not throw any exception should preferrably (but that is not mandatory) be contained within your try...catch
and using a FileInputStream
(which should be contained inside the try block) because it is the new FileInputStream()
constructor which is throwing an exception. Remove the use of Scanner
also when trying to read a file efficiently.
Here is a useful link :
- Best way to read a text file in Java
Javadoc
public File(String pathname)
Creates a new File instance by converting
the given pathname string into an abstract pathname. If the given
string is the empty string, then the result is the empty abstract
pathname.
That means that it does not check if the file exists or not.
Solution
FileInputStream fis = null;
try{
fis = new FileInputStream(new File(enter.nextLine()));
error = false;
} catch (FileNotFoundException e){
// Blabla
}
Error: EPERM: operation not permitted, unlink 'D:\Sources\**\node_modules\fsevents\node_modules\abbrev\package.json'
It is an npm 5.4.0 issue https://github.com/npm/npm/issues/18287
Workarounds are
- downgrade to 5.3
- try running with --no-optional, i.e.
npm install --no-optional
Related Topics
C++ Access Violation Reading Location 0Xcdcdcdcd Error on Calling a Function
Is Using an Union in Place of a Cast Well Defined
How to Remove Duplicate Values from a List in C++
How to Create Temporary Object in C++
How to Store a Reference of a Singleton Class
How to Know the Right Max Size of Vector? Max_Size()? But No
How to Print Out the Size of a C++ Class at Compile-Time
C++11 Cross Compiler/Standard Library Random Distribution Reproducibility
Convert Hexadecimal String with Leading "0X" to Signed Short in C++
Is "Long Long" = "Long Long Int" = "Long Int Long" = "Int Long Long"
Correct Way to Define C++ Namespace Methods in .Cpp File
How to Declare Std::Unique_Ptr and What Is the Use of It
Strange Exception Throw - Assign: Operation Not Permitted
What Are the Rules Governing C++ Single and Double Precision Mixed Calculations