iTranslated by AI
FizzBuzz.txt (8 Exabytes)
FizzBuzzFS
FizzBuzz problems are famously known for periodically becoming a hot topic on social media, with people inventing bizarre solutions (?), but since I hadn't seen an example using a filesystem, I decided to give it a try.

Well, as you can see... /mnt/FizzBuzz contains FizzBuzz.txt (8 exabytes), and it contains FizzBuzz.

On and on...

As long as there is capacity...

That's all!!!!!!!
Technical Explanation
FUSE
By using software called FUSE (Filesystem in USEr space), it is possible to easily implement a new filesystem.
In Rust, while it seems there are multiple forks as a result of the fuse crate being discontinued, it seems better to use the fuser crate considering the number of users and update frequency.
If you pass a value that implements the FileSystem trait to the mount2 function, you'll have a functioning filesystem for now. Since default implementations are provided for all methods, the minimal configuration is extremely simple.
use fuser::{Filesystem, MountOption};
use std::env;
struct NullFS;
impl Filesystem for NullFS {}
fn main() {
env_logger::init(); // This can be omitted
let mountpoint = env::args_os().nth(1).unwrap();
fuser::mount2(NullFS, mountpoint, &[MountOption::AutoUnmount]).unwrap();
}
This alone works as a mountable filesystem. Though it can't do anything other than mounting...
The features required this time are:
- Being able to get a list of files with
ls - Being able to get the contents of
FizzBuzz.txt
However, referring to the official sample hello.rs is sufficient.
The methods to be implemented are:
-
lookup: Returns file metadata based on the name -
getattr: Returns file metadata based on the inode number -
read: Returns file content based on the inode number and position -
readdir: Returns directory content based on the inode number
Except for read, these are almost exactly like the sample. For read, just prepare a function char_at(pos) that returns the content by specifying the file position:
let mut buf = Vec::new();
for i in offset..(offset.saturating_add(size as i64)) {
buf.push(char_at(i));
}
reply.data(&buf)
Function to calculate the content at an arbitrary position in FizzBuzz.txt
We can't just calculate 8 exabytes of FizzBuzz, so we'll take the approach of calculating only the part needed once a position is specified. It's probably about the difficulty level of a D-problem in AtCoder ABC. It took me a whole day, though...
- Write a function
fizz_buzz_length(n)that calculates the number of bytes up to the -th line ofn FizzBuzz.txt. - Find which line in
FizzBuzz.txtcorresponds to the specified position using binary search withfizz_buzz_length. - Calculate the content of that line and return the character at the specified position.
That's the plan. Calculating character by character is inefficient, but it doesn't pose a practical problem, so it's fine.
First, regarding fizz_buzz_length(n), it's really messy, so please don't look at it too closely. By calculating the "number of numeric lines", "number of Fizz lines", "number of Buzz lines", and "number of FizzBuzz lines" for each interval (0-9, 10-99, 100-999, etc.) and summing their character counts, we get the total character count. It's ugly!!!
pub fn fizz_buzz_length(n: i64) -> u128 {
assert!(n >= 0);
let n = n as u128;
let zero = 0u128;
let mut num_len_sum = zero;
let mut num_count = zero;
let mut fizz_count = zero;
let mut buzz_count = zero;
let mut fizz_buzz_count = zero;
let max = (i64::MAX / 10 + 1) as u128;
let mut i = 10u128;
let mut digits = 1;
while i <= n {
let fizz_buzz_count0 = (i - 1) / 15 - fizz_buzz_count;
let fizz_count0 = (i - 1) / 3 - fizz_buzz_count0 - fizz_count - fizz_buzz_count;
let buzz_count0 = (i - 1) / 5 - fizz_buzz_count0 - buzz_count - fizz_buzz_count;
fizz_buzz_count += fizz_buzz_count0;
fizz_count += fizz_count0;
buzz_count += buzz_count0;
let num_count0 = i - fizz_count - buzz_count - fizz_buzz_count - num_count;
num_count += num_count0;
num_len_sum += (digits + NEWLINE_LEN) * num_count0;
assert_eq!(num_count + fizz_count + buzz_count + fizz_buzz_count, i);
digits += 1;
if max <= i {
break;
}
i *= 10;
}
let fizz_buzz_count0 = n / 15 - fizz_buzz_count;
let fizz_count0 = n / 3 - fizz_buzz_count0 - fizz_count - fizz_buzz_count;
let buzz_count0 = n / 5 - fizz_buzz_count0 - buzz_count - fizz_buzz_count;
fizz_buzz_count += fizz_buzz_count0;
fizz_count += fizz_count0;
buzz_count += buzz_count0;
let num_count0 = n + 1 - fizz_count - buzz_count - fizz_buzz_count - num_count;
num_count += num_count0;
num_len_sum += (digits + NEWLINE_LEN) * num_count0;
assert_eq!(fizz_count + buzz_count + fizz_buzz_count + num_count, n + 1);
// 1 + NEWLINE_LEN for "0\n"
num_len_sum
+ fizz_count * (4 + NEWLINE_LEN)
+ buzz_count * (4 + NEWLINE_LEN)
+ fizz_buzz_count * (8 + NEWLINE_LEN)
- (1 + NEWLINE_LEN)
}
Calculating the content of the Cow.
pub fn line_at(n: i64) -> Cow<'static, [u8]> {
if n % 15 == 0 {
Cow::from(b"FizzBuzz\n" as &'static [u8])
} else if n % 3 == 0 {
Cow::from(b"Fizz\n" as &'static [u8])
} else if n % 5 == 0 {
Cow::from(b"Buzz\n" as &'static [u8])
} else {
let s = format!("{}\n", n);
let v = Vec::from(s.as_bytes());
Cow::from(v)
}
}
A function that uses the two functions above to find the content at the index-th byte. Binary search is performed under the condition that l is the index of a line before index, and r is the index of a line that contains or comes after index. When the search finishes, r will be the index of the line containing index.
pub fn char_at(index: i64) -> u8 {
assert!(index >= 0);
let index = index as u128;
let mut l = 0;
let mut r = i64::MAX;
while l + 1 < r {
let mid = l + (r - l) / 2;
let v = fizz_buzz_length(mid);
if v <= index {
l = mid;
} else {
r = mid;
}
}
let base = fizz_buzz_length(r - 1);
let line_index = (index - base) as usize;
let line = line_at(r);
line[line_index]
}
And so, we can now read any part of the 8 exabytes. Isn't that great?
Discussion