How to check file is writable (resource is not busy nor locked)












0















excel4node's write to file function catches error and does not propagate to a caller. Therefore, my app cannot determine whether write to file is successful or not.



My current workaround is like below:



let fs = require('fs')
try {
let filePath = 'blahblah'
fs.writeFileSync(filePath, '') // Try-catch is for this statement
excel4nodeWorkbook.write(filePath)
} catch (e) {
console.log('File save is not successful')
}


It works, but I think it's a sort of hack and that it's not a semantically correct way. I also testedfs.access and fs.accessSync, but they only check permission, not the state (busy/lock) of resource.



Is there any suggestion for this to look and behave nicer without modifying excel4node source code?










share|improve this question



























    0















    excel4node's write to file function catches error and does not propagate to a caller. Therefore, my app cannot determine whether write to file is successful or not.



    My current workaround is like below:



    let fs = require('fs')
    try {
    let filePath = 'blahblah'
    fs.writeFileSync(filePath, '') // Try-catch is for this statement
    excel4nodeWorkbook.write(filePath)
    } catch (e) {
    console.log('File save is not successful')
    }


    It works, but I think it's a sort of hack and that it's not a semantically correct way. I also testedfs.access and fs.accessSync, but they only check permission, not the state (busy/lock) of resource.



    Is there any suggestion for this to look and behave nicer without modifying excel4node source code?










    share|improve this question

























      0












      0








      0








      excel4node's write to file function catches error and does not propagate to a caller. Therefore, my app cannot determine whether write to file is successful or not.



      My current workaround is like below:



      let fs = require('fs')
      try {
      let filePath = 'blahblah'
      fs.writeFileSync(filePath, '') // Try-catch is for this statement
      excel4nodeWorkbook.write(filePath)
      } catch (e) {
      console.log('File save is not successful')
      }


      It works, but I think it's a sort of hack and that it's not a semantically correct way. I also testedfs.access and fs.accessSync, but they only check permission, not the state (busy/lock) of resource.



      Is there any suggestion for this to look and behave nicer without modifying excel4node source code?










      share|improve this question














      excel4node's write to file function catches error and does not propagate to a caller. Therefore, my app cannot determine whether write to file is successful or not.



      My current workaround is like below:



      let fs = require('fs')
      try {
      let filePath = 'blahblah'
      fs.writeFileSync(filePath, '') // Try-catch is for this statement
      excel4nodeWorkbook.write(filePath)
      } catch (e) {
      console.log('File save is not successful')
      }


      It works, but I think it's a sort of hack and that it's not a semantically correct way. I also testedfs.access and fs.accessSync, but they only check permission, not the state (busy/lock) of resource.



      Is there any suggestion for this to look and behave nicer without modifying excel4node source code?







      node.js file






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 14:12









      JeonJeon

      1,93731859




      1,93731859
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You already marked a line in the library's source code. If you look a few lines above, you can see it uses the handler argument to pass any errors to. In fact, peeking at the documentation comment above the function, it says:




          If callback is given, callback called with (err, fs.Stats) passed




          Hence you can simply pass a function as your second argument and check for err like you've probably already seen elsewhere in the node environment:



          excel4nodeWorkbook.write(filepath, (err) => {
          if (err) {
          console.error(err);
          }
          });





          share|improve this answer



















          • 1





            Oh, that's my fault. I didn't look the source code carefully. Thanks.

            – Jeon
            Jan 1 at 14:33



















          1














          I think you are asking the wrong question. If you check at time T, then write at time T + 1ms, what would guarantee that the file is still writeable?



          If the file is not writeable for whatever reason, the write will fail, period. Nothing to do. Your code is fine, but you can probably also do without the fs.writeFileSync(), which will just erase whatever else was in the file before.



          You can also write to a randomly-generated file path to make reasonably sure that two processes are not writing to the same file at the same time, but again, that will not prevent all possible write errors, so what you really, really want is rather some good error handling.



          In order to handle errors properly you have to provide a callback!



          Something along the lines of:



          excel4nodeWorkbook.write(filePath, (err) => {
          if (err) console.error(err);
          });


          Beware, this is asynchronous code, so you need to handle that as well!






          share|improve this answer


























          • I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

            – Jeon
            Jan 1 at 14:23








          • 1





            check updated answer ;)

            – frsechet
            Jan 1 at 14:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996160%2fhow-to-check-file-is-writable-resource-is-not-busy-nor-locked%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You already marked a line in the library's source code. If you look a few lines above, you can see it uses the handler argument to pass any errors to. In fact, peeking at the documentation comment above the function, it says:




          If callback is given, callback called with (err, fs.Stats) passed




          Hence you can simply pass a function as your second argument and check for err like you've probably already seen elsewhere in the node environment:



          excel4nodeWorkbook.write(filepath, (err) => {
          if (err) {
          console.error(err);
          }
          });





          share|improve this answer



















          • 1





            Oh, that's my fault. I didn't look the source code carefully. Thanks.

            – Jeon
            Jan 1 at 14:33
















          1














          You already marked a line in the library's source code. If you look a few lines above, you can see it uses the handler argument to pass any errors to. In fact, peeking at the documentation comment above the function, it says:




          If callback is given, callback called with (err, fs.Stats) passed




          Hence you can simply pass a function as your second argument and check for err like you've probably already seen elsewhere in the node environment:



          excel4nodeWorkbook.write(filepath, (err) => {
          if (err) {
          console.error(err);
          }
          });





          share|improve this answer



















          • 1





            Oh, that's my fault. I didn't look the source code carefully. Thanks.

            – Jeon
            Jan 1 at 14:33














          1












          1








          1







          You already marked a line in the library's source code. If you look a few lines above, you can see it uses the handler argument to pass any errors to. In fact, peeking at the documentation comment above the function, it says:




          If callback is given, callback called with (err, fs.Stats) passed




          Hence you can simply pass a function as your second argument and check for err like you've probably already seen elsewhere in the node environment:



          excel4nodeWorkbook.write(filepath, (err) => {
          if (err) {
          console.error(err);
          }
          });





          share|improve this answer













          You already marked a line in the library's source code. If you look a few lines above, you can see it uses the handler argument to pass any errors to. In fact, peeking at the documentation comment above the function, it says:




          If callback is given, callback called with (err, fs.Stats) passed




          Hence you can simply pass a function as your second argument and check for err like you've probably already seen elsewhere in the node environment:



          excel4nodeWorkbook.write(filepath, (err) => {
          if (err) {
          console.error(err);
          }
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 14:27









          KiruseKiruse

          885




          885








          • 1





            Oh, that's my fault. I didn't look the source code carefully. Thanks.

            – Jeon
            Jan 1 at 14:33














          • 1





            Oh, that's my fault. I didn't look the source code carefully. Thanks.

            – Jeon
            Jan 1 at 14:33








          1




          1





          Oh, that's my fault. I didn't look the source code carefully. Thanks.

          – Jeon
          Jan 1 at 14:33





          Oh, that's my fault. I didn't look the source code carefully. Thanks.

          – Jeon
          Jan 1 at 14:33













          1














          I think you are asking the wrong question. If you check at time T, then write at time T + 1ms, what would guarantee that the file is still writeable?



          If the file is not writeable for whatever reason, the write will fail, period. Nothing to do. Your code is fine, but you can probably also do without the fs.writeFileSync(), which will just erase whatever else was in the file before.



          You can also write to a randomly-generated file path to make reasonably sure that two processes are not writing to the same file at the same time, but again, that will not prevent all possible write errors, so what you really, really want is rather some good error handling.



          In order to handle errors properly you have to provide a callback!



          Something along the lines of:



          excel4nodeWorkbook.write(filePath, (err) => {
          if (err) console.error(err);
          });


          Beware, this is asynchronous code, so you need to handle that as well!






          share|improve this answer


























          • I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

            – Jeon
            Jan 1 at 14:23








          • 1





            check updated answer ;)

            – frsechet
            Jan 1 at 14:32
















          1














          I think you are asking the wrong question. If you check at time T, then write at time T + 1ms, what would guarantee that the file is still writeable?



          If the file is not writeable for whatever reason, the write will fail, period. Nothing to do. Your code is fine, but you can probably also do without the fs.writeFileSync(), which will just erase whatever else was in the file before.



          You can also write to a randomly-generated file path to make reasonably sure that two processes are not writing to the same file at the same time, but again, that will not prevent all possible write errors, so what you really, really want is rather some good error handling.



          In order to handle errors properly you have to provide a callback!



          Something along the lines of:



          excel4nodeWorkbook.write(filePath, (err) => {
          if (err) console.error(err);
          });


          Beware, this is asynchronous code, so you need to handle that as well!






          share|improve this answer


























          • I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

            – Jeon
            Jan 1 at 14:23








          • 1





            check updated answer ;)

            – frsechet
            Jan 1 at 14:32














          1












          1








          1







          I think you are asking the wrong question. If you check at time T, then write at time T + 1ms, what would guarantee that the file is still writeable?



          If the file is not writeable for whatever reason, the write will fail, period. Nothing to do. Your code is fine, but you can probably also do without the fs.writeFileSync(), which will just erase whatever else was in the file before.



          You can also write to a randomly-generated file path to make reasonably sure that two processes are not writing to the same file at the same time, but again, that will not prevent all possible write errors, so what you really, really want is rather some good error handling.



          In order to handle errors properly you have to provide a callback!



          Something along the lines of:



          excel4nodeWorkbook.write(filePath, (err) => {
          if (err) console.error(err);
          });


          Beware, this is asynchronous code, so you need to handle that as well!






          share|improve this answer















          I think you are asking the wrong question. If you check at time T, then write at time T + 1ms, what would guarantee that the file is still writeable?



          If the file is not writeable for whatever reason, the write will fail, period. Nothing to do. Your code is fine, but you can probably also do without the fs.writeFileSync(), which will just erase whatever else was in the file before.



          You can also write to a randomly-generated file path to make reasonably sure that two processes are not writing to the same file at the same time, but again, that will not prevent all possible write errors, so what you really, really want is rather some good error handling.



          In order to handle errors properly you have to provide a callback!



          Something along the lines of:



          excel4nodeWorkbook.write(filePath, (err) => {
          if (err) console.error(err);
          });


          Beware, this is asynchronous code, so you need to handle that as well!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 14:31

























          answered Jan 1 at 14:15









          frsechetfrsechet

          312312




          312312













          • I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

            – Jeon
            Jan 1 at 14:23








          • 1





            check updated answer ;)

            – frsechet
            Jan 1 at 14:32



















          • I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

            – Jeon
            Jan 1 at 14:23








          • 1





            check updated answer ;)

            – frsechet
            Jan 1 at 14:32

















          I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

          – Jeon
          Jan 1 at 14:23







          I know it's not guaranteed at T and T+1, so I called it a sort of hack. And console.log(err) is just an example for try-catch. Point is whether there is less ugly solution than writing zero string for resource test.

          – Jeon
          Jan 1 at 14:23






          1




          1





          check updated answer ;)

          – frsechet
          Jan 1 at 14:32





          check updated answer ;)

          – frsechet
          Jan 1 at 14:32


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996160%2fhow-to-check-file-is-writable-resource-is-not-busy-nor-locked%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith