LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-31-2023, 06:19 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,687
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
O_TRUNC on open(2)


when calling open() for writing with flag O_WRONLY set, and all flags O_CREAT, O_EXCL, and O_TRUNC not set, what is the behavior of the first write() call. what will be seen as happening to the file if it is written to without truncating?

unfortunately, the man pages tend to describe flag behavior when set, but do not describe the effect when not set, or the behavior of combinations with other flags.
 
Old 01-31-2023, 09:38 PM   #2
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
I believe the intent is if you have an existing file you can "seek()" to a location and overwrite any portion of an existing file.
 
Old 03-25-2023, 06:56 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,687

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
if the first operation after the file is open without O_TRUNC is write() (not seek()), where is the data written to? in other words, where is the implied first "seek()" position? is it position 0, the beginning of the file?

what is supposed to happen if O_TRUNC is used with O_RDONLY? i have seen this actually truncate the file to 0 length.
 
Old 03-25-2023, 08:53 PM   #4
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
If the file is opened with O_APPEND, the write will append after the last byte.

Otherwise, unless you seek first, it will overwrite the first.

> what is supposed to happen if O_TRUNC is used with O_RDONLY? i have seen this actually truncate the file to 0 length.

that sounds like a bug...

From man 2 open...

O_TRUNC
If the file already exists and is a regular file and the access mode allows writing (i.e., is O_RDWR
or O_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, the
O_TRUNC flag is ignored. Otherwise, the effect of O_TRUNC is unspecified.
 
Old 03-31-2023, 07:31 PM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,687

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
unspecified could mean anything. it could cause my laptop to vanish. i better be more careful. i will try to make a minimal reproduction.
 
Old 04-03-2023, 11:58 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
.. or worse
Quote:

undefined— The behavior for something incorrect, on which the standard does not impose any
requirements. Anything is allowed to happen, from nothing, to a warning message to program
termination, to CPU meltdown, to launching nuclear missiles (assuming you have the correct
hardware option installed).
https://progforperf.github.io/Expert_C_Programming.pdf

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Open exchange, open doors, open minds: A recipe for global progress LXer Syndicated Linux News 0 03-15-2022 08:03 PM
LXer: Top open source projects in 2018, open source hardware, donation model for sustaining open source projects, and more news LXer Syndicated Linux News 0 10-27-2018 06:12 PM
LXer: Linux Foundation takes on Open vSwitch, defining 'open,' and more open source news LXer Syndicated Linux News 0 08-13-2016 06:09 PM
LXer: Top 5 articles of the week: Open source tasks, open data apps, and open source in education LXer Syndicated Linux News 0 09-21-2014 10:40 PM
xdg-open, gnome-open or kde(4?)-open??? akamikeym Programming 0 08-03-2010 08:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration