When async,and nfs it's more optimized than you think [Solved]

I need help about this code

async fn bajar_imagen(
    url_imagen: Url,
    nombre_fichero: &Path,
    directorio_capitulo: &Path,
    cliente: &reqwest::Client,
) -> Result<(), &'static str> {
    dbg!(&url_imagen);
    const MIN_SIZE_FILE: u64 = 1141;

    match cliente.get(url_imagen).send().await {
        Ok(mut doc1) => {
            dbg!(&nombre_fichero);
            match tokio::fs::File::create(&nombre_fichero).await {
                Ok(mut file) => {
                    while let Some(chunk) = match doc1.chunk().await {
                        Ok(x) => x,
                        Err(e) => {
                            imprimir_rojo(
                                "        Fallo al obtener datos de la imagen, borrar directorio",
                            );
                            println!("Error: {}", e);
                            borrar_directorio_y_panic(&directorio_capitulo).await;
                            panic!();
                        }
                    } {
                        match file.write_all(&chunk).await {
                            Ok(_) => (),
                            Err(e) => {
                                imprimir_rojo(
                                    "        Fallo al escribir en el fichero, borrar directorio",
                                );
                                println!("Error: {}", e);
                                borrar_directorio_y_panic(&directorio_capitulo).await;
                                panic!();
                            }
                        }
                    }
                }
                Err(e) => {
                    imprimir_rojo("        Error al crear fichero, borrar directorio");
                    println!("Error: {}", e);
                    borrar_directorio_y_panic(&directorio_capitulo).await;
                    panic!();
                }
            }
            println!("Tamaño fichero creado: {}",Path::new(nombre_fichero).metadata().unwrap().len());
            println!("Tamaño fichero creado segunda lectura: {}",Path::new(nombre_fichero).metadata().unwrap().len());
            if Path::new(nombre_fichero).metadata().unwrap().len() <= MIN_SIZE_FILE {
                Err("fichero_pequeño")
            } else {
                Ok(())
            }
        }
        Err(e) => {
            imprimir_rojo("        Error al bajar imagen mirar status, borrar directorio");
            println!("Error: {}", e);
            borrar_directorio_y_panic(&directorio_capitulo).await;
            panic!();
        }
    }
}

I'm trying to get a image, the problem is that i check if the image is correct looking with the size of the write image

if it's very little i suposse that there is an error (<1141 bytes)

But the nfs is still writing the image (or i think that this is the problem) because sometimes
when i check two or three time the size of the file, the size is increasing

./scrape
http://www.mangatown.com/manga/danberu_nan_kiro_moteru/ Apr 22,2022
    https://www.mangatown.com/manga/danberu_nan_kiro_moteru/c144/
Tamaño fichero creado: 0
Tamaño fichero creado segunda lectura: 16
        Fichero muy pequeño, borrar directorio
        Se ha borrado el directorio

i mount the nfs share with this mount:

sudo mount -o sync -t nfs 192.168.1.2:/Cyberdojo /mnt/Cyberdojo 

(in the server options are subtree_check,insecure,crossmnt,sync)

I don't know why perhaps because i have and zfs, after the share of the nfs,
the file is created but i don't know how to solve and see if the write or the image is ok, or how to wait to the sync of the write that the filesystem is doing while i check the size of the file that i have writed

You should call File::sync_all() before dropping the File object. This will ensure that both the contents and metadata of the file are fully written to disk.

Many thanks, now this part is ok, but i got something similar when i delete a folder with files

that sometimes i get an error because the directory is not empty

'''
async fn borrar_directorio_y_panic(directorio_capitulo: &Path) -> ! {
match tokio::fs::remove_dir_all(&directorio_capitulo).await {
Ok(_) => {
imprimir_verde(" Se ha borrado el directorio");
}
Err(e) => {
imprimir_rojo(" No he podido borrar directorio");
println!("Error: {}", e);
}
};
panic!();
}

'''

But in this funciton remove_dir_all in tokio::fs - Rust i don't see how to do a sync in the folder (after that i do a panic), because i want to stop the program but with the folder removed (i can say that many times it's work OK, but sometimes it's seems that rust want to delete the folder before the files have been deleted

What is the exact error that you get? tokio::fs::remove_dir_all() is a thin wrapper over the standard library function std::fs::remove_dir_all(), which should be pretty robust on all systems. Are you sure that you have the permissions to delete all of the contents?

i think that the problem it's the mount of the fedora, because in the raspberry i don't have the same problem and the rust is always deleting the folder,... i change this to solved

because i'm seeing that now the page have changed the way of seeing the images and i'm obtain a 403 when i to get directly to the images

I think your code would be much clearer with some heavy use of error shortcut, a.k.a. "?". You'd have something like:

    let mut doc1 = cliente.get(url_imagen).send().await?;
    dbg!(&nombre_fichero);
    let mut file = tokio::fs::File::create(&nombre_fichero).await?;
    ...

I know but this way i'm obligated to do something if the functions gone wrong, sometimes if something goes wrong i prefer to delete the folder where i'm downloading a chapter

the tipical problem about code clean and do something with the errors in the moment you catch it

this is the unique fucntion in my code that i return a Result(), typical i return nothing because i try to catch all the errors and this way i can understand more where my code or the libraries that i used can go wrong

Thanks for the info, perhaps when i recode all i will try to send the errors to the fucntions more up-level, in all the functions, and leave the code more clean

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.