code: def copyfile(
      originalfile,
      newfile,
      copy=False,
      create_new=False,
      hashmethod=None,
      use_hardlink=False,
      copy_related_files=True,
  ):
      """Copy or link ``originalfile`` to ``newfile``.
  
      If ``use_hardlink`` is True, and the file can be hard-linked, then a
      link is created, instead of copying the file.
  
      If a hard link is not created and ``copy`` is False, then a symbolic
      link is created.
  
      Parameters
      ----------
      originalfile : str
          full path to original file
      newfile : str
          full path to new file
      copy : Bool
          specifies whether to copy or symlink files
          (default=False) but only for POSIX systems
      use_hardlink : Bool
          specifies whether to hard-link files, when able
          (Default=False), taking precedence over copy
      copy_related_files : Bool
          specifies whether to also operate on related files, as defined in
          ``related_filetype_sets``
  
      Returns
      -------
      None
  
      """
      newhash = None
      orighash = None
      fmlogger.debug(newfile)
  
      if create_new:
          while op.exists(newfile):
              base, fname, ext = split_filename(newfile)
              s = re.search("_c[0-9]{4,4}$", fname)
              i = 0
              if s:
                  i = int(s.group()[2:]) + 1
                  fname = fname[:-6] + "_c%04d" % i
              else:
                  fname += "_c%04d" % i
              newfile = base + os.sep + fname + ext
  
      if hashmethod is None:
          hashmethod = config.get("execution", "hash_method").lower()
  
      # Don't try creating symlinks on CIFS
      if copy is False and on_cifs(newfile):
          copy = True
  
      # Existing file
      # -------------
      # Options:
      #   symlink
      #       to regular file originalfile            (keep if symlinking)
      #       to same dest as symlink originalfile    (keep if symlinking)
      #       to other file                           (unlink)
      #   regular file
      #       hard link to originalfile               (keep)
      #       copy of file (same hash)                (keep)
      #       different file (diff hash)              (unlink)
      keep = False
      if op.lexists(newfile):
          if op.islink(newfile):
              if all(
                  (
                      os.readlink(newfile) == op.realpath(originalfile),
                      not use_hardlink,
                      not copy,
                  )
              ):
                  keep = True
          elif posixpath.samefile(newfile, originalfile):
              keep = True
          else:
              if hashmethod == "timestamp":
                  hashfn = hash_timestamp
              elif hashmethod == "content":
                  hashfn = hash_infile
              else:
                  raise AttributeError(
                      "Unknown hash method found:", hashmethod
                  )
              newhash = hashfn(newfile)
              fmlogger.debug(
                  "File: %s already exists,%s, copy:%d",
                  newfile,
                  newhash,
                  copy,
              )
              orighash = hashfn(originalfile)
              keep = newhash == orighash
          if keep:
              fmlogger.debug(
                  "File: %s already exists, not overwriting, copy:%d",
                  newfile,
                  copy,
              )
          else:
              os.unlink(newfile)
  
      # New file
      # --------
      # use_hardlink & can_hardlink => hardlink
      # ~hardlink & ~copy & can_symlink => symlink
      # ~hardlink & ~symlink => copy
      if not keep and use_hardlink:
          try:
              fmlogger.debug(
                  "Linking File: %s->%s", newfile, originalfile
              )
              # Use realpath to avoid hardlinking symlinks
              os.link(op.realpath(originalfile), newfile)
          except OSError:
              use_hardlink = (
                  False  # Disable hardlink for associated files
              )
          else:
              keep = True
  
      if not keep and not copy and os.name == "posix":
          try:
              fmlogger.debug(
                  "Symlinking File: %s->%s", newfile, originalfile
              )
              os.symlink(originalfile, newfile)
          except OSError:
              copy = True  # Disable symlink for associated files
          else:
              keep = True
  
      if not keep:
          try:
              fmlogger.debug(
                  "Copying File: %s->%s", newfile, originalfile
              )
              shutil.copyfile(originalfile, newfile)
          except shutil.Error as e:
              fmlogger.warning(str(e))
  
      # Associated files
      if copy_related_files:
          related_file_pairs = (
              get_related_files(f, include_this_file=False)
              for f in (originalfile, newfile)
          )
          for alt_ofile, alt_nfile in zip(*related_file_pairs):
              if op.exists(alt_ofile):
                  copyfile(
                      alt_ofile,
                      alt_nfile,
                      copy,
                      hashmethod=hashmethod,
                      use_hardlink=use_hardlink,
                      copy_related_files=False,
                  )
  
      return newfile
  
imports: import sys
  import pickle
  import errno
  import subprocess
  import gzip
  import hashlib
  import locale
  from hashlib import md5
  import os
  import os.path
  import re
  import shutil
  import contextlib
  import posixpath
  from pathlib import Path
  import simplejson
  from time import sleep, time
  from ..None import logging, config, __version__
  from .misc import is_container
label_desc: Use image node_id 11 for calling a global function with example usage: copyfile(originalfile, newfile, copy, create_new, hashmethod, use_hardlink, copy_related_files) and returns: newfile