| 
									
										
										
										
											2018-11-07 02:19:42 +08:00
										 |  |  | // Copyright 2018 The Prometheus Authors
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | package fileutil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-11-15 02:04:30 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type MmapFile struct { | 
					
						
							|  |  |  | 	f *os.File | 
					
						
							|  |  |  | 	b []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func OpenMmapFile(path string) (*MmapFile, error) { | 
					
						
							| 
									
										
										
										
											2020-02-05 21:38:30 +08:00
										 |  |  | 	return OpenMmapFileWithSize(path, 0) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 00:33:44 +08:00
										 |  |  | func OpenMmapFileWithSize(path string, size int) (mf *MmapFile, retErr error) { | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | 	f, err := os.Open(path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-15 02:04:30 +08:00
										 |  |  | 		return nil, fmt.Errorf("try lock file: %w", err) | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-20 00:33:44 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if retErr != nil { | 
					
						
							|  |  |  | 			f.Close() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2020-02-05 21:38:30 +08:00
										 |  |  | 	if size <= 0 { | 
					
						
							|  |  |  | 		info, err := f.Stat() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-15 02:04:30 +08:00
										 |  |  | 			return nil, fmt.Errorf("stat: %w", err) | 
					
						
							| 
									
										
										
										
											2020-02-05 21:38:30 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		size = int(info.Size()) | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 21:38:30 +08:00
										 |  |  | 	b, err := mmap(f, size) | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-15 02:04:30 +08:00
										 |  |  | 		return nil, fmt.Errorf("mmap, size %d: %w", size, err) | 
					
						
							| 
									
										
										
										
											2017-11-30 22:34:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &MmapFile{f: f, b: b}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *MmapFile) Close() error { | 
					
						
							|  |  |  | 	err0 := munmap(f.b) | 
					
						
							|  |  |  | 	err1 := f.f.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err0 != nil { | 
					
						
							|  |  |  | 		return err0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *MmapFile) File() *os.File { | 
					
						
							|  |  |  | 	return f.f | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *MmapFile) Bytes() []byte { | 
					
						
							|  |  |  | 	return f.b | 
					
						
							|  |  |  | } |