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

Improve portability of SystemError #10726

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/crystal/system/unix/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module Crystal::System::File
end
end

raise ::File::Error.from_errno("Cannot read link", Errno::ENAMETOOLONG, file: path)
raise ::File::Error.from_os_error("Cannot read link", Errno::ENAMETOOLONG, file: path)
end

def self.rename(old_filename, new_filename)
Expand Down
8 changes: 4 additions & 4 deletions src/crystal/system/unix/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Crystal::System::Process

def self.pgid(pid)
# Disallow users from depending on ppid(0) instead of `pgid`
raise RuntimeError.from_errno("getpgid", Errno::EINVAL) if pid == 0
raise RuntimeError.from_os_error("getpgid", Errno::EINVAL) if pid == 0

ret = LibC.getpgid(pid)
raise RuntimeError.from_errno("getpgid") if ret < 0
Expand Down Expand Up @@ -107,7 +107,7 @@ struct Crystal::System::Process
# error:
errno = Errno.value
LibC.pthread_sigmask(LibC::SIG_SETMASK, pointerof(oldmask), nil)
raise RuntimeError.from_errno("fork", errno)
raise RuntimeError.from_os_error("fork", errno)
else
# parent:
LibC.pthread_sigmask(LibC::SIG_SETMASK, pointerof(oldmask), nil)
Expand Down Expand Up @@ -220,9 +220,9 @@ struct Crystal::System::Process
private def self.raise_exception_from_errno(command, errno = Errno.value)
case errno
when Errno::EACCES, Errno::ENOENT
raise ::File::Error.from_errno("Error executing process", errno, file: command)
raise ::File::Error.from_os_error("Error executing process", errno, file: command)
else
raise IO::Error.from_errno("Error executing process: '#{command}'", errno)
raise IO::Error.from_os_error("Error executing process: '#{command}'", errno)
end
end

Expand Down
14 changes: 7 additions & 7 deletions src/crystal/system/unix/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Thread
}, self.as(Void*))

if ret != 0
raise RuntimeError.from_errno("pthread_create", Errno.new(ret))
raise RuntimeError.from_os_error("pthread_create", Errno.new(ret))
end
end

Expand Down Expand Up @@ -66,7 +66,7 @@ class Thread

