Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:36:04+00:00 2026-06-15T23:36:04+00:00

I’m writing some code in c++ that monitors the directory that ubuntu puts files

  • 0

I’m writing some code in c++ that monitors the directory that ubuntu puts files received via bluetooth (~/Downloads). I’m using images as sample files, as the application is opening the images and displaying them.

The code polls the directory every 2 seconds and checks if there’s any files there. If there is, it immediately puts the file into a subdirectory, and loads it. However, it loads the file before the transfer is complete. It sees the file, moves the file, loads the file, and displays a broken image, all while still being transferred.

The subdirectory eventually contains the full image.

I tried using fcntl to lock the file but it locks successfully even while it’s still being written:

#ifndef WIN32
        //We have to do a file lock on ubuntu...
                          /* l_type   l_whence  l_start  l_len  l_pid   */
        struct flock fl = {F_WRLCK, SEEK_SET,   0,      0,     0 };
        int fd;
        fl.l_pid = getpid();
        if((fd = open(newFile.c_str(),O_RDWR)) == -1){
            DODEBUG(ALWAYS,("LOCKING FAILED!"));
            continue;
        }
        if (fcntl(fd, F_SETLKW, &fl) == -1) {
            DODEBUG(ALWAYS,("LOCKING FAILED!"));
            close(fd);
            continue;
        }
        DODEBUG(ALWAYS,("GOT LOCK"));
        fl.l_type = F_UNLCK;  /* set to unlock same region */
        if (fcntl(fd, F_SETLK, &fl) == -1) {
            DODEBUG(ALWAYS,("UNLOCKING FAILED!"));
        }
        close(fd);

#endif
        newFile = moveFile(newFile);
        const std::string s(newFile);
        loadImage(newFIle);

Note that the code I’m writing needs to work on windows and mac also. The windows version already works flawlessly, as the file is not accessible until it is fully received. I haven’t tried mac but I would assume it has the same issues as ubuntu.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:36:05+00:00Added an answer on June 15, 2026 at 11:36 pm

    A workaround to solve this problem is to maintain a list of structs with the file name and file size. Every time you check the dir get a list of the files, check them against your list, if a file isn’t on your list, add it. If a file is on your list compare the file sizes. If the file sizes are the same, then (assuming the transfer wasn’t interrupted) you know the transfer has completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.