@@ -2328,3 +2328,189 @@ TEST_IMPL(fs_write_multiple_bufs) {
23282328 MAKE_VALGRIND_HAPPY ();
23292329 return 0 ;
23302330}
2331+
2332+
2333+ TEST_IMPL (fs_write_alotof_bufs ) {
2334+ const size_t iovcount = 54321 ;
2335+ uv_buf_t * iovs ;
2336+ char * buffer ;
2337+ size_t index ;
2338+ int r ;
2339+
2340+ /* Setup. */
2341+ unlink ("test_file" );
2342+
2343+ loop = uv_default_loop ();
2344+
2345+ iovs = malloc (sizeof (* iovs ) * iovcount );
2346+ ASSERT (iovs != NULL );
2347+
2348+ r = uv_fs_open (loop ,
2349+ & open_req1 ,
2350+ "test_file" ,
2351+ O_RDWR | O_CREAT ,
2352+ S_IWUSR | S_IRUSR ,
2353+ NULL );
2354+ ASSERT (r >= 0 );
2355+ ASSERT (open_req1 .result >= 0 );
2356+ uv_fs_req_cleanup (& open_req1 );
2357+
2358+ for (index = 0 ; index < iovcount ; ++ index )
2359+ iovs [index ] = uv_buf_init (test_buf , sizeof (test_buf ));
2360+
2361+ r = uv_fs_write (loop ,
2362+ & write_req ,
2363+ open_req1 .result ,
2364+ iovs ,
2365+ iovcount ,
2366+ -1 ,
2367+ NULL );
2368+ ASSERT (r >= 0 );
2369+ ASSERT ((size_t )write_req .result == sizeof (test_buf ) * iovcount );
2370+ uv_fs_req_cleanup (& write_req );
2371+
2372+ /* Read the strings back to separate buffers. */
2373+ buffer = malloc (sizeof (test_buf ) * iovcount );
2374+ ASSERT (buffer != NULL );
2375+
2376+ for (index = 0 ; index < iovcount ; ++ index )
2377+ iovs [index ] = uv_buf_init (buffer + index * sizeof (test_buf ),
2378+ sizeof (test_buf ));
2379+
2380+ r = uv_fs_read (loop , & read_req , open_req1 .result , iovs , iovcount , 0 , NULL );
2381+ ASSERT (r >= 0 );
2382+ ASSERT ((size_t )read_req .result == sizeof (test_buf ) * iovcount );
2383+
2384+ for (index = 0 ; index < iovcount ; ++ index )
2385+ ASSERT (strncmp (buffer + index * sizeof (test_buf ),
2386+ test_buf ,
2387+ sizeof (test_buf )) == 0 );
2388+
2389+ uv_fs_req_cleanup (& read_req );
2390+ free (buffer );
2391+
2392+ iov = uv_buf_init (buf , sizeof (buf ));
2393+ r = uv_fs_read (loop ,
2394+ & read_req ,
2395+ open_req1 .result ,
2396+ & iov ,
2397+ 1 ,
2398+ read_req .result ,
2399+ NULL );
2400+ ASSERT (r == 0 );
2401+ ASSERT (read_req .result == 0 );
2402+ uv_fs_req_cleanup (& read_req );
2403+
2404+ r = uv_fs_close (loop , & close_req , open_req1 .result , NULL );
2405+ ASSERT (r == 0 );
2406+ ASSERT (close_req .result == 0 );
2407+ uv_fs_req_cleanup (& close_req );
2408+
2409+ /* Cleanup */
2410+ unlink ("test_file" );
2411+ free (iovs );
2412+
2413+ MAKE_VALGRIND_HAPPY ();
2414+ return 0 ;
2415+ }
2416+
2417+
2418+ TEST_IMPL (fs_write_alotof_bufs_with_offset ) {
2419+ const size_t iovcount = 54321 ;
2420+ uv_buf_t * iovs ;
2421+ char * buffer ;
2422+ size_t index ;
2423+ int r ;
2424+ int64_t offset ;
2425+ char * filler = "0123456789" ;
2426+ int filler_len = strlen (filler );
2427+
2428+ /* Setup. */
2429+ unlink ("test_file" );
2430+
2431+ loop = uv_default_loop ();
2432+
2433+ iovs = malloc (sizeof (* iovs ) * iovcount );
2434+ ASSERT (iovs != NULL );
2435+
2436+ r = uv_fs_open (loop ,
2437+ & open_req1 ,
2438+ "test_file" ,
2439+ O_RDWR | O_CREAT ,
2440+ S_IWUSR | S_IRUSR ,
2441+ NULL );
2442+ ASSERT (r >= 0 );
2443+ ASSERT (open_req1 .result >= 0 );
2444+ uv_fs_req_cleanup (& open_req1 );
2445+
2446+ iov = uv_buf_init (filler , filler_len );
2447+ r = uv_fs_write (loop , & write_req , open_req1 .result , & iov , 1 , -1 , NULL );
2448+ ASSERT (r == filler_len );
2449+ ASSERT (write_req .result == filler_len );
2450+ uv_fs_req_cleanup (& write_req );
2451+ offset = (int64_t )r ;
2452+
2453+ for (index = 0 ; index < iovcount ; ++ index )
2454+ iovs [index ] = uv_buf_init (test_buf , sizeof (test_buf ));
2455+
2456+ r = uv_fs_write (loop ,
2457+ & write_req ,
2458+ open_req1 .result ,
2459+ iovs ,
2460+ iovcount ,
2461+ offset ,
2462+ NULL );
2463+ ASSERT (r >= 0 );
2464+ ASSERT ((size_t )write_req .result == sizeof (test_buf ) * iovcount );
2465+ uv_fs_req_cleanup (& write_req );
2466+
2467+ /* Read the strings back to separate buffers. */
2468+ buffer = malloc (sizeof (test_buf ) * iovcount );
2469+ ASSERT (buffer != NULL );
2470+
2471+ for (index = 0 ; index < iovcount ; ++ index )
2472+ iovs [index ] = uv_buf_init (buffer + index * sizeof (test_buf ),
2473+ sizeof (test_buf ));
2474+
2475+ r = uv_fs_read (loop , & read_req , open_req1 .result , iovs , iovcount , offset , NULL );
2476+ ASSERT (r >= 0 );
2477+ ASSERT (read_req .result == sizeof (test_buf ) * iovcount );
2478+
2479+ for (index = 0 ; index < iovcount ; ++ index )
2480+ ASSERT (strncmp (buffer + index * sizeof (test_buf ),
2481+ test_buf ,
2482+ sizeof (test_buf )) == 0 );
2483+
2484+ uv_fs_req_cleanup (& read_req );
2485+ free (buffer );
2486+
2487+ r = uv_fs_stat (loop , & stat_req , "test_file" , NULL );
2488+ ASSERT (r == 0 );
2489+ ASSERT ((int64_t )((uv_stat_t * )stat_req .ptr )-> st_size ==
2490+ offset + (int64_t )(iovcount * sizeof (test_buf )));
2491+ uv_fs_req_cleanup (& stat_req );
2492+
2493+ iov = uv_buf_init (buf , sizeof (buf ));
2494+ r = uv_fs_read (loop ,
2495+ & read_req ,
2496+ open_req1 .result ,
2497+ & iov ,
2498+ 1 ,
2499+ read_req .result + offset ,
2500+ NULL );
2501+ ASSERT (r == 0 );
2502+ ASSERT (read_req .result == 0 );
2503+ uv_fs_req_cleanup (& read_req );
2504+
2505+ r = uv_fs_close (loop , & close_req , open_req1 .result , NULL );
2506+ ASSERT (r == 0 );
2507+ ASSERT (close_req .result == 0 );
2508+ uv_fs_req_cleanup (& close_req );
2509+
2510+ /* Cleanup */
2511+ unlink ("test_file" );
2512+ free (iovs );
2513+
2514+ MAKE_VALGRIND_HAPPY ();
2515+ return 0 ;
2516+ }
0 commit comments