<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>250495</bug_id>
          
          <creation_ts>2023-01-11 19:57:04 -0800</creation_ts>
          <short_desc>Data written via FileSystemSyncAccessHandle disappears after creating new FileSystemFileHandle</short_desc>
          <delta_ts>2023-01-13 09:29:51 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Website Storage</component>
          <version>Safari 16</version>
          <rep_platform>All</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>BrowserCompat, InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Alex Titarenko">03_placid_daft</reporter>
          <assigned_to name="Sihui Liu">sihui_liu</assigned_to>
          <cc>karlcow</cc>
    
    <cc>sihui_liu</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1924972</commentid>
    <comment_count>0</comment_count>
    <who name="Alex Titarenko">03_placid_daft</who>
    <bug_when>2023-01-11 19:57:04 -0800</bug_when>
    <thetext>With many attempts, I could not find a way to persist written data to FileSystemSyncAccessHandle.
The example from https://webkit.org/blog/12257/the-file-system-access-api-with-origin-private-file-system/ article works only if you try to getFile/read data from the same instance of FileSystemFileHandle, when you get a new instance of FileHandle all data disappears.

This can be easily reproduced with a slight modification to the existing LayoutTest at: https://github.com/WebKit/WebKit/blob/3729059e6a040c8c168679975c9d04dab48e5a4d/LayoutTests/storage/filesystemaccess/resources/file-handle-getfile.js#L44

If before `fileObject = await fileHandle.getFile();` line you place `fileHandle = await rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;, { &quot;create&quot; : false });` you will not get any actual data in return, even if you add accessHandle.flush() call before accessHandle.close();

This works perfectly fine in Chrome but not in WebKit.

FileSystemSyncAccessHandle is the only available way to write data in WebKit but it&apos;s not working because it does not persist data.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1924975</commentid>
    <comment_count>1</comment_count>
    <who name="Alex Titarenko">03_placid_daft</who>
    <bug_when>2023-01-11 20:22:13 -0800</bug_when>
    <thetext>More clear information on how to reproduce:

In file:
https://github.com/WebKit/WebKit/blob/main/LayoutTests/storage/filesystemaccess/resources/file-handle-getfile.js

replace test function with:

```
async function test() 
{
    try {
        var rootHandle = await navigator.storage.getDirectory();
        // Create a new file for this test.
        await rootHandle.removeEntry(&quot;file-handle-getfile.txt&quot;).then(() =&gt; { }, () =&gt; { });
        fileHandle = await rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;, { &quot;create&quot; : true });
        fileContent = &quot;&quot;;
    
        // Write file content in worker.
        if (typeof WorkerGlobalScope !== &apos;undefined&apos; &amp;&amp; self instanceof WorkerGlobalScope) {
            console.log(&apos;hello from WebWorker&apos;)
            accessHandle = await fileHandle.createSyncAccessHandle();
            const encoder = new TextEncoder();
            fileContent = &quot;This is a test.&quot;;
            writeBuffer = encoder.encode(fileContent);
            writeSize = accessHandle.write(writeBuffer, { &quot;at&quot; : 0 });
            shouldBe(&quot;writeSize&quot;, &quot;writeBuffer.byteLength&quot;);
            accessHandle.flush(); // This is a new line
            accessHandle.close();
        }

        fileHandle = await rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;); // This is a new line
        fileObject = await fileHandle.getFile();
        readText = await read(fileObject);
        shouldBe(&quot;readText&quot;, &quot;fileContent&quot;);

        finishTest();
    } catch (error) {
        finishTest(error.toString());
    }
}
```

