@@ -556,6 +556,7 @@ int ca_chunk_file_load(
556556 CaChunkCompression * ret_effective_compression ) {
557557
558558 _cleanup_ (safe_closep ) int fd = -1 ;
559+ CaChunkCompression fd_compression ;
559560 int r ;
560561
561562 if (chunk_fd < 0 && chunk_fd != AT_FDCWD )
@@ -569,40 +570,48 @@ int ca_chunk_file_load(
569570 if (!buffer )
570571 return - EINVAL ;
571572
572- fd = ca_chunk_file_open (chunk_fd , prefix , chunkid , NULL , O_RDONLY |O_CLOEXEC |O_NOCTTY |O_NOFOLLOW );
573+ if (desired_compression == CA_CHUNK_UNCOMPRESSED ) {
574+ fd = ca_chunk_file_open (chunk_fd , prefix , chunkid , NULL , O_RDONLY |O_CLOEXEC |O_NOCTTY |O_NOFOLLOW );
575+ fd_compression = CA_CHUNK_UNCOMPRESSED ;
576+ } else {
577+ fd = ca_chunk_file_open (chunk_fd , prefix , chunkid , ca_compressed_chunk_suffix (), O_RDONLY |O_CLOEXEC |O_NOCTTY |O_NOFOLLOW );
578+ fd_compression = CA_CHUNK_COMPRESSED ;
579+ }
580+
581+ if (fd == - ELOOP ) /* If it's a symlink, then it's marked as "missing" */
582+ return - EADDRNOTAVAIL ;
583+
573584 if (fd < 0 ) {
574- if (fd == - ELOOP ) /* If it's a symlink, then it's marked as "missing" */
575- return - EADDRNOTAVAIL ;
576585 if (fd != - ENOENT )
577586 return fd ;
578587
579- fd = ca_chunk_file_open (chunk_fd , prefix , chunkid , ca_compressed_chunk_suffix (), O_RDONLY |O_CLOEXEC |O_NOCTTY |O_NOFOLLOW );
580- if (fd == - ELOOP )
588+ if (desired_compression == CA_CHUNK_UNCOMPRESSED ) {
589+ fd = ca_chunk_file_open (chunk_fd , prefix , chunkid , ca_compressed_chunk_suffix (), O_RDONLY |O_CLOEXEC |O_NOCTTY |O_NOFOLLOW );
590+ fd_compression = CA_CHUNK_COMPRESSED ;
591+ } else {
592+ fd = ca_chunk_file_open (chunk_fd , prefix , chunkid , NULL , O_RDONLY |O_CLOEXEC |O_NOCTTY |O_NOFOLLOW );
593+ fd_compression = CA_CHUNK_UNCOMPRESSED ;
594+ }
595+
596+ if (fd == - ELOOP ) /* If it's a symlink, then it's marked as "missing" */
581597 return - EADDRNOTAVAIL ;
582598 if (fd < 0 )
583599 return fd ;
600+ }
584601
585- if (desired_compression == CA_CHUNK_UNCOMPRESSED )
586- r = ca_load_and_decompress_fd (fd , buffer );
587- else
588- r = ca_load_fd (fd , buffer );
589- if (r < 0 )
590- return r ;
591602
592- if (ret_effective_compression )
593- * ret_effective_compression = desired_compression == CA_CHUNK_AS_IS ? CA_CHUNK_COMPRESSED : desired_compression ;
603+ if (desired_compression == CA_CHUNK_UNCOMPRESSED && fd_compression == CA_CHUNK_COMPRESSED )
604+ r = ca_load_and_decompress_fd (fd , buffer );
605+ else if (desired_compression == CA_CHUNK_COMPRESSED && fd_compression == CA_CHUNK_UNCOMPRESSED )
606+ r = ca_load_and_compress_fd (fd , compression_type , buffer );
607+ else
608+ r = ca_load_fd (fd , buffer );
594609
595- } else {
596- if (desired_compression == CA_CHUNK_COMPRESSED )
597- r = ca_load_and_compress_fd (fd , compression_type , buffer );
598- else
599- r = ca_load_fd (fd , buffer );
600- if (r < 0 )
601- return r ;
610+ if (r < 0 )
611+ return r ;
602612
603- if (ret_effective_compression )
604- * ret_effective_compression = desired_compression == CA_CHUNK_AS_IS ? CA_CHUNK_UNCOMPRESSED : desired_compression ;
605- }
613+ if (ret_effective_compression )
614+ * ret_effective_compression = desired_compression == CA_CHUNK_AS_IS ? fd_compression : desired_compression ;
606615
607616 return 0 ;
608617}
0 commit comments