file.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // Copyright 2017 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package ini
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "strings"
  23. "sync"
  24. )
  25. // File represents a combination of one or more INI files in memory.
  26. type File struct {
  27. options LoadOptions
  28. dataSources []dataSource
  29. // Should make things safe, but sometimes doesn't matter.
  30. BlockMode bool
  31. lock sync.RWMutex
  32. // To keep data in order.
  33. sectionList []string
  34. // To keep track of the index of a section with same name.
  35. // This meta list is only used with non-unique section names are allowed.
  36. sectionIndexes []int
  37. // Actual data is stored here.
  38. sections map[string][]*Section
  39. NameMapper
  40. ValueMapper
  41. }
  42. // newFile initializes File object with given data sources.
  43. func newFile(dataSources []dataSource, opts LoadOptions) *File {
  44. if len(opts.KeyValueDelimiters) == 0 {
  45. opts.KeyValueDelimiters = "=:"
  46. }
  47. if len(opts.KeyValueDelimiterOnWrite) == 0 {
  48. opts.KeyValueDelimiterOnWrite = "="
  49. }
  50. if len(opts.ChildSectionDelimiter) == 0 {
  51. opts.ChildSectionDelimiter = "."
  52. }
  53. return &File{
  54. BlockMode: true,
  55. dataSources: dataSources,
  56. sections: make(map[string][]*Section),
  57. options: opts,
  58. }
  59. }
  60. // Empty returns an empty file object.
  61. func Empty(opts ...LoadOptions) *File {
  62. var opt LoadOptions
  63. if len(opts) > 0 {
  64. opt = opts[0]
  65. }
  66. // Ignore error here, we are sure our data is good.
  67. f, _ := LoadSources(opt, []byte(""))
  68. return f
  69. }
  70. // NewSection creates a new section.
  71. func (f *File) NewSection(name string) (*Section, error) {
  72. if len(name) == 0 {
  73. return nil, errors.New("empty section name")
  74. }
  75. if (f.options.Insensitive || f.options.InsensitiveSections) && name != DefaultSection {
  76. name = strings.ToLower(name)
  77. }
  78. if f.BlockMode {
  79. f.lock.Lock()
  80. defer f.lock.Unlock()
  81. }
  82. if !f.options.AllowNonUniqueSections && inSlice(name, f.sectionList) {
  83. return f.sections[name][0], nil
  84. }
  85. f.sectionList = append(f.sectionList, name)
  86. // NOTE: Append to indexes must happen before appending to sections,
  87. // otherwise index will have off-by-one problem.
  88. f.sectionIndexes = append(f.sectionIndexes, len(f.sections[name]))
  89. sec := newSection(f, name)
  90. f.sections[name] = append(f.sections[name], sec)
  91. return sec, nil
  92. }
  93. // NewRawSection creates a new section with an unparseable body.
  94. func (f *File) NewRawSection(name, body string) (*Section, error) {
  95. section, err := f.NewSection(name)
  96. if err != nil {
  97. return nil, err
  98. }
  99. section.isRawSection = true
  100. section.rawBody = body
  101. return section, nil
  102. }
  103. // NewSections creates a list of sections.
  104. func (f *File) NewSections(names ...string) (err error) {
  105. for _, name := range names {
  106. if _, err = f.NewSection(name); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }
  112. // GetSection returns section by given name.
  113. func (f *File) GetSection(name string) (*Section, error) {
  114. secs, err := f.SectionsByName(name)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return secs[0], err
  119. }
  120. // HasSection returns true if the file contains a section with given name.
  121. func (f *File) HasSection(name string) bool {
  122. section, _ := f.GetSection(name)
  123. return section != nil
  124. }
  125. // SectionsByName returns all sections with given name.
  126. func (f *File) SectionsByName(name string) ([]*Section, error) {
  127. if len(name) == 0 {
  128. name = DefaultSection
  129. }
  130. if f.options.Insensitive || f.options.InsensitiveSections {
  131. name = strings.ToLower(name)
  132. }
  133. if f.BlockMode {
  134. f.lock.RLock()
  135. defer f.lock.RUnlock()
  136. }
  137. secs := f.sections[name]
  138. if len(secs) == 0 {
  139. return nil, fmt.Errorf("section %q does not exist", name)
  140. }
  141. return secs, nil
  142. }
  143. // Section assumes named section exists and returns a zero-value when not.
  144. func (f *File) Section(name string) *Section {
  145. sec, err := f.GetSection(name)
  146. if err != nil {
  147. if name == "" {
  148. name = DefaultSection
  149. }
  150. sec, _ = f.NewSection(name)
  151. return sec
  152. }
  153. return sec
  154. }
  155. // SectionWithIndex assumes named section exists and returns a new section when not.
  156. func (f *File) SectionWithIndex(name string, index int) *Section {
  157. secs, err := f.SectionsByName(name)
  158. if err != nil || len(secs) <= index {
  159. // NOTE: It's OK here because the only possible error is empty section name,
  160. // but if it's empty, this piece of code won't be executed.
  161. newSec, _ := f.NewSection(name)
  162. return newSec
  163. }
  164. return secs[index]
  165. }
  166. // Sections returns a list of Section stored in the current instance.
  167. func (f *File) Sections() []*Section {
  168. if f.BlockMode {
  169. f.lock.RLock()
  170. defer f.lock.RUnlock()
  171. }
  172. sections := make([]*Section, len(f.sectionList))
  173. for i, name := range f.sectionList {
  174. sections[i] = f.sections[name][f.sectionIndexes[i]]
  175. }
  176. return sections
  177. }
  178. // ChildSections returns a list of child sections of given section name.
  179. func (f *File) ChildSections(name string) []*Section {
  180. return f.Section(name).ChildSections()
  181. }
  182. // SectionStrings returns list of section names.
  183. func (f *File) SectionStrings() []string {
  184. list := make([]string, len(f.sectionList))
  185. copy(list, f.sectionList)
  186. return list
  187. }
  188. // DeleteSection deletes a section or all sections with given name.
  189. func (f *File) DeleteSection(name string) {
  190. secs, err := f.SectionsByName(name)
  191. if err != nil {
  192. return
  193. }
  194. for i := 0; i < len(secs); i++ {
  195. // For non-unique sections, it is always needed to remove the first one so
  196. // in the next iteration, the subsequent section continue having index 0.
  197. // Ignoring the error as index 0 never returns an error.
  198. _ = f.DeleteSectionWithIndex(name, 0)
  199. }
  200. }
  201. // DeleteSectionWithIndex deletes a section with given name and index.
  202. func (f *File) DeleteSectionWithIndex(name string, index int) error {
  203. if !f.options.AllowNonUniqueSections && index != 0 {
  204. return fmt.Errorf("delete section with non-zero index is only allowed when non-unique sections is enabled")
  205. }
  206. if len(name) == 0 {
  207. name = DefaultSection
  208. }
  209. if f.options.Insensitive || f.options.InsensitiveSections {
  210. name = strings.ToLower(name)
  211. }
  212. if f.BlockMode {
  213. f.lock.Lock()
  214. defer f.lock.Unlock()
  215. }
  216. // Count occurrences of the sections
  217. occurrences := 0
  218. sectionListCopy := make([]string, len(f.sectionList))
  219. copy(sectionListCopy, f.sectionList)
  220. for i, s := range sectionListCopy {
  221. if s != name {
  222. continue
  223. }
  224. if occurrences == index {
  225. if len(f.sections[name]) <= 1 {
  226. delete(f.sections, name) // The last one in the map
  227. } else {
  228. f.sections[name] = append(f.sections[name][:index], f.sections[name][index+1:]...)
  229. }
  230. // Fix section lists
  231. f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
  232. f.sectionIndexes = append(f.sectionIndexes[:i], f.sectionIndexes[i+1:]...)
  233. } else if occurrences > index {
  234. // Fix the indices of all following sections with this name.
  235. f.sectionIndexes[i-1]--
  236. }
  237. occurrences++
  238. }
  239. return nil
  240. }
  241. func (f *File) reload(s dataSource) error {
  242. r, err := s.ReadCloser()
  243. if err != nil {
  244. return err
  245. }
  246. defer r.Close()
  247. return f.parse(r)
  248. }
  249. // Reload reloads and parses all data sources.
  250. func (f *File) Reload() (err error) {
  251. for _, s := range f.dataSources {
  252. if err = f.reload(s); err != nil {
  253. // In loose mode, we create an empty default section for nonexistent files.
  254. if os.IsNotExist(err) && f.options.Loose {
  255. _ = f.parse(bytes.NewBuffer(nil))
  256. continue
  257. }
  258. return err
  259. }
  260. if f.options.ShortCircuit {
  261. return nil
  262. }
  263. }
  264. return nil
  265. }
  266. // Append appends one or more data sources and reloads automatically.
  267. func (f *File) Append(source interface{}, others ...interface{}) error {
  268. ds, err := parseDataSource(source)
  269. if err != nil {
  270. return err
  271. }
  272. f.dataSources = append(f.dataSources, ds)
  273. for _, s := range others {
  274. ds, err = parseDataSource(s)
  275. if err != nil {
  276. return err
  277. }
  278. f.dataSources = append(f.dataSources, ds)
  279. }
  280. return f.Reload()
  281. }
  282. func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
  283. equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight
  284. if PrettyFormat || PrettyEqual {
  285. equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite)
  286. }
  287. // Use buffer to make sure target is safe until finish encoding.
  288. buf := bytes.NewBuffer(nil)
  289. lastSectionIdx := len(f.sectionList) - 1
  290. for i, sname := range f.sectionList {
  291. sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
  292. if len(sec.Comment) > 0 {
  293. // Support multiline comments
  294. lines := strings.Split(sec.Comment, LineBreak)
  295. for i := range lines {
  296. if lines[i][0] != '#' && lines[i][0] != ';' {
  297. lines[i] = "; " + lines[i]
  298. } else {
  299. lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
  300. }
  301. if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
  302. return nil, err
  303. }
  304. }
  305. }
  306. if i > 0 || DefaultHeader || (i == 0 && strings.ToUpper(sec.name) != DefaultSection) {
  307. if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
  308. return nil, err
  309. }
  310. } else {
  311. // Write nothing if default section is empty
  312. if len(sec.keyList) == 0 {
  313. continue
  314. }
  315. }
  316. isLastSection := i == lastSectionIdx
  317. if sec.isRawSection {
  318. if _, err := buf.WriteString(sec.rawBody); err != nil {
  319. return nil, err
  320. }
  321. if PrettySection && !isLastSection {
  322. // Put a line between sections
  323. if _, err := buf.WriteString(LineBreak); err != nil {
  324. return nil, err
  325. }
  326. }
  327. continue
  328. }
  329. // Count and generate alignment length and buffer spaces using the
  330. // longest key. Keys may be modified if they contain certain characters so
  331. // we need to take that into account in our calculation.
  332. alignLength := 0
  333. if PrettyFormat {
  334. for _, kname := range sec.keyList {
  335. keyLength := len(kname)
  336. // First case will surround key by ` and second by """
  337. if strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters) {
  338. keyLength += 2
  339. } else if strings.Contains(kname, "`") {
  340. keyLength += 6
  341. }
  342. if keyLength > alignLength {
  343. alignLength = keyLength
  344. }
  345. }
  346. }
  347. alignSpaces := bytes.Repeat([]byte(" "), alignLength)
  348. KeyList:
  349. for _, kname := range sec.keyList {
  350. key := sec.Key(kname)
  351. if len(key.Comment) > 0 {
  352. if len(indent) > 0 && sname != DefaultSection {
  353. buf.WriteString(indent)
  354. }
  355. // Support multiline comments
  356. lines := strings.Split(key.Comment, LineBreak)
  357. for i := range lines {
  358. if lines[i][0] != '#' && lines[i][0] != ';' {
  359. lines[i] = "; " + strings.TrimSpace(lines[i])
  360. } else {
  361. lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
  362. }
  363. if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
  364. return nil, err
  365. }
  366. }
  367. }
  368. if len(indent) > 0 && sname != DefaultSection {
  369. buf.WriteString(indent)
  370. }
  371. switch {
  372. case key.isAutoIncrement:
  373. kname = "-"
  374. case strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters):
  375. kname = "`" + kname + "`"
  376. case strings.Contains(kname, "`"):
  377. kname = `"""` + kname + `"""`
  378. }
  379. writeKeyValue := func(val string) (bool, error) {
  380. if _, err := buf.WriteString(kname); err != nil {
  381. return false, err
  382. }
  383. if key.isBooleanType {
  384. buf.WriteString(LineBreak)
  385. return true, nil
  386. }
  387. // Write out alignment spaces before "=" sign
  388. if PrettyFormat {
  389. buf.Write(alignSpaces[:alignLength-len(kname)])
  390. }
  391. // In case key value contains "\n", "`", "\"", "#" or ";"
  392. if strings.ContainsAny(val, "\n`") {
  393. val = `"""` + val + `"""`
  394. } else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
  395. val = "`" + val + "`"
  396. } else if len(strings.TrimSpace(val)) != len(val) {
  397. val = `"` + val + `"`
  398. }
  399. if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
  400. return false, err
  401. }
  402. return false, nil
  403. }
  404. shadows := key.ValueWithShadows()
  405. if len(shadows) == 0 {
  406. if _, err := writeKeyValue(""); err != nil {
  407. return nil, err
  408. }
  409. }
  410. for _, val := range shadows {
  411. exitLoop, err := writeKeyValue(val)
  412. if err != nil {
  413. return nil, err
  414. } else if exitLoop {
  415. continue KeyList
  416. }
  417. }
  418. for _, val := range key.nestedValues {
  419. if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
  420. return nil, err
  421. }
  422. }
  423. }
  424. if PrettySection && !isLastSection {
  425. // Put a line between sections
  426. if _, err := buf.WriteString(LineBreak); err != nil {
  427. return nil, err
  428. }
  429. }
  430. }
  431. return buf, nil
  432. }
  433. // WriteToIndent writes content into io.Writer with given indention.
  434. // If PrettyFormat has been set to be true,
  435. // it will align "=" sign with spaces under each section.
  436. func (f *File) WriteToIndent(w io.Writer, indent string) (int64, error) {
  437. buf, err := f.writeToBuffer(indent)
  438. if err != nil {
  439. return 0, err
  440. }
  441. return buf.WriteTo(w)
  442. }
  443. // WriteTo writes file content into io.Writer.
  444. func (f *File) WriteTo(w io.Writer) (int64, error) {
  445. return f.WriteToIndent(w, "")
  446. }
  447. // SaveToIndent writes content to file system with given value indention.
  448. func (f *File) SaveToIndent(filename, indent string) error {
  449. // Note: Because we are truncating with os.Create,
  450. // so it's safer to save to a temporary file location and rename after done.
  451. buf, err := f.writeToBuffer(indent)
  452. if err != nil {
  453. return err
  454. }
  455. return ioutil.WriteFile(filename, buf.Bytes(), 0666)
  456. }
  457. // SaveTo writes content to file system.
  458. func (f *File) SaveTo(filename string) error {
  459. return f.SaveToIndent(filename, "")
  460. }