├── LICENSE ├── README.md └── src ├── main.go └── ramdisk ├── example └── webserver │ └── main.go ├── fs.go ├── fs_test.go ├── notification.go └── notification_test.go /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ramdisk 2 | a RAM disk written in Go. a RAM disk is a file system where data is held only in RAM. 3 | 4 | An alpha-stage RAM disk implemented in Go. 5 | The RAM disk can be mounted as a Linux file system in user space (FUSE), needing no elevated privileges. 6 | Files can be created, read and written, but are not persisted to durable storage. Sufficient current must be flowing all the time. 7 | 8 | The Go process creating the RAM disk has direct in-process access to file data, represented by a byte slice. 9 | 10 | ## prepare 11 | 12 | ```bash 13 | git clone https://github.com/berndfo/ramdisk.git 14 | cd ramdisk 15 | export GOPATH=`pwd` # use backticks here! 16 | go get bazil.org/fuse 17 | go get golang.org/x/net 18 | 19 | go run src/main.go 20 | ``` 21 | 22 | ## how to mount 23 | 24 | Mounting a RAM disk is very simple. Just prepare the mount point (here: `/mnt/myramdisk`) 25 | and run: 26 | ```go 27 | ramdisk.MountAndServe("/mnt/myramdisk", nil) 28 | ``` 29 | 30 | A mounted RAM disk can be accessed like any other file system on Linux (cd, cp, echo, cat, etc.). 31 | No byte will ever hit any disk. All data is lost after terminating the process. 32 | 33 | ## how to track changes to FS 34 | 35 | to act on changes in the in-process RAM disk, you can listen on a number of channels: 36 | 37 | ```go 38 | func main() { 39 | 40 | fsevents := ramdisk.NewFSEvents() 41 | 42 | go func() { 43 | for { 44 | var event interface{} 45 | select { 46 | case event = <-fsevents.FileCreated: 47 | log.Printf("file create: %q", event.(ramdisk.EventFileCreated).File.Meta.Name()) 48 | case event = <-fsevents.FileOpened: 49 | case event = <-fsevents.FileWritten: 50 | case event = <-fsevents.FileClosed: 51 | file := event.(ramdisk.EventFileClosed) 52 | log.Printf("file closed: %q, size = %d", file.File.Meta.Name(), file.File.Meta.Size()) 53 | case event = <-fsevents.Unmount: 54 | } 55 | } 56 | } () 57 | 58 | ramdisk.MountAndServe("/mnt/myramdisk", &fsevents) 59 | } 60 | ``` 61 | 62 | in this example, every file creation and close operation is logged. 63 | Please make sure to listen on all channels, but feel free to ignore any event you're not interested in. 64 | 65 | ## how to unmount 66 | ```bash 67 | # on the Linux shell 68 | fusermount -u /mnt/fusemnt 69 | ``` 70 | 71 | or 72 | 73 | ```go 74 | // in Go code 75 | fuse.Unmount(mountpoint) 76 | ``` 77 | 78 | ## accessing file data in-process 79 | 80 | assume that `latest` is holding a recently written JPG image: 81 | `var latest *ramdisk.FileEntry // last closed file entry` 82 | this file might have been written by `ffmpeg` or any another out-of-process application. 83 | a web request can directly render this image to the response by copying data 84 | 85 | ```go 86 | func webHandler(response http.ResponseWriter, request *http.Request) { 87 | response.Header().Add("Content-type", "image/jpg") 88 | response.Write(latest.Data) 89 | } 90 | ``` 91 | 92 | for a running, detailed example see `src/ramdisk/webserver/main.go` 93 | 94 | ## missing features 95 | 96 | -[ ] deletion of files 97 | -[ ] support directory structure 98 | 99 | -------------------------------------------------------------------------------- /src/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "ramdisk" 5 | "log" 6 | ) 7 | 8 | func main() { 9 | 10 | fsevents := ramdisk.NewFSEvents() 11 | 12 | go func() { 13 | for { 14 | var event interface{} 15 | select { 16 | case event = <-fsevents.FileCreated: 17 | log.Printf("file create: %q", event.(ramdisk.EventFileCreated).File.Meta.Name()) 18 | case event = <-fsevents.FileOpened: 19 | case event = <-fsevents.FileWritten: 20 | case event = <-fsevents.FileClosed: 21 | file := event.(ramdisk.EventFileClosed) 22 | log.Printf("file closed: %q, size = %d", file.File.Meta.Name(), file.File.Meta.Size()) 23 | case event = <-fsevents.Unmount: 24 | } 25 | } 26 | } () 27 | 28 | ramdisk.MountAndServe("/mnt/myramdisk", &fsevents) 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/ramdisk/example/webserver/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "ramdisk" 5 | "log" 6 | "net/http" 7 | "strings" 8 | "sync" 9 | "image" 10 | "bytes" 11 | "image/jpeg" 12 | "image/color" 13 | "image/draw" 14 | ) 15 | 16 | const WEBSITE = ` 17 | 18 | 19 | 20 | A pic. 21 | 22 | 23 | 24 | 25 | ` 26 | 27 | // webHandler either sends the JPG stored in the global variable 'latest' 28 | func webHandler(response http.ResponseWriter, request *http.Request) { 29 | if strings.HasSuffix(request.RequestURI, ".jpg") { 30 | // send latest file, assuming it represents a jpg 31 | if latest == nil { 32 | response.WriteHeader(http.StatusNotFound) 33 | return 34 | } 35 | response.Header().Add("Cache-control", "no-cache") 36 | response.Header().Add("Content-type", "image/jpg") 37 | latestMutex.Lock() 38 | if strings.HasSuffix(request.RequestURI, "_alt.jpg") { 39 | stampOutPicture(latest.Data) // create new latestAlt from latest 40 | response.Write(latestAlt) 41 | } else { 42 | response.Write(latest.Data) 43 | } 44 | latestMutex.Unlock() 45 | } else { 46 | // send website for every URL not ending in .jpg 47 | response.Header().Add("Content-type", "text/html") 48 | response.Write([]byte(WEBSITE)) 49 | } 50 | } 51 | 52 | var latest *ramdisk.FileEntry // last closed file entry 53 | var latestAlt []byte // latest file transformed 54 | var latestMutex sync.Mutex // safeguard access to latest 55 | 56 | var maskedImage *image.RGBA 57 | 58 | // main starts the sample application, which 59 | // * opens a webserver on port 8080 60 | // * mounts RAM disk at /mnt/myramdisk (this directory must have created in advance) 61 | // when creating new files there, for example by running 62 | // ffmpeg -i MY_VIDEO.mp4 /mnt/myramdisk/%3d.jpg 63 | // the website http://localhost:8080/ will constantly render the latest picture. 64 | func main() { 65 | 66 | // prepare some static data 67 | maskedImage = image.NewRGBA(image.Rect(0, 0, 320, 200)) 68 | draw.Draw(maskedImage, maskedImage.Bounds(), &image.Uniform{image.White}, image.ZP, draw.Src) 69 | 70 | // notifications from file system 71 | fsevents := ramdisk.NewFSEvents() 72 | 73 | // start webserver 74 | go func() { 75 | http.ListenAndServe("localhost:8080", http.HandlerFunc(webHandler)) 76 | } () 77 | 78 | // prepare receiving file change notifications 79 | // here, a pointer the latest closed file is maintained 80 | go func() { 81 | for { 82 | var event interface{} 83 | select { 84 | case event = <-fsevents.FileCreated: 85 | log.Printf("file create: %q", event.(ramdisk.EventFileCreated).File.Meta.Name()) 86 | case event = <-fsevents.FileOpened: 87 | case event = <-fsevents.FileWritten: 88 | case event = <-fsevents.FileClosed: 89 | // as soon as file is closed, keep it in global variable 90 | file := event.(ramdisk.EventFileClosed) 91 | log.Printf("file closed: %q, size = %d", file.File.Meta.Name(), file.File.Meta.Size()) 92 | latestMutex.Lock() 93 | latest = file.File 94 | latestMutex.Unlock() 95 | case event = <-fsevents.Unmount: 96 | } 97 | } 98 | } () 99 | 100 | // mount ramdisk at "/mnt/fusemnt" 101 | // you can now copy files to it, like "cp mypic.jpg /mnt/fusemnt 102 | // it will appear as the 103 | ramdisk.MountAndServe("/mnt/myramdisk", &fsevents) 104 | } 105 | 106 | type circle struct { 107 | p image.Point 108 | r int 109 | } 110 | 111 | func (c *circle) ColorModel() color.Model { 112 | return color.AlphaModel 113 | } 114 | 115 | func (c *circle) Bounds() image.Rectangle { 116 | return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r) 117 | } 118 | 119 | func (c *circle) At(x, y int) color.Color { 120 | xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r) 121 | if xx*xx+yy*yy < rr*rr { 122 | return color.Alpha{255} 123 | } 124 | return color.Alpha{0} 125 | } 126 | 127 | func stampOutPicture(jpgData []byte) { 128 | img, err := jpeg.Decode(bytes.NewReader(jpgData)) 129 | if err != nil { 130 | return 131 | } 132 | 133 | draw.DrawMask(maskedImage, maskedImage.Bounds(), img, image.ZP, &circle{image.Point{160, 100}, 100}, image.ZP, draw.Over) 134 | 135 | buffer := bytes.Buffer{} 136 | jpeg.Encode(&buffer, maskedImage, nil) 137 | latestAlt = buffer.Bytes() 138 | } 139 | 140 | -------------------------------------------------------------------------------- /src/ramdisk/fs.go: -------------------------------------------------------------------------------- 1 | package ramdisk 2 | 3 | import ( 4 | "bazil.org/fuse/fs" 5 | "bazil.org/fuse" 6 | "os" 7 | "golang.org/x/net/context" 8 | "sync/atomic" 9 | "syscall" 10 | "time" 11 | "bazil.org/fuse/fuseutil" 12 | "sync" 13 | "runtime" 14 | "log" 15 | ) 16 | 17 | var atomicInode uint64 = 1 18 | 19 | func CreateRamFS() *ramdiskFS { 20 | filesys := &ramdiskFS{ 21 | backendEvents: NewFSEvents(), 22 | addListenerChan: make(chan *FSEvents), 23 | } 24 | 25 | eventQueueMutex := sync.Mutex{} 26 | eventQueue := make([]interface{}, 0) 27 | 28 | // fetch backend events, and queue them. decoupling from listeners 29 | go func(fsevents FSEvents) { 30 | // TODO go routine termination 31 | for { 32 | var event interface{} 33 | select { 34 | case event = <-fsevents.FileCreated: 35 | case event = <-fsevents.FileOpened: 36 | case event = <-fsevents.FileRead: 37 | case event = <-fsevents.FileWritten: 38 | case event = <-fsevents.FileClosed: 39 | case event = <-fsevents.Unmount: 40 | } 41 | _ = event 42 | eventQueueMutex.Lock() 43 | eventQueue = append(eventQueue, event) 44 | eventQueueMutex.Unlock() 45 | } 46 | return 47 | } (filesys.backendEvents) 48 | 49 | // propagate queued events to listeners 50 | go func() { 51 | // TODO go routine termination 52 | listenerEvents := make([]*FSEvents, 0) 53 | 54 | for { 55 | select { 56 | case newListener:=<-filesys.addListenerChan: 57 | listenerEvents = append(listenerEvents, newListener) 58 | default: 59 | // fall through 60 | } 61 | 62 | var event interface{} 63 | 64 | eventQueueMutex.Lock() 65 | if len(eventQueue) > 0 { 66 | event = eventQueue[0] 67 | eventQueue = eventQueue[1:] 68 | } 69 | eventQueueMutex.Unlock() 70 | 71 | if event != nil { 72 | // this part relies on cooperation of listeners 73 | for _, listener := range listenerEvents { 74 | switch event.(type) { 75 | case EventFileCreated: 76 | listener.FileCreated <- event.(EventFileCreated) 77 | case EventFileOpened: 78 | listener.FileOpened <- event.(EventFileOpened) 79 | case EventFileWritten: 80 | listener.FileWritten <- event.(EventFileWritten) 81 | case EventFileRead: 82 | listener.FileRead <- event.(EventFileRead) 83 | case EventFileClosed: 84 | listener.FileClosed <- event.(EventFileClosed) 85 | case bool: 86 | listener.Unmount <- event.(bool) 87 | default: 88 | log.Panicf("unknown and unhandled FS event %T", event) 89 | } 90 | } 91 | } else { 92 | // be a good go citizen 93 | runtime.Gosched() 94 | } 95 | } 96 | }() 97 | 98 | return filesys 99 | } 100 | 101 | func MountAndServe(mountpoint string, optionalListener *FSEvents) error { 102 | c, err := fuse.Mount(mountpoint) 103 | if err != nil { 104 | log.Printf("failed to MountAndServe %q", mountpoint) 105 | return err 106 | } 107 | log.Printf("successfully mounted %q", mountpoint) 108 | 109 | defer c.Close() 110 | 111 | filesys := CreateRamFS() 112 | 113 | if optionalListener != nil { 114 | filesys.AddListener(optionalListener) 115 | } 116 | 117 | if err := fs.Serve(c, filesys); err != nil { 118 | log.Printf("failed to serve a filesystem at MountAndServe %q", mountpoint) 119 | return err 120 | } 121 | 122 | // check if the MountAndServe process has an error to report 123 | <-c.Ready 124 | if err := c.MountError; err != nil { 125 | log.Printf("failure mounting a filesystem at MountAndServe %q", mountpoint) 126 | return err 127 | } 128 | 129 | //fuse.Unmount(mountpoint) 130 | 131 | return nil 132 | } 133 | func nextInode() uint64 { 134 | return atomic.AddUint64(&atomicInode, 1) 135 | } 136 | 137 | // implements FSInodeGenerator 138 | type ramdiskFS struct { 139 | backendEvents FSEvents 140 | addListenerChan chan *FSEvents 141 | } 142 | 143 | func (f *ramdiskFS) Root() (fs.Node, error) { 144 | return &Dir{fs: f}, nil 145 | } 146 | 147 | func (f *ramdiskFS) GenerateInode(parentInode uint64, name string) uint64 { 148 | return nextInode() 149 | } 150 | 151 | func (f *ramdiskFS) AddListener(newListener *FSEvents) { 152 | f.addListenerChan <- newListener 153 | } 154 | 155 | type Dir struct { 156 | mutex sync.RWMutex 157 | fs *ramdiskFS 158 | } 159 | 160 | func (d *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) { 161 | entry, found := findEntryByName(name) 162 | if !found { 163 | return nil, fuse.ENOENT 164 | } 165 | return &entry.Meta, nil 166 | } 167 | 168 | func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error { 169 | a.Inode = 1 170 | a.Mode = os.ModeDir | 0555 171 | return nil 172 | } 173 | 174 | func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { 175 | entries := make([]fuse.Dirent, 0) 176 | for _, entry := range rootEntries { 177 | entries = append(entries, entry.dirEntry) 178 | } 179 | return entries, nil 180 | } 181 | 182 | func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { 183 | requestedName := req.Name 184 | if requestedName == "" { 185 | // no file has no name 186 | return nil, nil, fuse.EPERM 187 | } 188 | 189 | _, alreadyExits := findEntryByName(requestedName) 190 | if alreadyExits { 191 | // already exists 192 | return nil, nil, fuse.EPERM 193 | } 194 | 195 | newEntry := createFileEntry(requestedName, d.fs) 196 | 197 | d.mutex.Lock() 198 | rootEntries = append(rootEntries, newEntry) 199 | d.mutex.Unlock() 200 | 201 | handle := Handle{inode: newEntry.Meta.inode} 202 | 203 | d.fs.backendEvents.FileCreated<-EventFileCreated{FSEvent{File: newEntry}} 204 | 205 | return &newEntry.Meta, handle, nil 206 | } 207 | 208 | // implements fs.Node 209 | type RamFile struct { 210 | fuse *fs.Server 211 | inode uint64 212 | name string 213 | size uint64 214 | created time.Time 215 | modified time.Time 216 | writable bool 217 | } 218 | 219 | func (f *RamFile) Attr(ctx context.Context, a *fuse.Attr) error { 220 | a.Inode = f.inode 221 | if f.writable { 222 | a.Mode = 0666 223 | } else { 224 | a.Mode = 0555 225 | } 226 | a.Size = f.size 227 | a.Ctime = f.created 228 | a.Mtime = f.modified 229 | return nil 230 | } 231 | 232 | func (f *RamFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { 233 | if !f.writable && !req.Flags.IsReadOnly() { 234 | return nil, fuse.Errno(syscall.EACCES) 235 | } 236 | resp.Flags |= fuse.OpenDirectIO 237 | 238 | entry, found := findEntryByInode(f.inode) 239 | if !found { 240 | return nil, fuse.Errno(syscall.ENOENT) 241 | } 242 | 243 | handle := Handle{inode: f.inode} 244 | 245 | entry.fs.backendEvents.FileOpened<-EventFileOpened{FSEvent{File: entry}} 246 | 247 | return handle, nil 248 | } 249 | 250 | func (f *RamFile) Inode() uint64 { 251 | return f.inode 252 | } 253 | 254 | func (f *RamFile) Name() string { 255 | return f.name 256 | } 257 | 258 | func (f *RamFile) Size() uint64 { 259 | return f.size 260 | } 261 | 262 | // implements fs.Handle, fs.HandleWriter, fs.HandleReader 263 | type Handle struct { 264 | inode uint64 265 | } 266 | 267 | func (h Handle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { 268 | entry, found := findEntryByInode(h.inode) 269 | if !found { 270 | return fuse.Errno(syscall.ENOENT) 271 | } 272 | 273 | fuseutil.HandleRead(req, resp, entry.Data) 274 | 275 | entry.fs.backendEvents.FileRead <-EventFileRead{FSEvent{File: entry}} 276 | 277 | return nil 278 | } 279 | 280 | func (h Handle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error { 281 | //log.Printf("try to write %s", req.ID) 282 | //n, err := w.buf.Write(req.Data) 283 | newBytes := req.Data 284 | 285 | inode := h.inode 286 | 287 | entry, found := findEntryByInode(inode) 288 | if !found { 289 | return fuse.Errno(syscall.ENOENT) 290 | } 291 | 292 | currentDataLength := len(entry.Data) 293 | offsetPos := int(req.Offset) 294 | if (offsetPos == currentDataLength) { 295 | // new data is added at the end 296 | entry.Data = append(entry.Data, newBytes...) 297 | } else if (offsetPos < currentDataLength) { 298 | // data is partially overwritten 299 | endPos := int(offsetPos) + len(newBytes) 300 | if (endPos > currentDataLength) { 301 | missingBytes := endPos - currentDataLength 302 | // extend slice by missing byte count 303 | entry.Data = append(entry.Data, make([]byte, missingBytes)...) 304 | } 305 | copy(entry.Data[offsetPos:endPos], newBytes[:]) 306 | } else { 307 | // offset is beyond last byte 308 | newEndPos := int(offsetPos) + len(newBytes) 309 | missingBytes := newEndPos - currentDataLength 310 | entry.Data = append(entry.Data, make([]byte, missingBytes)...) 311 | copy(entry.Data[offsetPos:newEndPos], newBytes[:]) 312 | } 313 | entry.Meta.size = uint64(len(entry.Data)) 314 | 315 | entry.Meta.modified = time.Now() 316 | resp.Size = len(newBytes) 317 | //log.Printf("write: added: %d, new total: %d", resp.Size, entry.Meta.size) 318 | 319 | entry.fs.backendEvents.FileWritten<-EventFileWritten{FSEvent{File: entry}} 320 | 321 | return nil 322 | } 323 | 324 | func (h Handle) Release(ctx context.Context, req *fuse.ReleaseRequest) error { 325 | inode := h.inode 326 | 327 | entry, found := findEntryByInode(inode) 328 | if !found { 329 | return fuse.Errno(syscall.ENOENT) 330 | } 331 | entry.fs.backendEvents.FileClosed<-EventFileClosed{FSEvent{File: entry}} 332 | 333 | return nil 334 | } 335 | 336 | 337 | var rootEntries = []*FileEntry{ 338 | } 339 | 340 | func findEntryByName(name string) (*FileEntry, bool) { 341 | for _, fileEntry := range rootEntries { 342 | if fileEntry.dirEntry.Name == name { 343 | return fileEntry, true 344 | } 345 | } 346 | return nil, false 347 | } 348 | 349 | func findEntryByInode(inode uint64) (*FileEntry, bool) { 350 | for _, fileEntry := range rootEntries { 351 | if fileEntry.dirEntry.Inode == inode { 352 | return fileEntry, true 353 | } 354 | } 355 | return nil, false 356 | } 357 | 358 | type FileEntry struct { 359 | fs *ramdiskFS 360 | dirEntry fuse.Dirent 361 | Meta RamFile 362 | Data []byte 363 | } 364 | 365 | func createFileEntry(name string, fs *ramdiskFS) (entry *FileEntry) { 366 | inode := nextInode() 367 | emptyContent := make([]byte, 0) 368 | entry = &FileEntry{ 369 | fs: fs, 370 | dirEntry: fuse.Dirent{Inode:inode, Name: name, Type: fuse.DT_File}, 371 | Meta: RamFile{inode: inode, name: name, writable: true}, 372 | Data: emptyContent, 373 | } 374 | return 375 | } 376 | 377 | 378 | 379 | 380 | 381 | -------------------------------------------------------------------------------- /src/ramdisk/fs_test.go: -------------------------------------------------------------------------------- 1 | package ramdisk 2 | 3 | import ( 4 | "testing" 5 | "bazil.org/fuse/fs/fstestutil" 6 | "os" 7 | "io/ioutil" 8 | "log" 9 | "io" 10 | ) 11 | 12 | func init() { 13 | fstestutil.DebugByDefault() 14 | } 15 | 16 | func TestWriteOnce(t *testing.T) { 17 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 18 | defer mnt.Close() 19 | 20 | writer, createErr := os.Create(mnt.Dir + "/" + "a1.txt") 21 | writtenBytes, writeErr:= writer.WriteString("testtesttest") 22 | defer writer.Close() 23 | 24 | if mntErr != nil || createErr != nil || writeErr != nil { 25 | t.Error("mount or create or write failed.") 26 | } 27 | 28 | if writtenBytes != 12 { 29 | t.Error("not 12 bytes written") 30 | } 31 | 32 | writer.Close() 33 | log.Print("file closed.") 34 | 35 | fileInfo, errStat := os.Stat(mnt.Dir + "/" + "a1.txt") 36 | if errStat != nil { 37 | t.Fatal("no stat on written file") 38 | } 39 | if fileInfo.Size() != 12 { 40 | t.Fatalf("stat reports wrong file size %d for file %q", fileInfo.Size(), fileInfo.Name()) 41 | } 42 | 43 | } 44 | 45 | func TestWriteMultiple(t *testing.T) { 46 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 47 | defer mnt.Close() 48 | 49 | writer, createErr := os.Create(mnt.Dir + "/" + "a2.txt") 50 | defer writer.Close() 51 | 52 | _, writeErr1 := writer.WriteString("testtesttest") 53 | if mntErr != nil || createErr != nil || writeErr1 != nil { 54 | t.Fatal("first write failed") 55 | } 56 | 57 | writtenBytes, writeErr2 := writer.WriteString("aaaabbbb") 58 | if writeErr2 != nil { 59 | t.Fatal("second write failed") 60 | } 61 | 62 | if writtenBytes != 8 { 63 | t.Fatal("not written 8 bytes") 64 | } 65 | writer.Close() 66 | 67 | fileInfo, errStat := os.Stat(mnt.Dir + "/" + "a2.txt") 68 | if errStat != nil { 69 | t.Fatal("no stat on written file") 70 | } 71 | if fileInfo.Size() != (3*4 + 8) { 72 | t.Fatal("stat reports wrong file size", fileInfo.Size()) 73 | } 74 | } 75 | 76 | func TestReadMultiwrite(t *testing.T) { 77 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 78 | defer mnt.Close() 79 | 80 | writer, createErr := os.Create(mnt.Dir + "/" + "a3.txt") 81 | defer writer.Close() 82 | 83 | _, writeErr1 := writer.WriteString("testtesttest") 84 | writtenBytes, writeErr2 := writer.WriteString("aaaabbbb") 85 | 86 | if mntErr != nil || createErr != nil || writeErr1 != nil || writeErr2 != nil { 87 | t.Fail() 88 | } 89 | 90 | writer.Close() 91 | 92 | _, errStat := os.Stat(mnt.Dir + "/" + "a3.txt") 93 | if errStat != nil { 94 | t.Fatal("no stat on written file") 95 | } 96 | 97 | reader, err := os.OpenFile(mnt.Dir + "/" + "a3.txt", os.O_RDONLY, 0) 98 | if err != nil { 99 | t.Fatal("not opened, " + err.Error()) 100 | } 101 | defer reader.Close() 102 | 103 | byts, err := ioutil.ReadAll(reader) 104 | if err != nil { 105 | t.Fatal("not read") 106 | } 107 | 108 | bytsToString := string(byts) 109 | log.Printf("read: %q", bytsToString) 110 | if bytsToString != "testtesttestaaaabbbb" { 111 | t.Fail() 112 | } 113 | 114 | if writtenBytes != 8 { 115 | t.Fail() 116 | } 117 | 118 | 119 | } 120 | 121 | func TestRandomRead(t *testing.T) { 122 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 123 | defer mnt.Close() 124 | 125 | writer, createErr := os.Create(mnt.Dir + "/" + "a4.txt") 126 | defer writer.Close() 127 | 128 | _, writeErr1 := writer.WriteString("testabctest") 129 | 130 | if (mntErr != nil || createErr != nil || writeErr1 != nil) { 131 | t.Fail() 132 | } 133 | 134 | writer.Close() 135 | 136 | reader, err := os.OpenFile(mnt.Dir + "/" + "a4.txt", os.O_RDONLY, 0) 137 | if err != nil { 138 | t.Fatal("not opened, " + err.Error()) 139 | } 140 | defer reader.Close() 141 | 142 | threeBytes := make([]byte, 3) 143 | readCount, errRead := reader.ReadAt(threeBytes, 4) 144 | if errRead != nil { 145 | t.Fatal("not read") 146 | } 147 | if readCount != 3 { 148 | t.Fatalf("instad of 3, read %d", readCount) 149 | } 150 | 151 | bytsToString := string(threeBytes) 152 | log.Printf("read: %q", bytsToString) 153 | if bytsToString != "abc" { 154 | t.Fail() 155 | } 156 | } 157 | 158 | func TestRandomReadIncomplete(t *testing.T) { 159 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 160 | defer mnt.Close() 161 | 162 | writer, createErr := os.Create(mnt.Dir + "/" + "a5.txt") 163 | defer writer.Close() 164 | 165 | _, writeErr1 := writer.WriteString("testtestab") 166 | 167 | if mntErr != nil || createErr != nil || writeErr1 != nil { 168 | t.Fail() 169 | } 170 | 171 | writer.Close() 172 | 173 | reader, err := os.OpenFile(mnt.Dir + "/" + "a5.txt", os.O_RDONLY, 0) 174 | if err != nil { 175 | t.Fatal("not opened, " + err.Error()) 176 | } 177 | defer reader.Close() 178 | 179 | threeBytes := make([]byte, 3) 180 | readCount, errRead := reader.ReadAt(threeBytes, 8) // only 2 bytes left in file 181 | if errRead != io.EOF { 182 | t.Fatal("not EOF", errRead.Error()) 183 | } 184 | if readCount != 2 { 185 | t.Fatalf("instad of 3, read %d", readCount) 186 | } 187 | 188 | bytsToString := string(threeBytes[:readCount]) 189 | log.Printf("read: %q", bytsToString) 190 | if bytsToString != "ab" { 191 | t.Fail() 192 | } 193 | } 194 | 195 | func TestRandomSeek(t *testing.T) { 196 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 197 | defer mnt.Close() 198 | 199 | writer, createErr := os.Create(mnt.Dir + "/" + "a6.txt") 200 | defer writer.Close() 201 | 202 | _, writeErr1 := writer.WriteString("testabatesttesttbabesttesttesttestcbctest") 203 | 204 | if mntErr != nil || createErr != nil || writeErr1 != nil { 205 | t.Fail() 206 | } 207 | 208 | writer.Close() 209 | 210 | reader, err := os.OpenFile(mnt.Dir + "/" + "a6.txt", os.O_RDONLY, 0) 211 | if err != nil { 212 | t.Fatal("not opened, " + err.Error()) 213 | } 214 | defer reader.Close() 215 | 216 | threeBytes := make([]byte, 3) 217 | 218 | reader.Seek(4, 0) // seek from start 219 | _, _ = reader.Read(threeBytes) 220 | if "aba" != string(threeBytes) { 221 | t.Fatal("not seeked to pos 4") 222 | } 223 | 224 | reader.Seek(16, 0) 225 | _, _ = reader.Read(threeBytes) 226 | if "bab" != string(threeBytes) { 227 | t.Fatal("not seeked to pos 16") 228 | } 229 | 230 | threeBytes = []byte("___") // neutralizes 231 | 232 | reader.Seek(-7, 2) // seek 7 backwards from end 233 | actuallyRead, seekErr := reader.Read(threeBytes) 234 | if seekErr != nil { 235 | t.Fatal(seekErr.Error()) 236 | } 237 | if "cbc" != string(threeBytes) { 238 | t.Fatalf("not seeked to pos 16: %d %q", actuallyRead, threeBytes) 239 | } 240 | 241 | reader.Seek(10, 0) // seek from start... 242 | reader.Seek(6, 1) // ... then seek relative 243 | _, _ = reader.Read(threeBytes) 244 | if "bab" != string(threeBytes) { 245 | t.Fatal("not seeked to pos 10+6") 246 | } 247 | } 248 | 249 | func TestRandomWrite(t *testing.T) { 250 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 251 | defer mnt.Close() 252 | 253 | writer, createErr := os.Create(mnt.Dir + "/" + "a7.txt") 254 | defer writer.Close() 255 | 256 | _, writeErr1 := writer.WriteString("abcdefghijklmnopqrstuvwxyz") 257 | 258 | if mntErr != nil || createErr != nil || writeErr1 != nil { 259 | t.Fail() 260 | } 261 | 262 | writer.Seek(7, 0) 263 | 264 | _, writeErr2 := writer.WriteString("test") 265 | if writeErr2 != nil { 266 | t.Fail() 267 | } 268 | 269 | writer.Close() 270 | 271 | reader, err := os.OpenFile(mnt.Dir + "/" + "a7.txt", os.O_RDONLY, 0) 272 | if err != nil { 273 | t.Fatal("not opened, " + err.Error()) 274 | } 275 | defer reader.Close() 276 | 277 | threeBytes := make([]byte, 3) 278 | 279 | reader.Seek(7, 0) // seek from start 280 | _, _ = reader.Read(threeBytes) 281 | if "tes" != string(threeBytes) { 282 | t.Fatal("not overwritten") 283 | } 284 | } 285 | 286 | func TestOverwriteBeyondEnd(t *testing.T) { 287 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 288 | defer mnt.Close() 289 | 290 | writer, createErr := os.Create(mnt.Dir + "/" + "a8.txt") 291 | defer writer.Close() 292 | 293 | _, writeErr1 := writer.WriteString("abcdefghijklmnopqrstuvwxyz") 294 | 295 | if mntErr != nil || createErr != nil || writeErr1 != nil { 296 | t.Fail() 297 | } 298 | 299 | writer.Seek(-2, 2) 300 | 301 | _, writeErr2 := writer.WriteString("test") 302 | if writeErr2 != nil { 303 | t.Fail() 304 | } 305 | 306 | writer.Close() 307 | 308 | fileInfo, errStat := os.Stat(mnt.Dir + "/" + "a8.txt") 309 | if errStat != nil { 310 | t.Fatal("no stat on written file") 311 | } 312 | if fileInfo.Size() != 26+2 { 313 | t.Fatal("file not expanded by 2 bytes") 314 | } 315 | 316 | reader, err := os.OpenFile(mnt.Dir + "/" + "a8.txt", os.O_RDONLY, 0) 317 | if err != nil { 318 | t.Fatal("not opened, " + err.Error()) 319 | } 320 | defer reader.Close() 321 | 322 | fourBytes := make([]byte, 4) 323 | 324 | reader.Seek(24, 0) // seek from start 325 | _, _ = reader.Read(fourBytes) 326 | if "test" != string(fourBytes) { 327 | t.Fatal("not overwritten") 328 | } 329 | } 330 | 331 | func TestWriteAtPositionAfterEnd(t *testing.T) { 332 | mnt, mntErr := fstestutil.MountedT(t, CreateRamFS(), nil) 333 | defer mnt.Close() 334 | 335 | writer, createErr := os.Create(mnt.Dir + "/" + "a9.txt") 336 | defer writer.Close() 337 | 338 | _, writeErr1 := writer.WriteString("abcdefghijklmnopqrstuvwxyz") 339 | 340 | if mntErr != nil || createErr != nil || writeErr1 != nil { 341 | t.Fail() 342 | } 343 | 344 | writer.Seek(3, 2) 345 | 346 | _, writeErr2 := writer.WriteString("test") 347 | if writeErr2 != nil { 348 | t.Fail() 349 | } 350 | 351 | writer.Close() 352 | 353 | fileInfo, errStat := os.Stat(mnt.Dir + "/" + "a9.txt") 354 | if errStat != nil { 355 | t.Fatal("no stat on written file") 356 | } 357 | if fileInfo.Size() != 26+3+4 { 358 | t.Fatalf("file not expanded by 7 bytes: %d", fileInfo.Size()) 359 | } 360 | 361 | reader, err := os.OpenFile(mnt.Dir + "/" + "a9.txt", os.O_RDONLY, 0) 362 | if err != nil { 363 | t.Fatal("not opened, " + err.Error()) 364 | } 365 | defer reader.Close() 366 | 367 | fourBytes := make([]byte, 4) 368 | 369 | reader.Seek(-4, 2) // seek from end 370 | _, _ = reader.Read(fourBytes) 371 | if "test" != string(fourBytes) { 372 | t.Fatal("not overwritten") 373 | } 374 | 375 | reader.Seek(-8, 2) // seek from end 376 | eightBytes := make([]byte, 8) 377 | _, _ = reader.Read(eightBytes) 378 | 379 | if "z\000\000\000test" != string(eightBytes) { 380 | t.Fatal("not overwritten") 381 | } 382 | } 383 | 384 | -------------------------------------------------------------------------------- /src/ramdisk/notification.go: -------------------------------------------------------------------------------- 1 | package ramdisk 2 | 3 | type FSEvent struct { 4 | File *FileEntry 5 | } 6 | 7 | type EventFileCreated struct { 8 | FSEvent 9 | } 10 | type EventFileOpened struct { 11 | FSEvent 12 | } 13 | type EventFileRead struct { 14 | FSEvent 15 | } 16 | type EventFileWritten struct { 17 | FSEvent 18 | } 19 | type EventFileClosed struct { 20 | FSEvent 21 | } 22 | 23 | type FSEvents struct { 24 | FileCreated chan EventFileCreated 25 | FileOpened chan EventFileOpened 26 | FileRead chan EventFileRead 27 | FileWritten chan EventFileWritten 28 | FileClosed chan EventFileClosed 29 | Unmount chan bool 30 | } 31 | 32 | func NewFSEvents() (fsevents FSEvents) { 33 | fsevents = FSEvents{ 34 | FileCreated: make(chan EventFileCreated), 35 | FileOpened: make(chan EventFileOpened), 36 | FileRead: make(chan EventFileRead), 37 | FileWritten: make(chan EventFileWritten), 38 | FileClosed: make(chan EventFileClosed), 39 | Unmount: make(chan bool), 40 | } 41 | return 42 | } 43 | -------------------------------------------------------------------------------- /src/ramdisk/notification_test.go: -------------------------------------------------------------------------------- 1 | package ramdisk 2 | 3 | import ( 4 | "testing" 5 | "bazil.org/fuse/fs/fstestutil" 6 | "os" 7 | "time" 8 | ) 9 | 10 | func TestNotification(t *testing.T) { 11 | fs := CreateRamFS() 12 | 13 | mnt, _ := fstestutil.MountedT(t, fs, nil) 14 | defer mnt.Close() 15 | 16 | notification := NewFSEvents() 17 | fs.AddListener(¬ification) 18 | 19 | writer, _ := os.Create(mnt.Dir + "/" + "b1.txt") 20 | defer writer.Close() 21 | 22 | select { 23 | case <-notification.FileCreated: 24 | // success 25 | case <-time.After(1*time.Minute): 26 | t.Fatal("missing FileCreated") 27 | } 28 | 29 | writer.Write([]byte("test")) 30 | select { 31 | case <-notification.FileWritten: 32 | // success 33 | case <-time.After(1*time.Minute): 34 | t.Fatal("missing FileWritten") 35 | } 36 | 37 | writer.Seek(0, 0) 38 | writer.Read(make([]byte, 4)) 39 | select { 40 | case <-notification.FileRead: 41 | // success 42 | case <-time.After(1*time.Minute): 43 | t.Fatal("missing FileRead") 44 | } 45 | 46 | writer.Close() 47 | select { 48 | case <-notification.FileClosed: 49 | // success 50 | case <-time.After(1*time.Minute): 51 | t.Fatal("missing FileClosed") 52 | } 53 | } 54 | 55 | --------------------------------------------------------------------------------