| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2018 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:10 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/minio/minio/pkg/ellipses" | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CacheConfig represents cache config settings
 | 
					
						
							|  |  |  | type CacheConfig struct { | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 	Drives  []string `json:"drives"` | 
					
						
							|  |  |  | 	Expiry  int      `json:"expiry"` | 
					
						
							| 
									
										
										
										
											2018-06-26 01:24:12 +08:00
										 |  |  | 	MaxUse  int      `json:"maxuse"` | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 	Exclude []string `json:"exclude"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // UnmarshalJSON - implements JSON unmarshal interface for unmarshalling
 | 
					
						
							|  |  |  | // json entries for CacheConfig.
 | 
					
						
							|  |  |  | func (cfg *CacheConfig) UnmarshalJSON(data []byte) (err error) { | 
					
						
							|  |  |  | 	type Alias CacheConfig | 
					
						
							|  |  |  | 	var _cfg = &struct { | 
					
						
							|  |  |  | 		*Alias | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		Alias: (*Alias)(cfg), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err = json.Unmarshal(data, _cfg); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if _, err = parseCacheDrives(_cfg.Drives); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if _, err = parseCacheExcludes(_cfg.Exclude); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Parses given cacheDrivesEnv and returns a list of cache drives.
 | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | func parseCacheDrives(drives []string) ([]string, error) { | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:10 +08:00
										 |  |  | 	if len(drives) == 0 { | 
					
						
							|  |  |  | 		return drives, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var endpoints []string | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 	for _, d := range drives { | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:10 +08:00
										 |  |  | 		if ellipses.HasEllipses(d) { | 
					
						
							|  |  |  | 			s, err := parseCacheDrivePaths(d) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return nil, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			endpoints = append(endpoints, s...) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			endpoints = append(endpoints, d) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, d := range endpoints { | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 		if !filepath.IsAbs(d) { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 			return nil, uiErrInvalidCacheDrivesValue(nil).Msg("cache dir should be absolute path: %s", d) | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:10 +08:00
										 |  |  | 	return endpoints, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Parses all arguments and returns a slice of drive paths following the ellipses pattern.
 | 
					
						
							|  |  |  | func parseCacheDrivePaths(arg string) (ep []string, err error) { | 
					
						
							|  |  |  | 	patterns, perr := ellipses.FindEllipsesPatterns(arg) | 
					
						
							|  |  |  | 	if perr != nil { | 
					
						
							|  |  |  | 		return []string{}, uiErrInvalidCacheDrivesValue(nil).Msg(perr.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, lbls := range patterns.Expand() { | 
					
						
							|  |  |  | 		ep = append(ep, strings.Join(lbls, "")) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ep, nil | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Parses given cacheExcludesEnv and returns a list of cache exclude patterns.
 | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | func parseCacheExcludes(excludes []string) ([]string, error) { | 
					
						
							|  |  |  | 	for _, e := range excludes { | 
					
						
							|  |  |  | 		if len(e) == 0 { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 			return nil, uiErrInvalidCacheExcludesValue(nil).Msg("cache exclude path (%s) cannot be empty", e) | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if hasPrefix(e, slashSeparator) { | 
					
						
							| 
									
										
										
										
											2018-05-09 10:04:36 +08:00
										 |  |  | 			return nil, uiErrInvalidCacheExcludesValue(nil).Msg("cache exclude pattern (%s) cannot start with / as prefix", e) | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-30 05:38:26 +08:00
										 |  |  | 	return excludes, nil | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | } |