Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt fails to copy file on ubuntu.
Forum Updated to NodeBB v4.3 + New Features

Qt fails to copy file on ubuntu.

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 5.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qingshui Kong
    wrote on 11 Jan 2019, 09:55 last edited by
    #1

    Hello everybody,

    Could somebody give me some advis?

    I want to copy file to U-storage. But the file on the destination directory is alway empty.

    I hope somebody will help me to solve this problem this time.

    Thanks in advance.

    My enviroment:
    Ubuntu 16.04
    0_1547200457166_7ea4ce92-1f6a-4060-9f27-888da8ff92c4-image.png
    0_1547200466241_470fe906-f7df-4a74-ac86-b120724d8f40-image.png

    J Q 2 Replies Last reply 11 Jan 2019, 09:59
    0
    • Q Qingshui Kong
      11 Jan 2019, 09:55

      Hello everybody,

      Could somebody give me some advis?

      I want to copy file to U-storage. But the file on the destination directory is alway empty.

      I hope somebody will help me to solve this problem this time.

      Thanks in advance.

      My enviroment:
      Ubuntu 16.04
      0_1547200457166_7ea4ce92-1f6a-4060-9f27-888da8ff92c4-image.png
      0_1547200466241_470fe906-f7df-4a74-ac86-b120724d8f40-image.png

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 11 Jan 2019, 09:59 last edited by
      #2

      @Qingshui-Kong said in Qt fails to copy file on ubuntu.:

      U-storage

      what is U-storage?

      Please post your code, else it is impossible to say why it is not working...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 11 Jan 2019, 11:04
      1
      • J jsulm
        11 Jan 2019, 09:59

        @Qingshui-Kong said in Qt fails to copy file on ubuntu.:

        U-storage

        what is U-storage?

        Please post your code, else it is impossible to say why it is not working...

        Q Offline
        Q Offline
        Qingshui Kong
        wrote on 11 Jan 2019, 11:04 last edited by
        #3

        @jsulm Thank you.

        I mean USB Flash Disk.
        0_1547204495238_4a231dfa-086a-4396-bc3c-b99b4a03d1ab-image.png
        No matter I use QFile::copy or system, it have the same result. It's often empty.

        I want to copy it to a USB Flash Disk, and open it on a different computer.

        J J 2 Replies Last reply 11 Jan 2019, 11:35
        0
        • Q Qingshui Kong
          11 Jan 2019, 11:04

          @jsulm Thank you.

          I mean USB Flash Disk.
          0_1547204495238_4a231dfa-086a-4396-bc3c-b99b4a03d1ab-image.png
          No matter I use QFile::copy or system, it have the same result. It's often empty.

          I want to copy it to a USB Flash Disk, and open it on a different computer.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 11 Jan 2019, 11:35 last edited by
          #4

          @Qingshui-Kong Does it work if you do it manually in a terminal? Just to make sure it is not an issue with access rights.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply 11 Jan 2019, 12:00
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 11 Jan 2019, 11:56 last edited by
            #5

            Hi
            Also, just to be 100% sure.
            You do unmount the USB stick after copy to it?
            From the File Manager.

            Q 1 Reply Last reply 11 Jan 2019, 12:03
            0
            • J jsulm
              11 Jan 2019, 11:35

              @Qingshui-Kong Does it work if you do it manually in a terminal? Just to make sure it is not an issue with access rights.

              Q Offline
              Q Offline
              Qingshui Kong
              wrote on 11 Jan 2019, 12:00 last edited by
              #6

              @jsulm Yes, it works, if I do it manually in a terminal.

              1 Reply Last reply
              0
              • M mrjj
                11 Jan 2019, 11:56

                Hi
                Also, just to be 100% sure.
                You do unmount the USB stick after copy to it?
                From the File Manager.

                Q Offline
                Q Offline
                Qingshui Kong
                wrote on 11 Jan 2019, 12:03 last edited by
                #7

                @mrjj No, I don't unmount it. Even I do it manunally in a terminal, I don't unmount it.
                Because the my software is fullscreen, I can't unmount it.

                1 Reply Last reply
                0
                • Q Qingshui Kong
                  11 Jan 2019, 11:04

                  @jsulm Thank you.

                  I mean USB Flash Disk.
                  0_1547204495238_4a231dfa-086a-4396-bc3c-b99b4a03d1ab-image.png
                  No matter I use QFile::copy or system, it have the same result. It's often empty.

                  I want to copy it to a USB Flash Disk, and open it on a different computer.

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Jan 2019, 12:40 last edited by JonB 1 Nov 2019, 12:43
                  #8

                  @Qingshui-Kong

                  • If behaviour happens with system() as well then it is not a Qt/QFile::copy() issue.

                  • You don't check the return results of system() & QFile::copy().

                  • You have read the QFile::copy() docs where it says:

                  Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).

                  haven't you?

                  • Do you know it's empty via ls -l or via cat?

                  • The fact that you say "It's often empty" implies to me it could be a timing issue. How long do you allow to elapse between doing the copy and trying to access the data?

                  • I find it hard to believe that system("cp ...") does not work but doing the cp from a shell does....

                  • If you really find you have to, have a look at man sync for forcing a flush.

                  Q 2 Replies Last reply 11 Jan 2019, 14:42
                  3
                  • J JonB
                    11 Jan 2019, 12:40

                    @Qingshui-Kong

                    • If behaviour happens with system() as well then it is not a Qt/QFile::copy() issue.

                    • You don't check the return results of system() & QFile::copy().

                    • You have read the QFile::copy() docs where it says:

                    Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).

                    haven't you?

                    • Do you know it's empty via ls -l or via cat?

                    • The fact that you say "It's often empty" implies to me it could be a timing issue. How long do you allow to elapse between doing the copy and trying to access the data?

                    • I find it hard to believe that system("cp ...") does not work but doing the cp from a shell does....

                    • If you really find you have to, have a look at man sync for forcing a flush.

                    Q Offline
                    Q Offline
                    Qingshui Kong
                    wrote on 11 Jan 2019, 14:42 last edited by
                    #9

                    @JonB Thank you. I will check it.

                    1 Reply Last reply
                    0
                    • J JonB
                      11 Jan 2019, 12:40

                      @Qingshui-Kong

                      • If behaviour happens with system() as well then it is not a Qt/QFile::copy() issue.

                      • You don't check the return results of system() & QFile::copy().

                      • You have read the QFile::copy() docs where it says:

                      Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).

                      haven't you?

                      • Do you know it's empty via ls -l or via cat?

                      • The fact that you say "It's often empty" implies to me it could be a timing issue. How long do you allow to elapse between doing the copy and trying to access the data?

                      • I find it hard to believe that system("cp ...") does not work but doing the cp from a shell does....

                      • If you really find you have to, have a look at man sync for forcing a flush.

                      Q Offline
                      Q Offline
                      Qingshui Kong
                      wrote on 12 Jan 2019, 03:09 last edited by
                      #10

                      @JonB
                      Thank you very much!

                      I think I have solved the issue by using sync to force a flush.

                      Maybe you think it hard to believe that system("cp ...") does not work but doing the cp from a shell does....
                      But it is true. I tested more then ten times for each methods. But I don't know the reason. Could you tell me that, if you know?

                      Thanks again.

                      kshegunovK 1 Reply Last reply 12 Jan 2019, 03:48
                      0
                      • Q Qingshui Kong
                        12 Jan 2019, 03:09

                        @JonB
                        Thank you very much!

                        I think I have solved the issue by using sync to force a flush.

                        Maybe you think it hard to believe that system("cp ...") does not work but doing the cp from a shell does....
                        But it is true. I tested more then ten times for each methods. But I don't know the reason. Could you tell me that, if you know?

                        Thanks again.

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on 12 Jan 2019, 03:48 last edited by
                        #11

                        @Qingshui-Kong said in Qt fails to copy file on ubuntu.:

                        But I don't know the reason. Could you tell me that, if you know?

                        This is your hint:
                        @mrjj said in Qt fails to copy file on ubuntu.:

                        You do unmount the USB stick after copy to it?

                        On Linux you most often run extX kind of a filesystem, which is a journaling file system. This means that writes, moves and w/e else operations you do, don't happen synchronously (i.e. immediately when you issue it). You can think of the journal as kind of transaction processing. You want that because file operations are usually consisting of many small actual writes to the nonvolatile memory and if something interrupts any of them you don't want your filesystem to be gone for good. This however implies that the journal has to be flushed, and if you don't do it explicitly for many devices it is done implicitly when you umount them. Since you don't umount your USB, the journal is never flushed and no data is in actuality written on the stick. sync does exactly that - you explicitly flush the journal, thus even when you don't umount the device it does contain the actual data. As an advice: always unmount your devices.

                        Read and abide by the Qt Code of Conduct

                        Q 1 Reply Last reply 12 Jan 2019, 05:38
                        3
                        • kshegunovK kshegunov
                          12 Jan 2019, 03:48

                          @Qingshui-Kong said in Qt fails to copy file on ubuntu.:

                          But I don't know the reason. Could you tell me that, if you know?

                          This is your hint:
                          @mrjj said in Qt fails to copy file on ubuntu.:

                          You do unmount the USB stick after copy to it?

                          On Linux you most often run extX kind of a filesystem, which is a journaling file system. This means that writes, moves and w/e else operations you do, don't happen synchronously (i.e. immediately when you issue it). You can think of the journal as kind of transaction processing. You want that because file operations are usually consisting of many small actual writes to the nonvolatile memory and if something interrupts any of them you don't want your filesystem to be gone for good. This however implies that the journal has to be flushed, and if you don't do it explicitly for many devices it is done implicitly when you umount them. Since you don't umount your USB, the journal is never flushed and no data is in actuality written on the stick. sync does exactly that - you explicitly flush the journal, thus even when you don't umount the device it does contain the actual data. As an advice: always unmount your devices.

                          Q Offline
                          Q Offline
                          Qingshui Kong
                          wrote on 12 Jan 2019, 05:38 last edited by
                          #12

                          @kshegunov Thank you very much for telling me the details. Now I know the reason why I can solve the issue by using sync.

                          I can't umount the device, because my application is showing fullscreen. So I can't operate the system. I think I can add a function to my software to umount the device.

                          But I still wonder why doing the cp from a shell works even though I don't umount the device.

                          J 1 Reply Last reply 15 Jan 2019, 08:42
                          0
                          • Q Qingshui Kong
                            12 Jan 2019, 05:38

                            @kshegunov Thank you very much for telling me the details. Now I know the reason why I can solve the issue by using sync.

                            I can't umount the device, because my application is showing fullscreen. So I can't operate the system. I think I can add a function to my software to umount the device.

                            But I still wonder why doing the cp from a shell works even though I don't umount the device.

                            J Offline
                            J Offline
                            JonB
                            wrote on 15 Jan 2019, 08:42 last edited by JonB
                            #13

                            @Qingshui-Kong

                            Maybe you think it hard to believe that system("cp ...") does not work but doing the cp from a shell does....
                            But it is true. I tested more then ten times for each methods. But I don't know the reason. Could you tell me that, if you know?

                            But I still wonder why doing the cp from a shell works even though I don't umount the device.

                            Sorry, but I simply do not believe that sitting in a shell and executing a cp will behave any differently than sitting a program and executing system("cp ...")! (Assuming same permissions etc.) The shell itself is a program, and when it executes /bin/cp for you it will be using code not dissimilar (in terms of behaviour) from going system("cp").

                            You still did not put in the necessary error checking, and reading of stderr in case something appears there, when your code goes system("cp ...") with your arguments. So there is a chance that the cp you execute from your code is not doing what you think.

                            As for why cp works for you at all: there is a chance it executes a fsync(2) for you on this particular file, though frankly I would be surprised if it did.

                            J 1 Reply Last reply 15 Jan 2019, 08:49
                            0
                            • J JonB
                              15 Jan 2019, 08:42

                              @Qingshui-Kong

                              Maybe you think it hard to believe that system("cp ...") does not work but doing the cp from a shell does....
                              But it is true. I tested more then ten times for each methods. But I don't know the reason. Could you tell me that, if you know?

                              But I still wonder why doing the cp from a shell works even though I don't umount the device.

                              Sorry, but I simply do not believe that sitting in a shell and executing a cp will behave any differently than sitting a program and executing system("cp ...")! (Assuming same permissions etc.) The shell itself is a program, and when it executes /bin/cp for you it will be using code not dissimilar (in terms of behaviour) from going system("cp").

                              You still did not put in the necessary error checking, and reading of stderr in case something appears there, when your code goes system("cp ...") with your arguments. So there is a chance that the cp you execute from your code is not doing what you think.

                              As for why cp works for you at all: there is a chance it executes a fsync(2) for you on this particular file, though frankly I would be surprised if it did.

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 15 Jan 2019, 08:49 last edited by
                              #14

                              @JonB One another possibility for different behaviour: different environments in a terminal and when calling cp with system().

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J 1 Reply Last reply 15 Jan 2019, 12:29
                              0
                              • J jsulm
                                15 Jan 2019, 08:49

                                @JonB One another possibility for different behaviour: different environments in a terminal and when calling cp with system().

                                J Offline
                                J Offline
                                JonB
                                wrote on 15 Jan 2019, 12:29 last edited by JonB
                                #15

                                @jsulm
                                Possibly true, but can you name anything in the environment which is going to make a command-line-invoked cp behave the slightest bit differently wrt syncing to a USB device? Because I can't :) Just in case, perhaps the OP should test running his app from the terminal instead of from Qt Creator if that's what he's currently doing...

                                J 1 Reply Last reply 15 Jan 2019, 14:36
                                0
                                • J JonB
                                  15 Jan 2019, 12:29

                                  @jsulm
                                  Possibly true, but can you name anything in the environment which is going to make a command-line-invoked cp behave the slightest bit differently wrt syncing to a USB device? Because I can't :) Just in case, perhaps the OP should test running his app from the terminal instead of from Qt Creator if that's what he's currently doing...

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 15 Jan 2019, 14:36 last edited by
                                  #16

                                  @JonB I can't as well, this is simply something that could make a difference even if I can't say exactly what it could be.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 15 Jan 2019, 22:43 last edited by
                                    #17

                                    Hi,

                                    How are you handling the device ?
                                    Are you mounting manually ?
                                    Is your application responsible for that part too ?
                                    Do you have multiple devices and not writing on the same by accident ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    Q 1 Reply Last reply 17 Jan 2019, 07:52
                                    0
                                    • SGaistS SGaist
                                      15 Jan 2019, 22:43

                                      Hi,

                                      How are you handling the device ?
                                      Are you mounting manually ?
                                      Is your application responsible for that part too ?
                                      Do you have multiple devices and not writing on the same by accident ?

                                      Q Offline
                                      Q Offline
                                      Qingshui Kong
                                      wrote on 17 Jan 2019, 07:52 last edited by
                                      #18

                                      @SGaist As I said, I didn't mount or unmount manually.
                                      And I only have one ubuntu system and one USB disk.

                                      1 Reply Last reply
                                      0
                                      • Q Qingshui Kong
                                        11 Jan 2019, 09:55

                                        Hello everybody,

                                        Could somebody give me some advis?

                                        I want to copy file to U-storage. But the file on the destination directory is alway empty.

                                        I hope somebody will help me to solve this problem this time.

                                        Thanks in advance.

                                        My enviroment:
                                        Ubuntu 16.04
                                        0_1547200457166_7ea4ce92-1f6a-4060-9f27-888da8ff92c4-image.png
                                        0_1547200466241_470fe906-f7df-4a74-ac86-b120724d8f40-image.png

                                        Q Offline
                                        Q Offline
                                        Qingshui Kong
                                        wrote on 17 Jan 2019, 07:56 last edited by
                                        #19

                                        @jsulm @mrjj @JonB @kshegunov @SGaist
                                        Thank you all!
                                        I think maybe I should learn more knowledge about Linux system.

                                        J 1 Reply Last reply 17 Jan 2019, 08:37
                                        0
                                        • Q Qingshui Kong
                                          17 Jan 2019, 07:56

                                          @jsulm @mrjj @JonB @kshegunov @SGaist
                                          Thank you all!
                                          I think maybe I should learn more knowledge about Linux system.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 17 Jan 2019, 08:37 last edited by
                                          #20

                                          @Qingshui-Kong
                                          Yes and no! The question you are asking about the behaviour of a mounted USB drive, and seeming to need to either unmount or sync/fsync(), is actually a nasty area! I too would not be sure of behaviour, so it's very understandable that you are unsure :)

                                          Q 1 Reply Last reply 17 Jan 2019, 08:45
                                          0

                                          1/23

                                          11 Jan 2019, 09:55

                                          • Login

                                          • Login or register to search.
                                          1 out of 23
                                          • First post
                                            1/23
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved