| 
									
										
										
										
											2019-01-28 14:23:59 +03:00
										 |  |  | // For more tutorials: https://blog.learngoprogramming.com | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	args := os.Args[1:] | 
					
						
							|  |  |  | 	if len(args) == 0 { | 
					
						
							|  |  |  | 		fmt.Println("Provide a directory") | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	files, err := ioutil.ReadDir(args[0]) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Println(err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-06 18:24:47 +03:00
										 |  |  | 	// 1st: You can also take the average of the total file length | 
					
						
							|  |  |  | 	//      across platforms. It's about 255. | 
					
						
							|  |  |  | 	// | 
					
						
							| 
									
										
										
										
											2019-03-06 18:30:41 +03:00
										 |  |  | 	// https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// BTRFS   255 bytes | 
					
						
							|  |  |  | 	// exFAT   255 UTF-16 characters | 
					
						
							|  |  |  | 	// ext2    255 bytes | 
					
						
							|  |  |  | 	// ext3    255 bytes | 
					
						
							|  |  |  | 	// ext3cow 255 bytes | 
					
						
							|  |  |  | 	// ext4    255 bytes | 
					
						
							|  |  |  | 	// FAT32   255 bytes | 
					
						
							|  |  |  | 	// NTFS    255 characters | 
					
						
							|  |  |  | 	// XFS     255 bytes | 
					
						
							| 
									
										
										
										
											2019-03-06 18:24:47 +03:00
										 |  |  | 	// | 
					
						
							|  |  |  | 	// total := len(files) * 256 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 1st B: To be exact, find the total size of all the empty files | 
					
						
							| 
									
										
										
										
											2019-01-28 14:23:59 +03:00
										 |  |  | 	var total int | 
					
						
							|  |  |  | 	for _, file := range files { | 
					
						
							|  |  |  | 		if file.Size() == 0 { | 
					
						
							|  |  |  | 			// +1 for the newline character | 
					
						
							| 
									
										
										
										
											2019-03-06 18:24:47 +03:00
										 |  |  | 			// when printing the filename afterward | 
					
						
							| 
									
										
										
										
											2019-01-28 14:23:59 +03:00
										 |  |  | 			total += len(file.Name()) + 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-06 18:24:47 +03:00
										 |  |  | 	fmt.Printf("Total required space: %d bytes.\n", total) | 
					
						
							| 
									
										
										
										
											2019-01-28 14:23:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// 2nd: allocate a large enough byte slice in one go | 
					
						
							|  |  |  | 	names := make([]byte, 0, total) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, file := range files { | 
					
						
							|  |  |  | 		if file.Size() == 0 { | 
					
						
							|  |  |  | 			name := file.Name() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			names = append(names, name...) | 
					
						
							|  |  |  | 			names = append(names, '\n') | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = ioutil.WriteFile("out.txt", names, 0644) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Println(err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%s", names) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // See: https://www.tutorialspoint.com/unix/unix-file-permission.htm | 
					
						
							|  |  |  | // See: http://permissions-calculator.org/ |