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