func (s *server) AddDisk(name, root string) error { s.disks[name] = &disk.Disk{Name: name, Root: root} err := os.MkdirAll(root, 0700) if err != nil { return err } pwd, err := os.Getwd() if err != nil { log.Panicf("server: cannot get current working directory (%v)", err) } log.Infof("server: created disk[%s] at root path[%s]", name, path.Join(pwd, root)) return nil }
// fillBlock fills the partial block starting from the given offset with the // given data. It first reads out the block, fills in the given data, and // writes the block back. // The given index and offset should locate an existing point in the file. // The given bsize indicates the size of each block. // The given data should all be in the target block, and not exceed the // block boundary. func fillBlock(f *os.File, index, offset, bsize int64, data []byte) error { buf := make([]byte, bsize-crc32Len) n, err := readBlock(f, buf, index, bsize) if err != nil && err != io.EOF { return err } switch { case n < offset: log.Panicf("offset should be not bigger than file size") case n < offset+int64(len(data)): buf = append(buf[:offset], data...) default: copy(buf[offset:offset+int64(len(data))], data) buf = buf[:n] } return writeBlock(f, index, bsize, buf) }
// NewSyncer construct a syncer with the given sender and conf // If conf.Path is not exist it will return err func NewSyncer(sender Sender, conf SyncConfig) (*Syncer, error) { if conf.BatchSize == 0 { conf.BatchSize = DefaultBatchSize } else if conf.BatchSize > MaxBatchSize { log.Panicf("BatchSize %d > %d", conf.BatchSize, MaxBatchSize) } if conf.IntervalSecond == 0 { conf.IntervalSecond = DefaultIntervalSecond } else if conf.IntervalSecond < 0 { conf.IntervalSecond = 0 } if conf.Separator == 0 { conf.Separator = DefaultSeparator } isdir, err := isDir(conf.Path) if err != nil { return nil, err } var ( dir = conf.Path mp = "*" ) if !isdir { dir = filepath.Dir(conf.Path) mp = filepath.Base(conf.Path) + "*" } if dir, err = filepath.Abs(dir); err != nil { return nil, err } syncer := &Syncer{ SyncConfig: &conf, dir: dir, matchPattern: mp, sender: sender, endch: make(chan struct{}), } return syncer, nil }