# Buggy code
def sanitize_foldername(name):
    """Return foldername with dodgy chars converted to safe ones
    Remove any leading and trailing dot and space characters
    """
    if not name:
        return name

    illegal = CH_ILLEGAL + ':"'
    legal = CH_LEGAL + "-'"

    if sabnzbd.WIN32 or sabnzbd.cfg.sanitize_safe():
        # Remove all bad Windows chars too
        illegal += CH_ILLEGAL_WIN
        legal += CH_LEGAL_WIN

    repl = sabnzbd.cfg.replace_illegal()
    lst = []
    for ch in name.strip():
        if ch in illegal:
            if repl:
                ch = legal[illegal.find(ch)]
                lst.append(ch)
        else:
            lst.append(ch)
    name = "".join(lst)
    name = name.strip()

    if sabnzbd.WIN32 or sabnzbd.cfg.sanitize_safe():
        name = replace_win_devices(name)

    # And finally, make sure it doesn't end in a dot
    if name != "." and name != "..":
        name = name.rstrip(".")
    if not name:
        name = "unknown"

    return name


# Traceback (OSError)
/_UNPACK_[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [English Dub] [FuniDub 1080p x264 AAC] [MKV] [1EB4BBFB] [1+8] - [Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv y)
Traceback (most recent call last):
File "/usr/lib/python3.8/shutil.py", line 788, in move
os.rename(src, real_dst)
OSError: [Errno 36] Filename too long: "/downloads/nzbs/sabnzbd/incomplete/[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [English Dub] [FuniDub 1080p x264 AAC] [MKV] [1EB4BBFB] [1+8] - [Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv y/[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv" -> "/downloads/nzbs/sabnzbd/complete/sonarrenglish/_UNPACK_[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [English Dub] [FuniDub 1080p x264 AAC] [MKV] [1EB4BBFB] [1+8] - [Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv y/[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/app/sabnzbd/filesystem.py", line 655, in move_to_path
renamer(path, new_path)
File "/app/sabnzbd/decorators.py", line 36, in call_func
return f(*args, **kw)
File "/app/sabnzbd/filesystem.py", line 776, in renamer
shutil.move(old, new)
File "/usr/lib/python3.8/shutil.py", line 802, in move
copy_function(src, real_dst)
File "/usr/lib/python3.8/shutil.py", line 432, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.8/shutil.py", line 261, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
OSError: [Errno 36] Filename too long: "/downloads/nzbs/sabnzbd/complete/sonarrenglish/_UNPACK_[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [English Dub] [FuniDub 1080p x264 AAC] [MKV] [1EB4BBFB] [1+8] - [Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv y/[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/app/sabnzbd/filesystem.py", line 576, in create_all_dirs
os.mkdir(path_part_combined)
OSError: [Errno 36] Filename too long: "/downloads/nzbs/sabnzbd/complete/sonarrenglish/_UNPACK_[Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [English Dub] [FuniDub 1080p x264 AAC] [MKV] [1EB4BBFB] [1+8] - [Golumpa] Kino's Journey - The Beautiful World (2017) - 07 (Kino no Tabi) [FuniDub 1080p x264 AAC] [1EB4BBFB].mkv y"

# Fixed code
def sanitize_foldername(name):
    """Return foldername with dodgy chars converted to safe ones
    Remove any leading and trailing dot and space characters
    """
    if not name:
        return name

    illegal = CH_ILLEGAL + ':"'
    legal = CH_LEGAL + "-'"

    if sabnzbd.WIN32 or sabnzbd.cfg.sanitize_safe():
        # Remove all bad Windows chars too
        illegal += CH_ILLEGAL_WIN
        legal += CH_LEGAL_WIN

    repl = sabnzbd.cfg.replace_illegal()
    lst = []
    for ch in name.strip():
        if ch in illegal:
            if repl:
                ch = legal[illegal.find(ch)]
                lst.append(ch)
        else:
            lst.append(ch)
    name = "".join(lst)
    name = name.strip()

    if sabnzbd.WIN32 or sabnzbd.cfg.sanitize_safe():
        name = replace_win_devices(name)

    if len(name) >= sabnzbd.cfg.max_foldername_length():
        name = name[: sabnzbd.cfg.max_foldername_length()]

    # And finally, make sure it doesn't end in a dot
    if name != "." and name != "..":
        name = name.rstrip(".")
    if not name:
        name = "unknown"

    return name