<commit_before>Module = require 'module'

return if Module.__trojan_source__?
Module.__trojan_source__ = {}

fs = require 'fs'
path = require 'path'

# errors

ENOENT = (filename) ->
  err = new Error("ENOENT, no such file or directory '#{filename}'")
  err.code = 'ENOENT'
  err

EISDIR = (filename) ->
  err = new Error("EISDIR, illegal operation on a directory")
  err.code = 'EISDIR'
  err

ENOTDIR = (filename) ->
  err = new Error("ENOTDIR, not a directory '#{filename}'")
  err.code = 'ENOTDIR'
  err

# helpers

get_file_ref = (filename) ->
  a = Object.keys(Module.__trojan_source__)
  .map (f) ->
    return null unless filename.length > f.length
    return null unless f + '/' is filename.slice(0, f.length + 1)
    {
      root: f
      path: filename.slice(f.length)
    }
  .filter (f) -> f?
  
  return null if a.length is 0
  
  max = a[0].root.length
  b = a[0]
  for i in a
    if i.root.length > max
      max = i.root.length
      b = i
  {
    tree: Module.__trojan_source__[b.root]
    path: b.path
  }

get_file = (filename) ->
  # console.log '------ GET FILE', filename
  o = get_file_ref(filename)
  res = {is_packaged: o?}
  res.path = o.path if o.path?
  res.file = o.tree[o.path] if o?.tree?[o.path]?
  # console.log '------ GET FILE', res
  res

get_encoding = (encoding_or_options) ->
  return null unless encoding_or_options?
  return encoding_or_options if typeof encoding_or_options is 'string'
  return encoding_or_options.encoding if encoding_or_options.encoding?
  null

create_stats = (o) ->
  stat = {}
  ['dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks'].forEach (k) -> stat[k] = o[k]
  ['atime', 'mtime', 'ctime'].forEach (k) -> stat[k] = new Date(o[k])
  ['isFile', 'isDirectory', 'isBlockDevice', 'isCharacterDevice', 'isSymbolicLink', 'isFIFO', 'isSocket'].forEach (k) -> stat[k] = -> o[k]
  stat

# fs implementations

fs_readdir_sync = (old_fn, filename) -><commit_msg>fixed issue in bulkhead<commit_after>Module = require 'module'

return if Module.__trojan_source__?
Module.__trojan_source__ = {}

fs = require 'fs'
path = require 'path'

# errors

ENOENT = (filename) ->
  err = new Error("ENOENT, no such file or directory '#{filename}'")
  err.code = 'ENOENT'
  err

EISDIR = (filename) ->
  err = new Error("EISDIR, illegal operation on a directory")
  err.code = 'EISDIR'
  err

ENOTDIR = (filename) ->
  err = new Error("ENOTDIR, not a directory '#{filename}'")
  err.code = 'ENOTDIR'
  err

# helpers

get_file_ref = (filename) ->
  a = Object.keys(Module.__trojan_source__)
  .map (f) ->
    return null unless filename.length > f.length
    return null unless f + '/' is filename.slice(0, f.length + 1)
    {
      root: f
      path: filename.slice(f.length)
    }
  .filter (f) -> f?
  
  return null if a.length is 0
  
  max = a[0].root.length
  b = a[0]
  for i in a
    if i.root.length > max
      max = i.root.length
      b = i
  {
    tree: Module.__trojan_source__[b.root]
    path: b.path
  }

get_file = (filename) ->
  # console.log '------ GET FILE', filename
  o = get_file_ref(filename)
  res = {is_packaged: o?}
  res.path = o?.path if o?.path?
  res.file = o.tree[o.path] if o?.tree?[o.path]?
  # console.log '------ GET FILE', res
  res

get_encoding = (encoding_or_options) ->
  return null unless encoding_or_options?
  return encoding_or_options if typeof encoding_or_options is 'string'
  return encoding_or_options.encoding if encoding_or_options.encoding?
  null

create_stats = (o) ->
  stat = {}
  ['dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks'].forEach (k) -> stat[k] = o[k]
  ['atime', 'mtime', 'ctime'].forEach (k) -> stat[k] = new Date(o[k])
  ['isFile', 'isDirectory', 'isBlockDevice', 'isCharacterDevice', 'isSymbolicLink', 'isFIFO', 'isSocket'].forEach (k) -> stat[k] = -> o[k]
  stat

# fs implementations

fs_readdir_sync = (old_fn, filename) ->
{'fraction_non_alphanumeric': 0.1633384536610343, 'fraction_numerical': 0.002560163850486431, 'mean_word_length': 3.2839912280701755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1261471', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1442, 'n_words': 375}