mirror of
https://github.com/9001/copyparty.git
synced 2025-08-18 01:22:13 -06:00
replace magic numbers with errno.*
This commit is contained in:
parent
046b494b53
commit
20c6b82bec
|
@ -5,6 +5,7 @@ import argparse # typechk
|
||||||
import base64
|
import base64
|
||||||
import calendar
|
import calendar
|
||||||
import copy
|
import copy
|
||||||
|
import errno
|
||||||
import gzip
|
import gzip
|
||||||
import itertools
|
import itertools
|
||||||
import json
|
import json
|
||||||
|
@ -1290,10 +1291,10 @@ class HttpCli(object):
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
self.log("makedirs failed [{}]".format(dst))
|
self.log("makedirs failed [{}]".format(dst))
|
||||||
if not bos.path.isdir(dst):
|
if not bos.path.isdir(dst):
|
||||||
if ex.errno == 13:
|
if ex.errno == errno.EACCES:
|
||||||
raise Pebkac(500, "the server OS denied write-access")
|
raise Pebkac(500, "the server OS denied write-access")
|
||||||
|
|
||||||
if ex.errno == 17:
|
if ex.errno == errno.EEXIST:
|
||||||
raise Pebkac(400, "some file got your folder name")
|
raise Pebkac(400, "some file got your folder name")
|
||||||
|
|
||||||
raise Pebkac(500, min_ex())
|
raise Pebkac(500, min_ex())
|
||||||
|
@ -1544,7 +1545,7 @@ class HttpCli(object):
|
||||||
try:
|
try:
|
||||||
bos.mkdir(fn)
|
bos.mkdir(fn)
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
if ex.errno == 13:
|
if ex.errno == errno.EACCES:
|
||||||
raise Pebkac(500, "the server OS denied write-access")
|
raise Pebkac(500, "the server OS denied write-access")
|
||||||
|
|
||||||
raise Pebkac(500, "mkdir failed:\n" + min_ex())
|
raise Pebkac(500, "mkdir failed:\n" + min_ex())
|
||||||
|
@ -1862,7 +1863,7 @@ class HttpCli(object):
|
||||||
srv_lastmod = st.st_mtime
|
srv_lastmod = st.st_mtime
|
||||||
srv_lastmod3 = int(srv_lastmod * 1000)
|
srv_lastmod3 = int(srv_lastmod * 1000)
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
if ex.errno != 2:
|
if ex.errno != errno.ENOENT:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# if file exists, chekc that timestamp matches the client's
|
# if file exists, chekc that timestamp matches the client's
|
||||||
|
|
|
@ -35,6 +35,7 @@ from .util import (
|
||||||
FHC,
|
FHC,
|
||||||
Garda,
|
Garda,
|
||||||
Magician,
|
Magician,
|
||||||
|
E_SCK,
|
||||||
min_ex,
|
min_ex,
|
||||||
shut_socket,
|
shut_socket,
|
||||||
spack,
|
spack,
|
||||||
|
@ -332,7 +333,7 @@ class HttpSrv(object):
|
||||||
cli.run()
|
cli.run()
|
||||||
|
|
||||||
except (OSError, socket.error) as ex:
|
except (OSError, socket.error) as ex:
|
||||||
if ex.errno not in [10038, 10054, 107, 57, 49, 9]:
|
if ex.errno not in E_SCK:
|
||||||
self.log(
|
self.log(
|
||||||
"%s %s" % addr,
|
"%s %s" % addr,
|
||||||
"run({}): {}".format(fno, ex),
|
"run({}): {}".format(fno, ex),
|
||||||
|
@ -354,13 +355,7 @@ class HttpSrv(object):
|
||||||
"shut({}): {}".format(fno, ex),
|
"shut({}): {}".format(fno, ex),
|
||||||
c="1;30",
|
c="1;30",
|
||||||
)
|
)
|
||||||
if ex.errno not in [10038, 10054, 107, 57, 49, 9]:
|
if ex.errno not in E_SCK:
|
||||||
# 10038 No longer considered a socket
|
|
||||||
# 10054 Foribly closed by remote
|
|
||||||
# 107 Transport endpoint not connected
|
|
||||||
# 57 Socket is not connected
|
|
||||||
# 49 Can't assign requested address (wifi down)
|
|
||||||
# 9 Bad file descriptor
|
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
with self.mutex:
|
with self.mutex:
|
||||||
|
|
|
@ -8,7 +8,15 @@ import sys
|
||||||
|
|
||||||
from .__init__ import ANYWIN, MACOS, PY2, TYPE_CHECKING, VT100, unicode
|
from .__init__ import ANYWIN, MACOS, PY2, TYPE_CHECKING, VT100, unicode
|
||||||
from .stolen.qrcodegen import QrCode
|
from .stolen.qrcodegen import QrCode
|
||||||
from .util import chkcmd, sunpack, termsize
|
from .util import (
|
||||||
|
E_ACCESS,
|
||||||
|
E_ADDR_IN_USE,
|
||||||
|
E_ADDR_NOT_AVAIL,
|
||||||
|
E_UNREACH,
|
||||||
|
chkcmd,
|
||||||
|
sunpack,
|
||||||
|
termsize,
|
||||||
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .svchub import SvcHub
|
from .svchub import SvcHub
|
||||||
|
@ -133,9 +141,9 @@ class TcpSrv(object):
|
||||||
srv.bind((ip, port))
|
srv.bind((ip, port))
|
||||||
self.srv.append(srv)
|
self.srv.append(srv)
|
||||||
except (OSError, socket.error) as ex:
|
except (OSError, socket.error) as ex:
|
||||||
if ex.errno in [98, 48]:
|
if ex.errno in E_ADDR_IN_USE:
|
||||||
e = "\033[1;31mport {} is busy on interface {}\033[0m".format(port, ip)
|
e = "\033[1;31mport {} is busy on interface {}\033[0m".format(port, ip)
|
||||||
elif ex.errno in [99, 49]:
|
elif ex.errno in E_ADDR_NOT_AVAIL:
|
||||||
e = "\033[1;31minterface {} does not exist\033[0m".format(ip)
|
e = "\033[1;31minterface {} does not exist\033[0m".format(ip)
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
@ -321,9 +329,9 @@ class TcpSrv(object):
|
||||||
default_route = s.getsockname()[0]
|
default_route = s.getsockname()[0]
|
||||||
break
|
break
|
||||||
except (OSError, socket.error) as ex:
|
except (OSError, socket.error) as ex:
|
||||||
if ex.errno == 13:
|
if ex.errno in E_ACCESS:
|
||||||
self.log("tcpsrv", "eaccess {} (trying next)".format(ip))
|
self.log("tcpsrv", "eaccess {} (trying next)".format(ip))
|
||||||
elif ex.errno not in [101, 10065, 10051]:
|
elif ex.errno not in E_UNREACH:
|
||||||
self.log("tcpsrv", "route lookup failed; err {}".format(ex.errno))
|
self.log("tcpsrv", "route lookup failed; err {}".format(ex.errno))
|
||||||
|
|
||||||
s.close()
|
s.close()
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import print_function, unicode_literals
|
from __future__ import print_function, unicode_literals
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
import errno
|
||||||
import gzip
|
import gzip
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
@ -2670,7 +2671,7 @@ class Up2k(object):
|
||||||
try:
|
try:
|
||||||
atomic_move(sabs, dabs)
|
atomic_move(sabs, dabs)
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
if ex.errno != 18:
|
if ex.errno != errno.EXDEV:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
self.log("cross-device move:\n {}\n {}".format(sabs, dabs))
|
self.log("cross-device move:\n {}\n {}".format(sabs, dabs))
|
||||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import print_function, unicode_literals
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import errno
|
||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import math
|
import math
|
||||||
|
@ -29,6 +30,28 @@ from .__init__ import ANYWIN, MACOS, PY2, TYPE_CHECKING, VT100, WINDOWS
|
||||||
from .__version__ import S_BUILD_DT, S_VERSION
|
from .__version__ import S_BUILD_DT, S_VERSION
|
||||||
from .stolen import surrogateescape
|
from .stolen import surrogateescape
|
||||||
|
|
||||||
|
|
||||||
|
def _ens(want: str) -> tuple[int, ...]:
|
||||||
|
ret: list[int] = []
|
||||||
|
for v in want.split():
|
||||||
|
try:
|
||||||
|
ret.append(getattr(errno, v))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
|
||||||
|
# WSAECONNRESET - foribly closed by remote
|
||||||
|
# WSAENOTSOCK - no longer a socket
|
||||||
|
# EUNATCH - can't assign requested address (wifi down)
|
||||||
|
E_SCK = _ens("ENOTCONN EUNATCH EBADF WSAENOTSOCK WSAECONNRESET")
|
||||||
|
E_ADDR_NOT_AVAIL = _ens("EADDRNOTAVAIL WSAEADDRNOTAVAIL")
|
||||||
|
E_ADDR_IN_USE = _ens("EADDRINUSE WSAEADDRINUSE")
|
||||||
|
E_ACCESS = _ens("EACCES WSAEACCES")
|
||||||
|
E_UNREACH = _ens("EHOSTUNREACH WSAEHOSTUNREACH ENETUNREACH WSAENETUNREACH")
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import ctypes
|
import ctypes
|
||||||
import fcntl
|
import fcntl
|
||||||
|
@ -1020,7 +1043,7 @@ def ren_open(
|
||||||
except OSError as ex_:
|
except OSError as ex_:
|
||||||
ex = ex_
|
ex = ex_
|
||||||
|
|
||||||
if ex.errno == 22 and not asciified:
|
if ex.errno == errno.EINVAL and not asciified:
|
||||||
asciified = True
|
asciified = True
|
||||||
bname, fname = [
|
bname, fname = [
|
||||||
zs.encode("ascii", "replace").decode("ascii").replace("?", "_")
|
zs.encode("ascii", "replace").decode("ascii").replace("?", "_")
|
||||||
|
@ -1028,7 +1051,10 @@ def ren_open(
|
||||||
]
|
]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if ex.errno not in [36, 63, 95] and (not WINDOWS or ex.errno != 22):
|
# ENOTSUP: zfs on ubuntu 20.04
|
||||||
|
if ex.errno not in (errno.ENAMETOOLONG, errno.ENOSR, errno.ENOTSUP) and (
|
||||||
|
not WINDOWS or ex.errno != errno.EINVAL
|
||||||
|
):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if not b64:
|
if not b64:
|
||||||
|
@ -1884,8 +1910,8 @@ def sendfile_kern(
|
||||||
stuck = 0
|
stuck = 0
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
d = time.time() - stuck
|
d = time.time() - stuck
|
||||||
log("sendfile stuck for {:.3f} sec: {!r}".format(d, ex))
|
log("sendfile stuck for {:.3f} sec: {!r}".format(d, ex), "90")
|
||||||
if d < 3600 and ex.errno == 11: # eagain
|
if d < 3600 and ex.errno == errno.EWOULDBLOCK:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
|
|
Loading…
Reference in a new issue