taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

file.go (15198B)


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