~ubuntu-branches/ubuntu/trusty/virtualbox-ose/trusty

« back to all changes in this revision

Viewing changes to src/VBox/Runtime/r3/posix/fileio-posix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-12-18 16:44:29 UTC
  • mfrom: (0.3.3 upstream) (0.4.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091218164429-jd34ccexpv5na11a
Tags: 3.1.2-dfsg-1ubuntu1
* Merge from Debian unstable (LP: #498219), remaining changes:
  - Disable update action
    - debian/patches/u01-disable-update-action.dpatch
  - VirtualBox should go in Accessories, not in System tools (LP: #288590)
    - debian/virtualbox-ose-qt.files/virtualbox-ose.desktop
  - Add Apport hook
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Add Launchpad integration
    - debian/control
    - debian/lpi-bug.xpm
    - debian/patches/u02-lp-integration.dpatch
* Fixes the following bugs:
  - Kernel module fails to build with Linux >= 2.6.32 (LP: #474625)
  - X.Org drivers need to be rebuilt against X-Server 1.7 (LP: #495935)
  - The *-source packages try to build the kernel modules even though the
    kernel headers aren't available (LP: #473334)
* Replace *-source packages with transitional packages for *-dkms.
* Adapt u01-disable-update-action.dpatch and u02-lp-integration.dpatch for
  new upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
# include <unistd.h>
48
48
# include <sys/time.h>
49
49
#endif
 
50
#ifdef RT_OS_LINUX
 
51
# include <sys/file.h>
 
52
#endif
50
53
#if defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006)
51
54
# include <io.h>
52
55
#endif
108
111
}
109
112
 
110
113
 
111
 
RTR3DECL(int)  RTFileOpen(PRTFILE pFile, const char *pszFilename, unsigned fOpen)
 
114
RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint32_t fOpen)
112
115
{
113
116
    /*
114
117
     * Validate input.
115
118
     */
116
 
    if (!VALID_PTR(pFile))
117
 
    {
118
 
        AssertMsgFailed(("Invalid pFile %p\n", pFile));
119
 
        return VERR_INVALID_PARAMETER;
120
 
    }
 
119
    AssertPtrReturn(pFile, VERR_INVALID_POINTER);
121
120
    *pFile = NIL_RTFILE;
122
 
    if (!VALID_PTR(pszFilename))
123
 
    {
124
 
        AssertMsgFailed(("Invalid pszFilename %p\n", pszFilename));
125
 
        return VERR_INVALID_PARAMETER;
126
 
    }
 
121
    AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
127
122
 
128
123
    /*
129
124
     * Merge forced open flags and validate them.
166
161
    if (fOpen & RTFILE_O_ASYNC_IO)
167
162
        fOpenMode |= O_DIRECT;
168
163
#endif
 
164
#if defined(O_DIRECT) && (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
 
165
    /* Disable the kernel cache. */
 
166
    if (fOpen & RTFILE_O_NO_CACHE)
 
167
        fOpenMode |= O_DIRECT;
 
168
#endif
169
169
 
170
170
    /* create/truncate file */
171
171
    switch (fOpen & RTFILE_O_ACTION_MASK)
180
180
 
181
181
    switch (fOpen & RTFILE_O_ACCESS_MASK)
182
182
    {
183
 
        case RTFILE_O_READ:             fOpenMode |= O_RDONLY; break;
184
 
        case RTFILE_O_WRITE:            fOpenMode |= O_WRONLY; break;
185
 
        case RTFILE_O_READWRITE:        fOpenMode |= O_RDWR; break;
 
183
        case RTFILE_O_READ:
 
184
            fOpenMode |= O_RDONLY; /* RTFILE_O_APPEND is ignored. */
 
185
            break;
 
186
        case RTFILE_O_WRITE:
 
187
            fOpenMode |= fOpen & RTFILE_O_APPEND ? O_APPEND | O_WRONLY : O_WRONLY;
 
188
            break;
 
189
        case RTFILE_O_READWRITE:
 
190
            fOpenMode |= fOpen & RTFILE_O_APPEND ? O_APPEND | O_RDWR   : O_RDWR;
 
191
            break;
186
192
        default:
187
193
            AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
188
194
            return VERR_INVALID_PARAMETER;
213
219
#endif
214
220
    if (fh >= 0)
215
221
    {
 
222
        iErr = 0;
 
223
 
216
224
        /*
217
225
         * Mark the file handle close on exec, unless inherit is specified.
218
226
         */
219
 
        if (    !(fOpen & RTFILE_O_INHERIT)
 
227
        if (    (fOpen & RTFILE_O_INHERIT)
220
228
#ifdef O_NOINHERIT
221
 
            ||  (fOpenMode & O_NOINHERIT) /* careful since it could be a dummy. */
222
 
#endif
223
 
            ||  fcntl(fh, F_SETFD, FD_CLOEXEC) >= 0)
 
229
            &&  !(fOpenMode & O_NOINHERIT)  /* Take care since it might be a zero value dummy. */
 
230
#endif
 
231
            )
 
232
            iErr = fcntl(fh, F_SETFD, FD_CLOEXEC) >= 0 ? 0 : errno;
 
233
 
 
234
        /*
 
235
         * Switch direct I/O on now if requested and required.
 
236
         */
 
237
#if defined(RT_OS_DARWIN) \
 
238
 || (defined(RT_OS_SOLARIS) && !defined(IN_GUEST))
 
239
        if (iErr == 0 && (fOpen & RTFILE_O_NO_CACHE))
 
240
        {
 
241
# if defined(RT_OS_DARWIN)
 
242
            iErr = fcntl(fh, F_NOCACHE, 1)        >= 0 ? 0 : errno;
 
243
# else
 
244
            iErr = directio(fh, DIRECTIO_ON)      >= 0 ? 0 : errno;
 
245
# endif
 
246
        }
 
247
#endif
 
248
 
 
249
        /*
 
250
         * Implement / emulate file sharing.
 
251
         *
 
252
         * We need another mode which allows skipping this stuff completely
 
253
         * and do things the UNIX way. So for the present this is just a debug
 
254
         * aid that can be enabled by developers too lazy to test on Windows.
 
255
         */
 
256
#if 0 && defined(RT_OS_LINUX)
 
257
        if (iErr == 0)
 
258
        {
 
259
            /* This approach doesn't work because only knfsd checks for these
 
260
               buggers. :-( */
 
261
            int iLockOp;
 
262
            switch (fOpen & RTFILE_O_DENY_MASK)
 
263
            {
 
264
                default:
 
265
                AssertFailed();
 
266
                case RTFILE_O_DENY_NONE:
 
267
                case RTFILE_O_DENY_NOT_DELETE:
 
268
                    iLockOp = LOCK_MAND | LOCK_READ | LOCK_WRITE;
 
269
                    break;
 
270
                case RTFILE_O_DENY_READ:
 
271
                case RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
 
272
                    iLockOp = LOCK_MAND | LOCK_WRITE;
 
273
                    break;
 
274
                case RTFILE_O_DENY_WRITE:
 
275
                case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_NOT_DELETE:
 
276
                    iLockOp = LOCK_MAND | LOCK_READ;
 
277
                    break;
 
278
                case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ:
 
279
                case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
 
280
                    iLockOp = LOCK_MAND;
 
281
                    break;
 
282
            }
 
283
            iErr = flock(fh, iLockOp | LOCK_NB);
 
284
            if (iErr != 0)
 
285
                iErr = errno == EAGAIN ? ETXTBSY : 0;
 
286
        }
 
287
#endif /* 0 && RT_OS_LINUX */
 
288
#ifdef DEBUG_bird
 
289
        if (iErr == 0)
 
290
        {
 
291
            /* This emulation is incomplete but useful. */
 
292
            switch (fOpen & RTFILE_O_DENY_MASK)
 
293
            {
 
294
                default:
 
295
                AssertFailed();
 
296
                case RTFILE_O_DENY_NONE:
 
297
                case RTFILE_O_DENY_NOT_DELETE:
 
298
                case RTFILE_O_DENY_READ:
 
299
                case RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
 
300
                    break;
 
301
                case RTFILE_O_DENY_WRITE:
 
302
                case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_NOT_DELETE:
 
303
                case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ:
 
304
                case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
 
305
                    if (fOpen & RTFILE_O_WRITE)
 
306
                    {
 
307
                        iErr = flock(fh, LOCK_EX | LOCK_NB);
 
308
                        if (iErr != 0)
 
309
                            iErr = errno == EAGAIN ? ETXTBSY : 0;
 
310
                    }
 
311
                    break;
 
312
            }
 
313
        }
 
314
#endif
 
315
#ifdef RT_OS_SOLARIS
 
316
        /** @todo Use fshare_t and associates, it's a perfect match. see sys/fcntl.h */
 
317
#endif
 
318
 
 
319
        /*
 
320
         * We're done.
 
321
         */
 
322
        if (iErr == 0)
224
323
        {
225
324
            *pFile = (RTFILE)fh;
226
325
            Assert((int)*pFile == fh);
228
327
                     pFile, *pFile, pszFilename, pszFilename, fOpen, rc));
229
328
            return VINF_SUCCESS;
230
329
        }
231
 
        iErr = errno;
 
330
 
232
331
        close(fh);
233
332
    }
234
333
    return RTErrConvertFromErrno(iErr);