Then run LayoutTest:
https://github.com/WebKit/WebKit/blob/main/LayoutTests/storage/filesystemaccess/file-handle-getfile-worker.html</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925099</commentid>
    <comment_count>2</comment_count>
    <who name="Sihui Liu">sihui_liu</who>
    <bug_when>2023-01-12 11:26:36 -0800</bug_when>
    <thetext>(In reply to Alex Titarenko from comment #1)
&gt; More clear information on how to reproduce:
&gt; 
&gt; In file:
&gt; https://github.com/WebKit/WebKit/blob/main/LayoutTests/storage/
&gt; filesystemaccess/resources/file-handle-getfile.js
&gt; 
&gt; replace test function with:
&gt; 
&gt; ```
&gt; async function test() 
&gt; {
&gt;     try {
&gt;         var rootHandle = await navigator.storage.getDirectory();
&gt;         // Create a new file for this test.
&gt;         await rootHandle.removeEntry(&quot;file-handle-getfile.txt&quot;).then(() =&gt; {
&gt; }, () =&gt; { });
&gt;         fileHandle = await
&gt; rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;, { &quot;create&quot; : true });
&gt;         fileContent = &quot;&quot;;
&gt;     
&gt;         // Write file content in worker.
&gt;         if (typeof WorkerGlobalScope !== &apos;undefined&apos; &amp;&amp; self instanceof
&gt; WorkerGlobalScope) {
&gt;             console.log(&apos;hello from WebWorker&apos;)
&gt;             accessHandle = await fileHandle.createSyncAccessHandle();
&gt;             const encoder = new TextEncoder();
&gt;             fileContent = &quot;This is a test.&quot;;
&gt;             writeBuffer = encoder.encode(fileContent);
&gt;             writeSize = accessHandle.write(writeBuffer, { &quot;at&quot; : 0 });
&gt;             shouldBe(&quot;writeSize&quot;, &quot;writeBuffer.byteLength&quot;);
&gt;             accessHandle.flush(); // This is a new line
&gt;             accessHandle.close();
&gt;         }
&gt; 
&gt;         fileHandle = await
&gt; rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;); // This is a new line
&gt;         fileObject = await fileHandle.getFile();
&gt;         readText = await read(fileObject);
&gt;         shouldBe(&quot;readText&quot;, &quot;fileContent&quot;);
&gt; 
&gt;         finishTest();
&gt;     } catch (error) {
&gt;         finishTest(error.toString());
&gt;     }
&gt; }
&gt; ```
&gt; 
&gt; Then run LayoutTest:
&gt; https://github.com/WebKit/WebKit/blob/main/LayoutTests/storage/
&gt; filesystemaccess/file-handle-getfile-worker.html</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925100</commentid>
    <comment_count>3</comment_count>
    <who name="Sihui Liu">sihui_liu</who>
    <bug_when>2023-01-12 11:28:12 -0800</bug_when>
    <thetext>(In reply to Alex Titarenko from comment #1)
&gt; More clear information on how to reproduce:
&gt; 
&gt; In file:
&gt; https://github.com/WebKit/WebKit/blob/main/LayoutTests/storage/
&gt; filesystemaccess/resources/file-handle-getfile.js
&gt; 
&gt; replace test function with:
&gt; 
&gt; ```
&gt; async function test() 
&gt; {
&gt;     try {
&gt;         var rootHandle = await navigator.storage.getDirectory();
&gt;         // Create a new file for this test.
&gt;         await rootHandle.removeEntry(&quot;file-handle-getfile.txt&quot;).then(() =&gt; {
&gt; }, () =&gt; { });
&gt;         fileHandle = await
&gt; rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;, { &quot;create&quot; : true });
&gt;         fileContent = &quot;&quot;;
&gt;     
&gt;         // Write file content in worker.
&gt;         if (typeof WorkerGlobalScope !== &apos;undefined&apos; &amp;&amp; self instanceof
&gt; WorkerGlobalScope) {
&gt;             console.log(&apos;hello from WebWorker&apos;)
&gt;             accessHandle = await fileHandle.createSyncAccessHandle();
&gt;             const encoder = new TextEncoder();
&gt;             fileContent = &quot;This is a test.&quot;;
&gt;             writeBuffer = encoder.encode(fileContent);
&gt;             writeSize = accessHandle.write(writeBuffer, { &quot;at&quot; : 0 });
&gt;             shouldBe(&quot;writeSize&quot;, &quot;writeBuffer.byteLength&quot;);
&gt;             accessHandle.flush(); // This is a new line
&gt;             accessHandle.close();
&gt;         }
&gt; 
&gt;         fileHandle = await
&gt; rootHandle.getFileHandle(&quot;file-handle-getfile.txt&quot;); // This is a new line
&gt;         fileObject = await fileHandle.getFile();
&gt;         readText = await read(fileObject);
&gt;         shouldBe(&quot;readText&quot;, &quot;fileContent&quot;);
&gt; 
&gt;         finishTest();
&gt;     } catch (error) {
&gt;         finishTest(error.toString());
&gt;     }
&gt; }
&gt; ```
&gt; 
&gt; Then run LayoutTest:
&gt; https://github.com/WebKit/WebKit/blob/main/LayoutTests/storage/
&gt; filesystemaccess/file-handle-getfile-worker.html

Thanks for the report. I think it&apos;s caused by file being truncated unexpectedly on FileSystemHandle creation. Will make a fix.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925101</commentid>
    <comment_count>4</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2023-01-12 11:29:25 -0800</bug_when>
    <thetext>&lt;rdar://problem/104187327&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925102</commentid>
    <comment_count>5</comment_count>
    <who name="Sihui Liu">sihui_liu</who>
    <bug_when>2023-01-12 11:32:28 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/8586</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925369</commentid>
    <comment_count>6</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2023-01-13 09:29:48 -0800</bug_when>
    <thetext>Committed 258876@main (60a6090b4106): &lt;https://commits.webkit.org/258876@main&gt;

Reviewed commits have been landed. Closing PR #8586 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>