@@current_key = begin
ret = LibC.pthread_key_create(out current_key, nil)
raise RuntimeError.from_errno("pthread_key_create", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_key_create", Errno.new(ret)) unless ret == 0
current_key
end

Expand All @@ -84,7 +84,7 @@ class Thread
# Associates the Thread object to the running system thread.
protected def self.current=(thread : Thread) : Thread
ret = LibC.pthread_setspecific(@@current_key, thread.as(Void*))
raise RuntimeError.from_errno("pthread_setspecific", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_setspecific", Errno.new(ret)) unless ret == 0
thread
end
{% else %}
Expand Down Expand Up @@ -144,23 +144,23 @@ class Thread
ret = LibC.pthread_attr_init(out attr)
unless ret == 0
LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_errno("pthread_attr_init", Errno.new(ret))
raise RuntimeError.from_os_error("pthread_attr_init", Errno.new(ret))
end

if LibC.pthread_attr_get_np(@th, pointerof(attr)) == 0
LibC.pthread_attr_getstack(pointerof(attr), pointerof(address), out _)
end
ret = LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_errno("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
{% elsif flag?(:linux) %}
if LibC.pthread_getattr_np(@th, out attr) == 0
LibC.pthread_attr_getstack(pointerof(attr), pointerof(address), out _)
end
ret = LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_errno("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
{% elsif flag?(:openbsd) %}
ret = LibC.pthread_stackseg_np(@th, out stack)
raise RuntimeError.from_errno("pthread_stackseg_np", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_stackseg_np", Errno.new(ret)) unless ret == 0

address =
if LibC.pthread_main_np == 1
Expand Down
12 changes: 6 additions & 6 deletions src/crystal/system/unix/pthread_condition_variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ class Thread
{% end %}

ret = LibC.pthread_cond_init(out @cond, pointerof(attributes))
raise RuntimeError.from_errno("pthread_cond_init", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_init", Errno.new(ret)) unless ret == 0

LibC.pthread_condattr_destroy(pointerof(attributes))
end

def signal
ret = LibC.pthread_cond_signal(self)
raise RuntimeError.from_errno("pthread_cond_signal", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_signal", Errno.new(ret)) unless ret == 0
end

def broadcast
ret = LibC.pthread_cond_broadcast(self)
raise RuntimeError.from_errno("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
end

def wait(mutex : Thread::Mutex)
ret = LibC.pthread_cond_wait(self, mutex)
raise RuntimeError.from_errno("pthread_cond_wait", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_wait", Errno.new(ret)) unless ret == 0
end

def wait(mutex : Thread::Mutex, time : Time::Span)
Expand Down Expand Up @@ -60,13 +60,13 @@ class Thread
when Errno::ETIMEDOUT
yield
else
raise RuntimeError.from_errno("pthread_cond_timedwait", errno)
raise RuntimeError.from_os_error("pthread_cond_timedwait", errno)
end
end

def finalize
ret = LibC.pthread_cond_destroy(self)
raise RuntimeError.from_errno("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
end

def to_unsafe
Expand Down
10 changes: 5 additions & 5 deletions src/crystal/system/unix/pthread_mutex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Thread
LibC.pthread_mutexattr_settype(pointerof(attributes), LibC::PTHREAD_MUTEX_ERRORCHECK)

ret = LibC.pthread_mutex_init(out @mutex, pointerof(attributes))
raise RuntimeError.from_errno("pthread_mutex_init", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_init", Errno.new(ret)) unless ret == 0

LibC.pthread_mutexattr_destroy(pointerof(attributes))
end

def lock
ret = LibC.pthread_mutex_lock(self)
raise RuntimeError.from_errno("pthread_mutex_lock", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_lock", Errno.new(ret)) unless ret == 0
end

def try_lock
Expand All @@ -27,13 +27,13 @@ class Thread
when Errno::EBUSY
false
else
raise RuntimeError.from_errno("pthread_mutex_trylock", ret)
raise RuntimeError.from_os_error("pthread_mutex_trylock", ret)
end
end

def unlock
ret = LibC.pthread_mutex_unlock(self)
raise RuntimeError.from_errno("pthread_mutex_unlock", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_unlock", Errno.new(ret)) unless ret == 0
end

def synchronize
Expand All @@ -45,7 +45,7 @@ class Thread

def finalize
ret = LibC.pthread_mutex_destroy(self)
raise RuntimeError.from_errno("pthread_mutex_destroy", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_destroy", Errno.new(ret)) unless ret == 0
end

def to_unsafe
Expand Down
6 changes: 3 additions & 3 deletions src/crystal/system/win32/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Crystal::System::Dir

def self.open(path : String) : DirHandle
unless ::Dir.exists? path
raise ::File::Error.from_errno("Error opening directory", Errno::ENOENT, file: path)
raise ::File::Error.from_os_error("Error opening directory", Errno::ENOENT, file: path)
end

DirHandle.new(LibC::INVALID_HANDLE_VALUE, to_windows_path(path + "\\*"))
Expand All @@ -31,7 +31,7 @@ module Crystal::System::Dir
if error == WinError::ERROR_FILE_NOT_FOUND
return nil
else
raise ::File::Error.from_winerror("Error reading directory entries", error, file: path)
raise ::File::Error.from_os_error("Error reading directory entries", error, file: path)
end
end
else
Expand All @@ -43,7 +43,7 @@ module Crystal::System::Dir
if error == WinError::ERROR_NO_MORE_FILES
return nil
else
raise ::File::Error.from_winerror("Error reading directory entries", error, file: path)
raise ::File::Error.from_os_error("Error reading directory entries", error, file: path)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module Crystal::System::Env
when WinError::ERROR_ENVVAR_NOT_FOUND
return
else
raise RuntimeError.from_winerror("GetEnvironmentVariableW", last_error)
raise RuntimeError.from_os_error("GetEnvironmentVariableW", last_error)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module Crystal::System::File
if NOT_FOUND_ERRORS.includes? error
return nil
else
raise ::File::Error.from_winerror(message, error, file: path)
raise ::File::Error.from_os_error(message, error, file: path)
end
end

Expand Down Expand Up @@ -179,7 +179,7 @@ module Crystal::System::File
end

unless exists? real_path
raise ::File::Error.from_errno("Error resolving real path", Errno::ENOENT, file: path)
raise ::File::Error.from_os_error("Error resolving real path", Errno::ENOENT, file: path)
end

real_path
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module Crystal::System::FileDescriptor

if file_type == LibC::FILE_TYPE_UNKNOWN
error = WinError.value
raise IO::Error.from_winerror("Unable to get info", error) unless error == WinError::ERROR_SUCCESS
raise IO::Error.from_os_error("Unable to get info", error) unless error == WinError::ERROR_SUCCESS
end

if file_type == LibC::FILE_TYPE_DISK
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct Crystal::System::Process
error = WinError.value
case error.to_errno
when Errno::EACCES, Errno::ENOENT
raise ::File::Error.from_winerror("Error executing process", error, file: command_args)
raise ::File::Error.from_os_error("Error executing process", error, file: command_args)
else
raise IO::Error.from_winerror("Error executing process: '#{command_args}'", error)
end
Expand Down
4 changes: 2 additions & 2 deletions src/errno.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum Errno
Errno.new LibC.__error.value
{% elsif flag?(:win32) %}
ret = LibC._get_errno(out errno)
raise RuntimeError.from_errno("_get_errno", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("_get_errno", Errno.new(ret)) unless ret == 0
Errno.new errno
{% end %}
end
Expand All @@ -60,7 +60,7 @@ enum Errno
LibC.__error.value = errno.value
{% elsif flag?(:win32) %}
ret = LibC._set_errno(errno.value)
raise RuntimeError.from_errno("_set_errno", ret) unless ret == 0
raise RuntimeError.from_os_error("_set_errno", ret) unless ret == 0
{% end %}
errno
end
Expand Down
12 changes: 6 additions & 6 deletions src/file/error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ class File::Error < IO::Error
getter file : String
getter other : String?

private def self.new_from_errno(message, errno, **opts)
case errno
when Errno::ENOENT
private def self.new_from_os_error(message, os_error, **opts)
case os_error
when Errno::ENOENT, WinError::ERROR_FILE_NOT_FOUND, WinError::ERROR_PATH_NOT_FOUND
File::NotFoundError.new(message, **opts)
when Errno::EEXIST
when Errno::EEXIST, WinError::ERROR_ALREADY_EXISTS
File::AlreadyExistsError.new(message, **opts)
when Errno::EACCES
when Errno::EACCES, WinError::ERROR_PRIVILEGE_NOT_HELD
File::AccessDeniedError.new(message, **opts)
else
super message, errno, **opts
super message, os_error, **opts
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/gc/boehm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module GC
# :nodoc:
def self.pthread_join(thread : LibC::PthreadT) : Void*
ret = LibGC.pthread_join(thread, out value)
raise RuntimeError.from_errno("pthread_join", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_join", Errno.new(ret)) unless ret == 0
value
end

Expand Down
8 changes: 4 additions & 4 deletions src/socket/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Socket
wsa_version = 0x202
err = LibC.WSAStartup(wsa_version, out wsadata)
unless err.zero?
raise IO::Error.from_winerror("WSAStartup", WinError.new(err.to_u32))
raise IO::Error.from_os_error("WSAStartup", WinError.new(err.to_u32))
end

if wsadata.wVersion != wsa_version
Expand Down Expand Up @@ -53,14 +53,14 @@ class Socket
end

class Error < IO::Error
private def self.new_from_errno(message, errno, **opts)
case errno
private def self.new_from_os_error(message, os_error, **opts)
case os_error
when Errno::ECONNREFUSED
Socket::ConnectError.new(message, **opts)
when Errno::EADDRINUSE
Socket::BindError.new(message, **opts)
else
super message, errno, **opts
super message, os_error, **opts
end
end
end
Expand Down
Loading