storage-local.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. -- ===================================================================
  2. -- /storage-local.lua
  3. -- Persistent storage in /data using Serpent Serializer
  4. -- Unix systems only
  5. -- ===================================================================
  6. -- Set directories and filenames
  7. modpol.datadir = modpol.topdir .. "/data"
  8. modpol.file_ledger = modpol.datadir .. "/ledger.dat"
  9. modpol.file_orgs = modpol.datadir .. "/orgs.dat"
  10. os.execute ("mkdir -p " .. modpol.datadir)
  11. modpol.ocutil.setlogdir (modpol.datadir)
  12. modpol.ocutil.setlogname ("modpol.log")
  13. -- ===================================================================
  14. -- Set up the Serpent Serializer functions.
  15. modpol.serpent = {}
  16. dofile (modpol.topdir .. "/util/serpent/serpent.lua")
  17. -- ===================================================================
  18. -- This function stores "ledger" data to disk.
  19. local store_ledger = function()
  20. local ok = modpol.ocutil.file_write (modpol.file_ledger,
  21. modpol.serpent.dump (modpol.ledger))
  22. if ok ~= true then
  23. modpol.ocutil.fatal_error ("store_data: ledger")
  24. end
  25. local nn = modpol.ocutil.table_length (modpol.ledger)
  26. local str = "entries"
  27. if nn == 1 then str = "entry" end
  28. modpol.ocutil.log (nn .. " global ledger entries stored to disk")
  29. end
  30. -- ===================================================================
  31. -- This function stores "orgs" data to disk.
  32. local store_orgs = function()
  33. local ok = modpol.ocutil.file_write (modpol.file_orgs,
  34. modpol.serpent.dump (modpol.orgs))
  35. if ok ~= true then
  36. modpol.ocutil.fatal_error ("store_data: orgs")
  37. end
  38. local nn = modpol.ocutil.table_length (modpol.orgs)
  39. local str = "entries"
  40. if nn == 1 then str = "entry" end
  41. modpol.ocutil.log (nn .. " orgs stored to disk")
  42. end
  43. -- ===================================================================
  44. -- This function stores data to disk.
  45. modpol.store_data = function()
  46. store_ledger()
  47. store_orgs()
  48. end
  49. -- ===================================================================
  50. -- This function loads "ledger" data from disk.
  51. local load_ledger = function()
  52. local obj = modpol.ocutil.file_read (modpol.file_ledger )
  53. if obj ~= nil then
  54. local func, err = load (obj)
  55. if err then
  56. modpol.ocutil.fatal_error ("load_data: ledger" )
  57. end
  58. modpol.ledger = func()
  59. local nn = modpol.ocutil.table_length (modpol.ledger)
  60. local str = "entries"
  61. if nn == 1 then str = "entry" end
  62. modpol.ocutil.log (nn .. " global ledger entries loaded from disk")
  63. else
  64. modpol.ocutil.log ("No stored global ledger data found")
  65. end
  66. end
  67. -- ===================================================================
  68. -- This function loads "orgs" data from disk.
  69. local load_orgs = function()
  70. local obj = modpol.ocutil.file_read (modpol.file_orgs )
  71. if obj ~= nil then
  72. local func, err = load (obj)
  73. if err then
  74. modpol.ocutil.fatal_error ("load_data: orgs" )
  75. end
  76. modpol.orgs = func()
  77. local nn = modpol.ocutil.table_length (modpol.orgs)
  78. local str = "entries"
  79. if nn == 1 then str = "entry" end
  80. modpol.ocutil.log (nn .. " orgs loaded from disk")
  81. else
  82. modpol.ocutil.log ("No stored orgs data found")
  83. end
  84. end
  85. -- ===================================================================
  86. -- This function loads stored data from disk.
  87. modpol.load_storage = function()
  88. load_ledger()
  89. load_orgs()
  90. end
  91. -- ===================================================================
  92. -- End of file.